7 The file Library

The file library implements the diskEntry, file, fileRep, patterns, all used to model external storage media such as disk files.

7.1 File and diskEntry

The file library implements the file pattern that is used to represent external storage media such as disk files. File is an abstract subpattern of stream, specifying the machine independent attributes of such media. Specific subpatterns: exists for the different machine types such as UNIX and Macintosh (unixFile, respectively macFile, see later).

The attributes of file are divided into two parts: the disk entry attributes and the contents related attributes. The disk entry attributes are located in the entry attribute of file, whereas the contents related attributes are ordinary attributes of file.

7.2 Disk entry attributes

The disk entry attributes are defined in the diskEntry pattern: path, size, readable, writable, isFile, isDirectory, exists, modtime, touch, and rename. Also related to the disk entry are the following exceptions: diskEntryExistsException, diskEntryModtimeException, diskEntryTouchException, and diskEntryRenameException. These attributes are accessed through the entry attribute of a file, e.g. if aFile is a file object, then aFile.entry.modtime will return the last modification time of the associated disk file.

7.3 File attributes

The contents related attributes are: entry, name, touch, delete, openRead, openWrite, openAppend, openReadWrite, openReadAppend, flush, and close. Note that file also inherits all the stream attributes (further binding several of them). File also defines the following exceptions: openException, accessError, writeError, readError, noSuchFileError, fileExistsError, noSpaceError, and otherError.

7.4 The FileRep Library

The fileRep pattern is consisting of a repetition of integers and operations which makes it possible to save and restore this repetition in one chunk from a file. When saving, the repetition elements [1:top-1] are saved (top is an attribute of fileRep) and when restoring, top will become equal to range of the stored repetition. The repetition is in the R attribute, and save and restore is used for saving and restoring the repetition onto some file.

7.5 The BinFile Library

This fragment declare attributes for direct reading/writing of various data sizes to a file. The data is written out exactly as is:
64 -> aBinFile.putLong
will write the number 0x00000020 to aBinFile, whereas
64 -> aBinFile.putInt
will write the two characters '6' (ascii 54) and '4' (ascii 52) to the file.

The operations putBytes and getBytes allow an arbitrary sequence of bytes to be written/read to/from a file. E.g.

buffer: [1000]@char;
   putB: @aFile.putBytes;
do ...
   (@@buffer[1],500) -> putB;
This will write the first 500 characters from the buffer repetition to the file. NOTICE, that you must have a static instance of putBytes/getBytes when using them, since they require an address argument. If dynamic instances are used, a garbage- collection may be triggered, and the address argument would be illegal.

These operations are declared in FileLib, e.g. they become usable for any file, by just including this fragment file. However, on some platforms, the "binary" virtual of File MUST be further bound to TrueObject for these operations to work. The binfile pattern below adds this further binding.

You should remember this further binding if you are using these operations on a file, that is not a binfile.

Exceptions: If any of the put-operations fail, they raise the WriteError file-exception. If any of the get-operations fail, they raise the ReadError file-exception.


Basic Libraries - Reference Manual
© 1990-2004 Mjølner Informatics
[Modified: Friday November 10th 2000 at 14:53]