source: vendor/python/2.5/Doc/lib/libchunk.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 4.5 KB
Line 
1\section{\module{chunk} ---
2 Read IFF chunked data}
3
4\declaremodule{standard}{chunk}
5\modulesynopsis{Module to read IFF chunks.}
6\moduleauthor{Sjoerd Mullender}{sjoerd@acm.org}
7\sectionauthor{Sjoerd Mullender}{sjoerd@acm.org}
8
9
10
11This module provides an interface for reading files that use EA IFF 85
12chunks.\footnote{``EA IFF 85'' Standard for Interchange Format Files,
13Jerry Morrison, Electronic Arts, January 1985.} This format is used
14in at least the Audio\index{Audio Interchange File
15Format}\index{AIFF}\index{AIFF-C} Interchange File Format
16(AIFF/AIFF-C) and the Real\index{Real Media File Format} Media File
17Format\index{RMFF} (RMFF). The WAVE audio file format is closely
18related and can also be read using this module.
19
20A chunk has the following structure:
21
22\begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents}
23 \lineiii{0}{4}{Chunk ID}
24 \lineiii{4}{4}{Size of chunk in big-endian byte order, not including the
25 header}
26 \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in
27 the preceding field}
28 \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and
29 chunk alignment is used}
30\end{tableiii}
31
32The ID is a 4-byte string which identifies the type of chunk.
33
34The size field (a 32-bit value, encoded using big-endian byte order)
35gives the size of the chunk data, not including the 8-byte header.
36
37Usually an IFF-type file consists of one or more chunks. The proposed
38usage of the \class{Chunk} class defined here is to instantiate an
39instance at the start of each chunk and read from the instance until
40it reaches the end, after which a new instance can be instantiated.
41At the end of the file, creating a new instance will fail with a
42\exception{EOFError} exception.
43
44\begin{classdesc}{Chunk}{file\optional{, align, bigendian, inclheader}}
45Class which represents a chunk. The \var{file} argument is expected
46to be a file-like object. An instance of this class is specifically
47allowed. The only method that is needed is \method{read()}. If the
48methods \method{seek()} and \method{tell()} are present and don't
49raise an exception, they are also used. If these methods are present
50and raise an exception, they are expected to not have altered the
51object. If the optional argument \var{align} is true, chunks are
52assumed to be aligned on 2-byte boundaries. If \var{align} is
53false, no alignment is assumed. The default value is true. If the
54optional argument \var{bigendian} is false, the chunk size is assumed
55to be in little-endian order. This is needed for WAVE audio files.
56The default value is true. If the optional argument \var{inclheader}
57is true, the size given in the chunk header includes the size of the
58header. The default value is false.
59\end{classdesc}
60
61A \class{Chunk} object supports the following methods:
62
63\begin{methoddesc}{getname}{}
64Returns the name (ID) of the chunk. This is the first 4 bytes of the
65chunk.
66\end{methoddesc}
67
68\begin{methoddesc}{getsize}{}
69Returns the size of the chunk.
70\end{methoddesc}
71
72\begin{methoddesc}{close}{}
73Close and skip to the end of the chunk. This does not close the
74underlying file.
75\end{methoddesc}
76
77The remaining methods will raise \exception{IOError} if called after
78the \method{close()} method has been called.
79
80\begin{methoddesc}{isatty}{}
81Returns \code{False}.
82\end{methoddesc}
83
84\begin{methoddesc}{seek}{pos\optional{, whence}}
85Set the chunk's current position. The \var{whence} argument is
86optional and defaults to \code{0} (absolute file positioning); other
87values are \code{1} (seek relative to the current position) and
88\code{2} (seek relative to the file's end). There is no return value.
89If the underlying file does not allow seek, only forward seeks are
90allowed.
91\end{methoddesc}
92
93\begin{methoddesc}{tell}{}
94Return the current position into the chunk.
95\end{methoddesc}
96
97\begin{methoddesc}{read}{\optional{size}}
98Read at most \var{size} bytes from the chunk (less if the read hits
99the end of the chunk before obtaining \var{size} bytes). If the
100\var{size} argument is negative or omitted, read all data until the
101end of the chunk. The bytes are returned as a string object. An
102empty string is returned when the end of the chunk is encountered
103immediately.
104\end{methoddesc}
105
106\begin{methoddesc}{skip}{}
107Skip to the end of the chunk. All further calls to \method{read()}
108for the chunk will return \code{''}. If you are not interested in the
109contents of the chunk, this method should be called so that the file
110points to the start of the next chunk.
111\end{methoddesc}
Note: See TracBrowser for help on using the repository browser.