jjSTREAM class JJ2+

A buffer of bytes for storing data and exchanging it with files

Data is appended at the end and read from the front, which removes it. push() and pop() store and restore values of primitive types, strings and other streams in binary form; write(), get() and getLine() work with raw bytes. A stream can be saved to a file and created from one.

Constructors

jjSTREAM() C++
Creates an empty stream
jjSTREAM(const string&in filename)
Creates a stream with the contents of a file

Functions

void clear() C++
Removes all contents of the stream
bool discard(uint count)
Removes count bytes from the front of the stream
bool get(string&out value, uint count = 1)
Reads count bytes from the front of the stream into a string
bool get(jjSTREAM&out value, uint count = 1)
Reads count bytes from the front of the stream and appends them to another stream
bool getLine(string&out value, const string&in delim = '\n')
Reads text from the front of the stream up to a delimiter
uint getSize() const C++
Returns the number of bytes left in the stream
bool isEmpty() const C++
Returns whether no bytes are left in the stream
bool pop(bool&out value)
Reads a boolean value from the front of the stream
bool pop(uint8&out value) C++
Reads an unsigned 8-bit value from the front of the stream
bool pop(int8&out value) C++
Reads a signed 8-bit value from the front of the stream
bool pop(uint16&out value) C++
Reads an unsigned 16-bit value from the front of the stream
bool pop(int16&out value) C++
Reads a signed 16-bit value from the front of the stream
bool pop(uint32&out value) C++
Reads an unsigned 32-bit value from the front of the stream
bool pop(int32&out value) C++
Reads a signed 32-bit value from the front of the stream
bool pop(uint64&out value) C++
Reads an unsigned 64-bit value from the front of the stream
bool pop(int64&out value) C++
Reads a signed 64-bit value from the front of the stream
bool pop(float&out value) C++
Reads a single-precision float from the front of the stream
bool pop(double&out value) C++
Reads a double-precision float from the front of the stream
bool pop(string&out value)
Reads a string stored by push() from the front of the stream
bool pop(jjSTREAM&out value)
Reads a stream stored by push() from the front of the stream and appends it to another stream
bool push(bool value)
Appends a boolean value to the stream
bool push(uint8 value) C++
Appends an unsigned 8-bit value to the stream
bool push(int8 value) C++
Appends a signed 8-bit value to the stream
bool push(uint16 value) C++
Appends an unsigned 16-bit value to the stream
bool push(int16 value) C++
Appends a signed 16-bit value to the stream
bool push(uint32 value) C++
Appends an unsigned 32-bit value to the stream
bool push(int32 value) C++
Appends a signed 32-bit value to the stream
bool push(uint64 value) C++
Appends an unsigned 64-bit value to the stream
bool push(int64 value) C++
Appends a signed 64-bit value to the stream
bool push(float value) C++
Appends a single-precision float to the stream
bool push(double value) C++
Appends a double-precision float to the stream
bool push(const string&in value) C++
Appends a string to the stream, preceded by its length
bool push(const jjSTREAM&in value) C++
Appends the unread contents of another stream, preceded by their length
bool save(const string&in filename) const
Saves the contents of the stream to a file and returns whether it succeeded
bool write(const string&in value)
Appends the bytes of a string to the stream
bool write(const jjSTREAM&in value) C++
Appends the unread bytes of another stream, without storing their length; always returns true

Operators

jjSTREAM& opAssign(const jjSTREAM&in o) = C++
Replaces the contents of this stream with the unread contents of another one

Constructor documentation

jjSTREAM::jjSTREAM(const string&in filename)

Creates a stream with the contents of a file

The file is looked up next to the game's data. If it doesn't exist or its name points outside that directory, the stream is empty, so isEmpty() tells whether anything was saved before.

Function documentation

bool jjSTREAM::discard(uint count)

Removes count bytes from the front of the stream

Returns false and leaves the stream unchanged if fewer bytes are left.

bool jjSTREAM::get(string&out value, uint count = 1)

Reads count bytes from the front of the stream into a string

The bytes are removed from the stream. Returns false and leaves the stream unchanged if fewer bytes are left.

bool jjSTREAM::get(jjSTREAM&out value, uint count = 1)

Reads count bytes from the front of the stream and appends them to another stream

The bytes are removed from this stream. Returns false and leaves it unchanged if fewer bytes are left.

bool jjSTREAM::getLine(string&out value, const string&in delim = '\n')

Reads text from the front of the stream up to a delimiter

The text before delim is stored in value, and both are removed from the stream. If the delimiter isn't found, the rest of the stream becomes the line and the stream ends up empty. Returns false only if the stream is empty or delim is empty.

bool jjSTREAM::pop(bool&out value)

Reads a boolean value from the front of the stream

Every pop() overload reads a value stored by the matching push() and removes its bytes. Returns false if the stream doesn't hold enough bytes for the value.

bool jjSTREAM::pop(string&out value)

Reads a string stored by push() from the front of the stream

Returns false and leaves the stream unchanged if the stored length exceeds the bytes left.

bool jjSTREAM::pop(jjSTREAM&out value)

Reads a stream stored by push() from the front of the stream and appends it to another stream

Returns false and leaves the stream unchanged if the stored length exceeds the bytes left.

bool jjSTREAM::push(bool value)

Appends a boolean value to the stream

Every push() overload appends its value in binary form and returns true: 1 byte for bool, int8 and uint8, 2 bytes for the 16-bit types, 4 bytes for int, uint and float, 8 bytes for the 64-bit types and double. Strings and streams take 4 bytes of length followed by their bytes. Read the values back with pop() in the same order and types.

bool jjSTREAM::save(const string&in filename) const

Saves the contents of the stream to a file and returns whether it succeeded

The file is written next to the game's data; names that point outside that directory are rejected. The stream itself is left unchanged. Always fails while the level runs inside the level editor.

bool jjSTREAM::write(const string&in value)

Appends the bytes of a string to the stream

Unlike push(), no length is stored, so the text can be read back only with get() and a known size, or with getLine(). Always returns true.