dvi2bitmap
dvi2bitmap1.0
|
Abstracts a file as a stream of bytes. More...
#include <InputByteStream.h>
Public Member Functions | |
InputByteStream (int fileno) throw (InputByteStreamError) | |
Prepares to read a stream from the specified file descriptor, which must be open. More... | |
InputByteStream (string srcspec) throw (InputByteStreamError) | |
Prepares to read a stream from the specified source. More... | |
~InputByteStream () | |
Closes the file and reclaims any buffers. More... | |
bool | eof () |
Indicates whether we are at the end of the file. More... | |
virtual void | close () |
Closes the stream, releasing all resources. More... | |
Byte | getByte (void) throw (InputByteStreamError) |
Reads a byte from the stream. More... | |
const Byte * | getBlock (unsigned int length) throw (InputByteStreamError) |
Retrieves a block from the current position in the stream. More... | |
void | skip (unsigned int) throw (InputByteStreamError) |
Skips a given number of bytes forward in the stream. More... | |
signed int | getSIU (int) throw (InputByteStreamError) |
Obtains an n-byte unsigned integer from the stream, as a signed int. More... | |
signed int | getSIS (int) throw (InputByteStreamError) |
Obtains an n-byte signed integer from the stream, as a signed int. More... | |
unsigned int | getUIU (int) throw (InputByteStreamError) |
Obtains an n-byte unsigned integer from the stream, as an unsigned int. More... | |
Static Public Member Functions | |
static unsigned int | getUIU (int, const Byte *) throw (InputByteStreamError) |
Obtains an n-byte unsigned integer from the beginning of a Byte array, as an unsigned int. More... | |
static void | setDefaultBufferSize (unsigned int length) |
Sets the default buffer size to be used for reading files. More... | |
static void | verbosity (const verbosities level) |
Sets the verbosity of this module. More... | |
static verbosities | getVerbosity (void) |
Returns the verbosity setting of this class. More... | |
Protected Member Functions | |
InputByteStream () | |
No-argument constructor creates a new InputByteStream object, but does not associate it with any source of bytes. More... | |
bool | bindToFileDescriptor (int fileno, string filename="", int bufsize=0, bool fillBufferAndClose=false, bool assertIsSeekable=false) throw (InputByteStreamError) |
Binds this stream to a given file descriptor. More... | |
int | openSourceSpec (string srcspec) throw (InputByteStreamError) |
Opens a source. More... | |
int | getFD (void) const |
Returns the file descriptor this stream is bound to. More... | |
void | bufferSeek (unsigned int pos) throw (InputByteStreamError) |
Seeks to a specific point in the buffer. More... | |
void | reloadBuffer (void) |
Reloads the buffer, presumably after the file descriptor has been adjusted by an extending class. More... | |
Abstracts a file as a stream of bytes.
Functions are provided to read individual bytes from the file and blocks of contiguous bytes.
Since this class is intended to help reading TeX DVI and PK files, we also provide methods to read signed and unsigned integers from the stream, encoded as described in the DVI driver standard.
This class is designed to be extended (and is extended in fact in classes FileByteStream and PipeStream). The subclassing interface consists of a no-argument constructor, a method to associate the class with a given file descriptor (bindToFileDescriptor), and a convenience method to help opening files, using the same specification syntax supported by this class (openSourceSpec). Since one of the main purposes of this subclassing is to support a class which handles a seekable input stream, we also have getFD to get the file descriptor being handled, and reloadBuffer to indicate to the parent class that the underlying stream has been modified (typically by a seek
operation) so that the input buffer should be invalidated. If bindToFileDescriptor
was invoked with the fillBufferAndClose
flag true, then the bufferSeek method allows rapid seeking by simply adjusting the buffer pointer; though this is useless unless the whole stream is in the buffer, the class makes no check on this, and it is the extending class's responsibility to make sure that this is not called inappropriately.
InputByteStream::InputByteStream | ( | int | fd) | ||
throw | ( | InputByteStreamError | |||
) |
Prepares to read a stream from the specified file descriptor, which must be open.
fd | an open file descriptor |
InputByteStreamError | if there is a problem binding to the descriptor |
InputByteStream::InputByteStream | ( | string | srcspec) | ||
throw | ( | InputByteStreamError | |||
) |
Prepares to read a stream from the specified source.
The source may be
<osfile>filename
: the specified file is opened <osfd>integer
: the integer specifyies an (open) OS file descriptor; thus <osfd>0
opens the standard input srcspec | a source specification as described above |
InputByteStreamError | if there is a problem opening or binding to the descriptor |
InputByteStream::~InputByteStream | ( | ) |
Closes the file and reclaims any buffers.
References close().
|
protected |
No-argument constructor creates a new InputByteStream object, but does not associate it with any source of bytes.
To associate it with a source, use bindToFileDescriptor or the convenience method openSourceSpec.
|
protected |
Binds this stream to a given file descriptor.
If the parameter fillBufferAndClose
is true, then this method will keep reading from the file descriptor until it reaches either the end of the newly-allocated buffer, or end-of-file, whichever comes first. It will then close the file descriptor. In this case (and in this case alone), the method bufferSeek becomes useful, and can be used by an extending class to implement an efficient seek
operation on the file.
fileno | the file descriptor to be handled by this object |
filename | the file name associated with this descriptor; this may be the empty string |
bufsize | a size suggested for the input buffer, or zero to accept the default |
fillBufferAndClose | if true, read the entire contents of the file into memory |
assertIsSeekable | if true, the caller is asserting that the given file descriptor points to a seekable object (presumably because it will depend on it being so). If this is not the case (the descriptor refers to a pipe or other non-seekable, and non-mappable, object), then throw an exception. If this is false, no assertion is being made; it is not being asserted that the object is non-seekable. |
InputByteStreamError | if there is some other problem reading the file; or if a negative buffer size is given; or if the assertion described for parameter assertIsSeekable turns out to be false. |
References debug, normal, and Util::verbosity_.
|
protected |
Seeks to a specific point in the buffer.
This is only useful when the buffer holds the complete file, that is, when bindToFileDescriptor
was called with parameter fillBufferAndClose
true.
offset | the offset from the beginning of the buffer, where the current position is relocated to |
InputByteStreamError | if the offset would take the pointer outside the buffer, or if the stream has been closed |
References debug, and Util::verbosity_.
|
virtual |
Closes the stream, releasing all resources.
Reimplemented in PipeStream.
References normal, and Util::verbosity_.
Referenced by PipeStream::close(), and ~InputByteStream().
bool InputByteStream::eof | ( | ) |
Indicates whether we are at the end of the file.
This method does not return true until after a failed attempt to read past the end of file; that is, it does not return true immediately after the last byte has been read from the file.
Referenced by DviFile::eof(), and getByte().
const Byte * InputByteStream::getBlock | ( | unsigned int | length) | ||
throw | ( | InputByteStreamError | |||
) |
Retrieves a block from the current position in the stream.
Leaves the pointer pointing after the block returned.
length | the size of block desired |
InputByteStreamError | if the requested number of bytes cannot be read, which includes the case of this method being called when eof() is true |
Byte InputByteStream::getByte | ( | void | ) | ||
throw | ( | InputByteStreamError | |||
) |
Reads a byte from the stream.
Increments the reading pointer. This method does not signal an error at end-of-file; if eof is true or becomes true as a result of this attempt to read past the end of the file, then we return zero. That is, eof()
does not return true immediately the last byte in the file has been read.
InputByteStreamError | if there is some problem reading the stream |
References eof().
|
inlineprotected |
Returns the file descriptor this stream is bound to.
If there is no open descriptor (see the fillBufferAndClose
parameter to bindToFileDescriptor), returns negative.
signed int InputByteStream::getSIS | ( | int | n) | ||
throw | ( | InputByteStreamError | |||
) |
Obtains an n-byte signed integer from the stream, as a signed int.
n | the number of bytes to read, in the range 1–4 inclusive |
InputByteStreamError | if parameter n was out of range |
signed int InputByteStream::getSIU | ( | int | n) | ||
throw | ( | InputByteStreamError | |||
) |
Obtains an n-byte unsigned integer from the stream, as a signed int.
n | the number of bytes to read, in the range 1–3 inclusive (there are no 4-byte unsigned quantities in DVI files) |
InputByteStreamError | if parameter n was out of range |
unsigned int InputByteStream::getUIU | ( | int | n) | ||
throw | ( | InputByteStreamError | |||
) |
Obtains an n-byte unsigned integer from the stream, as an unsigned int.
n | the number of bytes to read, in the range 1–4 inclusive |
InputByteStreamError | if parameter n was out of range |
|
static |
Obtains an n-byte unsigned integer from the beginning of a Byte
array, as an unsigned int.
This has little specifically to do with Input streams, and is here as a convenience method.
n | the number of bytes to read, in the range 1–4 inclusive |
p | a pointer to an array of Byte values |
InputByteStreamError | if parameter n was out of range |
|
inlinestatic |
Returns the verbosity setting of this class.
Referenced by PipeStreamSignalHandling::expectAnother(), and PipeStreamSignalHandling::got_status().
|
protected |
Opens a source.
The source is specified as in InputByteStream(string). Throws an exception on any problems, so that if it returns, it has successfully opened the file, or determined that the file descriptor is (syntactically) valid.
srcspec | a source specification |
InputByteStreamError | if there is any problem opening the file |
References normal, and Util::verbosity_.
|
protected |
Reloads the buffer, presumably after the file descriptor has been adjusted by an extending class.
References debug, and Util::verbosity_.
|
static |
Sets the default buffer size to be used for reading files.
length | the size, in bytes, of the input buffer |
void InputByteStream::skip | ( | unsigned int | increment) | ||
throw | ( | InputByteStreamError | |||
) |
Skips a given number of bytes forward in the stream.
increment | the number of bytes to move forward in the stream |
InputByteStreamError | if we skip past the end of file |
|
inlinestatic |