|
1 | 1 | \section{\module{chunk} --- |
2 | | - Helper for reading IFF chunks} |
| 2 | + Read IFF chunked data} |
3 | 3 |
|
4 | 4 | \declaremodule{standard}{chunk} |
5 | | -\sectionauthor{Moshe Zadka}{ [email protected]} |
6 | | -\modulesynopsis{Helper class for reading from IFF-based file formats.} |
| 5 | +\modulesynopsis{Module to read IFF chunks.} |
| 6 | +\moduleauthor{Sjoerd Mullender}{ [email protected]} |
| 7 | +\sectionauthor{Sjoerd Mullender}{ [email protected]} |
7 | 8 |
|
8 | | -The \module{chunk} module defines a class for interfacing to ``IFF'' |
9 | | -chunk-based files, like TIFF or AIFF. This is used as a helper module |
10 | | -for the \refmodule{aifc} and \refmodule{wave} modules. |
11 | 9 |
|
12 | | -The \module{chunk} module defines the following class: |
13 | 10 |
|
14 | | -\begin{classdesc}{Chunk}{file\optional{, align}} |
15 | | -The chunk from \var{file} starting at \var{file}'s current |
16 | | -position. The \var{align} argument, which defaults to true, determines |
17 | | -whether to align chunk data on 2-byte boundaries. |
18 | | - |
19 | | -\exception{EOFError} is raised if \var{file} does not contain enough |
20 | | -data to read the IFF header. |
21 | | -\end{classdesc} |
| 11 | +This module provides an interface for reading files that use EA IFF 85 |
| 12 | +chunks.\footnote{``EA IFF 85'' Standard for Interchange Format Files, |
| 13 | +Jerry Morrison, Electronic Arts, January 1985.} This format is used |
| 14 | +in at least the Audio\index{Audio Interchange File |
| 15 | +Format}\index{AIFF}\index{AIFF-C} Interchange File Format |
| 16 | +(AIFF/AIFF-C), the Real\index{Real Media File Format} Media File |
| 17 | +Format\index{RMFF} (RMFF), and the |
| 18 | +Tagged\index{Tagged Image File Format} Image File Format\index{TIFF} |
| 19 | +(TIFF). |
22 | 20 |
|
23 | | -The IFF header format is described in this table: |
| 21 | +A chunk has the following structure: |
24 | 22 |
|
25 | 23 | \begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents} |
26 | 24 | \lineiii{0}{4}{Chunk ID} |
27 | 25 | \lineiii{4}{4}{Size of chunk in big-endian byte order, including the |
28 | 26 | header} |
| 27 | + \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in |
| 28 | + the preceeding field} |
| 29 | + \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and |
| 30 | + chunk alignment is used} |
29 | 31 | \end{tableiii} |
30 | 32 |
|
| 33 | +The ID is a 4-byte string which identifies the type of chunk. |
| 34 | + |
| 35 | +The size field (a 32-bit value, encoded using big-endian byte order) |
| 36 | +gives the size of the whole chunk, including the 8-byte header. |
31 | 37 |
|
32 | | -\subsection{Chunk Objects \label{iff-chunk-objects}} |
| 38 | +Usually an IFF-type file consists of one or more chunks. The proposed |
| 39 | +usage of the \class{Chunk} class defined here is to instantiate an |
| 40 | +instance at the start of each chunk and read from the instance until |
| 41 | +it reaches the end, after which a new instance can be instantiated. |
| 42 | +At the end of the file, creating a new instance will fail with a |
| 43 | +\exception{EOFError} exception. |
33 | 44 |
|
34 | | -Chunk objects have the following methods: |
| 45 | +\begin{classdesc}{Chunk}{file\optional{, align}} |
| 46 | +Class which represents a chunk. The \var{file} argument is expected |
| 47 | +to be a file-like object. An instance of this class is specifically |
| 48 | +allowed. The only method that is needed is \method{read()}. If the |
| 49 | +methods \method{seek()} and \method{tell()} are present and don't |
| 50 | +raise an exception, they are also used. If these methods are present |
| 51 | +and raise an exception, they are expected to not have altered the |
| 52 | +object. If the optional argument \var{align} is true, chunks are |
| 53 | +assumed to be aligned on 2-byte boundaries. If \var{align} is |
| 54 | +false, no alignment is assumed. The default value is true. |
| 55 | +\end{classdesc} |
| 56 | + |
| 57 | +A \class{Chunk} object supports the following methods: |
35 | 58 |
|
36 | 59 | \begin{methoddesc}{getname}{} |
37 | | -Return the ID of the chunk. |
| 60 | +Returns the name (ID) of the chunk. This is the first 4 bytes of the |
| 61 | +chunk. |
38 | 62 | \end{methoddesc} |
39 | 63 |
|
40 | 64 | \begin{methoddesc}{close}{} |
41 | | -Close the chunk, forwarding the file pointer to the end of the chunk. |
| 65 | +Close and skip to the end of the chunk. This does not close the |
| 66 | +underlying file. |
42 | 67 | \end{methoddesc} |
43 | 68 |
|
| 69 | +The remaining methods will raise \exception{IOError} if called after |
| 70 | +the \method{close()} method has been called. |
| 71 | + |
44 | 72 | \begin{methoddesc}{isatty}{} |
45 | | -Returns false unless the chunk has been closed, in which case |
46 | | -\exception{ValueError} is raised. |
| 73 | +Returns \code{0}. |
47 | 74 | \end{methoddesc} |
48 | 75 |
|
49 | | -\begin{methoddesc}{seek}{offset\optional{, whence}} |
50 | | -Seek to a position within the chunk. If file pointer is not seekable, |
51 | | -or \var{offset} would point outside the chunk, an error is raised. |
52 | | -\var{whence} is interpreted the same as for the \method{seek()} method |
53 | | -on file objects; see section \ref{bltin-file-objects} for more |
54 | | -information. |
| 76 | +\begin{methoddesc}{seek}{pos\optional{, whence}} |
| 77 | +Set the chunk's current position. The \var{whence} argument is |
| 78 | +optional and defaults to \code{0} (absolute file positioning); other |
| 79 | +values are \code{1} (seek relative to the current position) and |
| 80 | +\code{2} (seek relative to the file's end). There is no return value. |
| 81 | +If the underlying file does not allow seek, only forward seeks are |
| 82 | +allowed. |
55 | 83 | \end{methoddesc} |
56 | 84 |
|
57 | 85 | \begin{methoddesc}{tell}{} |
58 | | -Return the current position within this chunk. |
| 86 | +Return the current position into the chunk. |
59 | 87 | \end{methoddesc} |
60 | 88 |
|
61 | | -\begin{methoddesc}{read}{\optional{n}} |
62 | | -Read at most \var{n} bytes from the chunk. If \var{n} is omitted |
63 | | -or negative, read until the end of the chunk. |
| 89 | +\begin{methoddesc}{read}{\optional{size}} |
| 90 | +Read at most \var{size} bytes from the chunk (less if the read hits |
| 91 | +the end of the chunk before obtaining \var{size} bytes). If the |
| 92 | +\var{size} argument is negative or omitted, read all data until the |
| 93 | +end of the chunk. The bytes are returned as a string object. An |
| 94 | +empty string is returned when the end of the chunk is encountered |
| 95 | +immediately. |
64 | 96 | \end{methoddesc} |
65 | 97 |
|
66 | 98 | \begin{methoddesc}{skip}{} |
|
0 commit comments