FileReader

For reading files. Can also be used for writing

Constructors

this
this(string filename)

If filename is not null, attempts to load file into memory

Destructor

~this
~this()
Undocumented in source.

Members

Functions

clear
void clear()

Clears the stream, resets the stream

loadFile
void loadFile(string filename)

loads file into memory, throws exception if fails

read
void[] read(uinteger size)

reads and returns size number of bytes from file starting from seek-position If not enough bytes are left, the array returned will be smaller than size Returns null if the seek-position is at end, or if there are no bytes to be read

read
void[] read(ubyte terminate)

Reads and returns bytes starting from seek till terminate byte, or if EOF is reached

remove
bool remove(uinteger count)

Removes a number of bytes starting from the seek-position

saveFile
void saveFile(string filename)

Writes the stream to a file, throws exception if fails

write
void write(ubyte[] t)

Writes an array at the seek-position, and moves seek to end of the written data

write
void write(ubyte t)

Writes a byte at the seek-position, and moves seek to end of the written data

Properties

seek
uinteger seek [@property getter]

The seek position, from where the next char(s) will be read, or written to

seek
uinteger seek [@property setter]

The seek position, from where the next char(s) will be read, or written to

size
uinteger size [@property getter]

The size of file in bytes, read-only

Examples

unittests for FileReader

// file loading/ saving not tested
FileReader stream = new FileReader();
assert(stream.seek == 0);
// write & read
stream.write(cast(ubyte[])"ABCE");
assert(stream.seek == 4);
assert(stream.size == 4);
stream.seek = 3;
stream.write(cast(ubyte)'D');
assert(stream.size == 5);
assert(stream.seek == 4);
stream.seek = 0;
assert(stream.read(stream.size) == cast(ubyte[])"ABCDE");
stream.seek = 0;
assert(stream.read(cast(ubyte)'C') == cast(ubyte[])"ABC");
stream.seek = 0;
assert(stream.read(cast(ubyte)'Z') == cast(ubyte[])"ABCDE");
// clear
stream.clear;
assert(stream.size == 0);
assert(stream.seek == 0);
// remove
stream.write(cast(ubyte[])"ABCDE");
stream.seek = 3;
assert(stream.remove(99) == true);
stream.seek = 0;
assert(stream.read(cast(ubyte)'Z') == cast(ubyte[])"AB");
stream.write(cast(ubyte[])"CDE");
stream.seek = 1;
assert(stream.remove(2) == true);
stream.seek = 0;
assert(stream.read(cast(ubyte)'Z') == "DE");

Meta