ofBuffer
ofBuffer is a convenience class that provides easy methods for reading from and writing to files. It makes heavy use of the standard C++ ostream and istream classes, but also adds in easy ways to convert file data to strings, like:
ofBuffer buffer = ofBufferFromFile("someFile.txt"); // reading into the buffer
cout << buffer.getText(); // let's see what it says
You can also use the ofBufferFromFile() method to create a buffer from a file:
ofBuffer fileBuffer = ofBufferFromFile("someFile.txt");
allocate( ... )
void allocate(size_t size)Request that the buffer capacity be at least enough to contain a specified number of bytes.
Parameters:
size number of bytes to reserve space for
Allocate memory for the buffer to use. This sizes the char vector that the ofBuffer instance contains.
append( ... )
void append(const string &buffer)Append bytes to the end of buffer from a string.
Parameters:
buffer string to copy bytes from
append( ... )
void append(const char *buffer, size_t size)Append bytes to the end of the buffer from a raw byte pointer.
Warning: buffer must not be NULL
Warning: size must be <= the number of bytes allocated in buffer
Parameters:
buffer pointer to the raw byte buffer to copy data from
size the number of bytes to read
clear( )
void clear()Remove all bytes from the buffer, leaving a size of 0.
Clears all the data from the buffer.
getData( )
char * getData()Access the buffer's contents using a raw byte pointer.
Warning: Do not access bytes at indices beyond size()!
Returns: pointer to internal raw bytes
getData( )
const char * getData()access the buffer's contents using a const raw byte pointer.
Warning: Do not access bytes at indices beyond size()!
Returns: const pointer to internal raw bytes
getLines( )
ofBuffer::Lines getLines()Access the contents of the buffer as a series of text lines.
If the buffer loads a text file with lines separated by an endline char '\n', you can access each line individually using Line structs.
Returns: buffer text lines
getReverseLines( )
ofBuffer::RLines getReverseLines()Access the contents of the buffer as a series of text lines in reverse order
If the buffer loads a text file with lines separated by an endline char '\n' or '\r\n', you can access each line individually using Line structs.
Returns: buffer text lines
getText( )
string getText()get the contents of the buffer as a string.
Returns: buffer contents as a string
Return the buffer data as a string.
ofBuffer( ... )
ofBuffer(istream &stream, size_t ioBlockSize)Create a buffer and set its contents from an input stream.
Parameters:
ioBlockSize the number of bytes to read from the stream in chunks
Copy constructor, allows you to do:
ofBuffer aBuff;
// put some stuff in aBuff
ofBuffer bBuff(aBuff); // now it's in bBuff as well
ofBuffer( )
ofBuffer()Constructor.
ofBuffer( ... )
ofBuffer(const char *buffer, size_t size)Create a buffer and set its contents from a raw byte pointer.
Parameters:
buffer pointer to the raw byte buffer to copy data from
size the number of bytes to read
Warning: buffer must not be NULL
Warning: size must be <= the number of bytes allocated in buffer
Create a buffer with a character array.
string giantDataString;
ofBuffer buff(giantDataString.c_str(), giantDataString.size());
operator=( ... )
ofBuffer & operator=(const string &text)set contents of the buffer from a string
reserve( ... )
void reserve(size_t size)Request that the buffer capacity be at least enough to contain a specified number of bytes.
Parameters:
size number of bytes to reserve space for
resize( ... )
void resize(size_t size)set( ... )
bool set(istream &stream, size_t ioBlockSize)Set contents of the buffer from an input stream.
Parameters:
stream input stream to copy data from
ioBlockSize the number of bytes to read from the stream in chunks
Set the buffer from an istream.
set( ... )
void set(const string &text)Set contents of the buffer from a string.
Parameters:
text string to copy data from
set( ... )
void set(const char *buffer, size_t size)Set the contents of the buffer from a raw byte pointer.
Warning: buffer must not be NULL
Warning: size must be <= the number of bytes allocated in buffer
Parameters:
buffer pointer to the raw byte buffer to copy data from
size the number of bytes to read
Set the buffer from a string.
string giantDataString;
ofBuffer buff;
buff.set(giantDataString.c_str(), giantDataString.size());
setall( ... )
void setall(char mem)Set all bytes in the buffer to a given value.
Parameters:
mem byte value to set
size( )
size_t size()Check the buffer's size.
Returns: the size of the buffer's content in bytes
Get the size of the buffer data.
writeTo( ... )
bool writeTo(ostream &stream)Write contents of the buffer to an output stream.