Determining the format of a file
Common Graphics provides methods to do the following:
• Determine whether a file has a given file format
• Determine which format a given file has
The formatMatchesFileHandle:atOffset: class method answers true if the given file matches the receiver's format, false if it does not. For example, the following code tests whether the file named my-image.pcx is actually a PCX image file:
| file |
file := CfsFileDescriptor open: 'my-image.pcx' oflag: ORDONLY.
(CgPCXFileFormat formatMatchesFileHandle: file atOffset: 0)
ifTrue: [Transcript cr; show: 'Format matches.']
ifFalse: [Transcript cr; show: 'Format does not match.'].
file close
The formatMatchingFileHandle:atOffset:ifNone: class method determines which format matches the given file. It answers the appropriate file format class if a matching format was found. If one was not found, the block passed as the last argument is evaluated and its result is answered. The following code determines the format of the file named my-image.pcx:
| file formatClass |
file := CfsFileDescriptor open: 'my-image.pcx' oflag: ORDONLY.
formatClass := CgImageFileFormat
formatMatchingFileHandle: file
atOffset: 0
ifNone: [file close. ^self error: 'No matching format found'].
file close.
Transcript cr; show: 'Matching format: ', formatClass printString
When the input is an array of ByteArrays, use the formatMatchesByteObjects:- offsetsIntoByteObjects: and formatMatchingByteObjects:offsetsIntoByteObjects:- ifNone: class methods.
Last modified date: 01/29/2015