TrinityCore
|
#include <BinaryOutput.h>
Private Member Functions | |
void | reserveBytesWhenOutOfMemory (size_t bytes) |
void | reallocBuffer (size_t bytes, size_t oldBufferLen) |
void | reserveBytes (size_t bytes) |
BinaryOutput (const BinaryOutput &) | |
BinaryOutput & | operator= (const BinaryOutput &) |
bool | operator== (const BinaryOutput &) |
Private Attributes | |
std::string | m_filename |
bool | m_committed |
int | m_beginEndBits |
int8 | m_bitString |
int | m_bitPos |
bool | m_swapBytes |
G3DEndian | m_fileEndian |
uint8 * | m_buffer |
size_t | m_bufferLen |
size_t | m_maxBufferLen |
int64 | m_pos |
bool | m_init |
int64 | m_alreadyWritten |
bool | m_ok |
Sequential or random access byte-order independent binary file access.
The compress() call can be used to compress with zlib.
Any method call can trigger an out of memory error (thrown as char*) when writing to "<memory>" instead of a file.
Compressed writing and seeking backwards is not supported for huge files (i.e., BinaryOutput may have to dump the contents to disk if they exceed available RAM).
|
private |
G3D::BinaryOutput::BinaryOutput | ( | ) |
You must call setEndian() if you use this (memory) constructor.
Doesn't actually open the file; commit() does that. Use "<memory>" as the filename if you're going to commit to memory.
Verify ability to write to disk
G3D::BinaryOutput::~BinaryOutput | ( | ) |
void G3D::BinaryOutput::beginBits | ( | ) |
Call before a series of BinaryOutput::writeBits calls. Only writeBits can be called between beginBits and endBits without corrupting the stream.
Write the bytes to disk. It is ok to call this multiple times; it will just overwrite the previous file.
Parent directories are created as needed if they do not exist.
Not called from the destructor; you must call it yourself.
flush | If true (default) the file is ready for reading when the method returns, otherwise the method returns immediately and writes the file in the background. |
void G3D::BinaryOutput::commit | ( | uint8 * | out | ) |
Write the bytes to memory (which must be of at least size() bytes).
void G3D::BinaryOutput::compress | ( | int | level = 9 | ) |
Compresses the data in the buffer in place, preceeding it with a little-endian uint32 indicating the uncompressed size.
Call immediately before commit().
Cannot be used for huge files (ones where the data was already written to disk)– will throw char*.
level | Compression level. 0 = fast, low compression; 9 = slow, high compression |
void G3D::BinaryOutput::endBits | ( | ) |
Call after a series of BinaryOutput::writeBits calls. This will finish out with zeros the last byte into which bits were written.
|
inline |
Returns a pointer to the internal memory buffer.
|
inline |
|
inline |
bool G3D::BinaryOutput::ok | ( | ) | const |
True if no errors have been encountered.
|
private |
|
private |
|
inline |
Returns the current byte position in the file, where 0 is the beginning and getLength() - 1 is the end.
|
private |
|
inlineprivate |
Make sure at least bytes can be written, resizing if necessary.
|
private |
void G3D::BinaryOutput::reset | ( | ) |
A memory BinaryOutput may be reset so that it can be written to again without allocating new memory. The underlying array will not be deallocated, but the reset structure will act like a newly intialized one.
void G3D::BinaryOutput::setEndian | ( | G3DEndian | fileEndian | ) |
|
inline |
Sets the length of the file to n, padding with 0's past the current end. Does not change the position of the next byte to be written unless n < size().
Throws char* when resetting a huge file to be shorter than its current length.
|
inline |
Sets the position. Can set past length, in which case the file is padded with zeros up to one byte before the next to be written.
May throw a char* exception when seeking backwards on a huge file.
|
inline |
|
inline |
Skips ahead n bytes.
void G3D::BinaryOutput::writeBits | ( | uint32 | bitString, |
int | numBits | ||
) |
Write numBits from bitString to the output stream. Bits are numbered from low to high.
Can only be called between beginBits and endBits. Bits written are semantically little-endian, regardless of the actual endian-ness of the system. That is, writeBits(0xABCD, 16)
writes 0xCD to the first byte and 0xAB to the second byte. However, if used with BinaryInput::readBits, the ordering is transparent to the caller.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Writes a signed 8-bit integer to the current position.
|
inline |
Write a string with NULL termination.
|
inline |
Write a string that always consumes len bytes, truncating or padding as necessary
void G3D::BinaryOutput::writeString | ( | const char * | s | ) |
void G3D::BinaryOutput::writeString32 | ( | const char * | s | ) |
|
inline |
Write a NULL-terminated string with a 32-bit length field in front of it. The NULL character is included in the length count.
|
inline |
Write a string, ensuring that the total length including NULL is even.
void G3D::BinaryOutput::writeStringEven | ( | const char * | s | ) |
void G3D::BinaryOutput::writeUInt16 | ( | uint16 | u | ) |
void G3D::BinaryOutput::writeUInt32 | ( | uint32 | u | ) |
void G3D::BinaryOutput::writeUInt64 | ( | uint64 | u | ) |
|
inline |
|
inline |
|
private |
Number of bytes already written to the file. Even on 32-bit OS, this can be 64-bits
|
private |
0 outside of beginBits...endBits, 1 inside
|
private |
Position (from the lowest bit) currently used in bitString.
|
private |
The current string of bits being built up by beginBits...endBits. This string is treated semantically, as if the lowest bit was on the left and the highest was on the right.
|
private |
|
private |
Size of the elements used
|
private |
|
private |
|
private |
|
private |
is this initialized?
|
private |
Underlying size of memory allocaded
|
private |
|
private |
Next byte in file
|
private |