A data store that keeps data in memory up to a certain limit, and switches to a temporary file if the limit is exceeded. More...
#include <libppbase/base/MemFile.h>
Public Member Functions | |
MemFile (size_t limit=1048576) | |
Constructor. | |
~MemFile () | |
Destructor. | |
MemFile * | clone () const |
Clones a mem file. | |
size_t | getPos () const |
Returns the file position. | |
const char * | getSourceBuffer (size_t *rsize=NULL) const |
Returns the source memory buffer. | |
File * | getSourceFile () const |
Returns the source file. | |
String * | getSourceString () const |
Returns the source memory buffer of type string. | |
bool | isInMemory () const |
Checks if the data is in memory. | |
const char * | loadIntoMemory (size_t *rsize=NULL) |
Loads the data into memory, if it is not there already. | |
size_t | read (void *buf, size_t count, bool exact=false) |
Reads some bytes from the data. | |
size_t | read (String &buf, size_t count, bool exact=false) |
Reads some bytes from the data. | |
bool | readln (String &rbuf, bool strip=false) |
Reads a line of data. | |
File * | saveToFile () |
Saves the data to a file, if it is not there already. | |
void | seek (size_t pos) |
Sets the file positon. | |
void | seekBOF () |
Sets the file position to the beginning of the data. | |
void | seekEOF () |
Sets the file position to the end of the data. | |
void | setLimit (size_t limit) |
Sets the memory limit. | |
void | setSource (const MemFile *source) |
Sets the data source from another memfile object. | |
void | setSourceBuffer (const char *buf, size_t bufsize, bool own=false) |
Associates this object with a new data source. | |
void | setSourceFile (File *file, bool own=false) |
Associates this object with a new data source. | |
void | setSourceFile (const String &name, int flags=File::ofReadOnly) |
Associates this object with a new data source. | |
void | setSourceString (String *str, bool own=false) |
Associates this object with a new data source. | |
size_t | size () const |
Returns the file size. | |
void | truncate (size_t len) |
Truncates the data to a given size. | |
size_t | write (const void *buf, size_t count, bool exact=true) |
Writes some bytes to the file. | |
size_t | write (const String &buf, bool exact=true) |
Writes some bytes to the file. |
A data store that keeps data in memory up to a certain limit, and switches to a temporary file if the limit is exceeded.
base::MemFile::MemFile | ( | size_t | limit = 1048576 |
) |
Constructor.
limit | memory limit in bytes |
MemFile* base::MemFile::clone | ( | ) | const |
Clones a mem file.
The cloned mem file starts at position 0. If this mem file owns the underlying data source, it generates a clone of the data source too. If not, the original data source is just passed.
size_t base::MemFile::getPos | ( | ) | const |
Returns the file position.
const char* base::MemFile::getSourceBuffer | ( | size_t * | rsize = NULL |
) | const |
Returns the source memory buffer.
rsize | returns the data size in bytes |
File* base::MemFile::getSourceFile | ( | ) | const |
Returns the source file.
String* base::MemFile::getSourceString | ( | ) | const |
Returns the source memory buffer of type string.
bool base::MemFile::isInMemory | ( | ) | const |
Checks if the data is in memory.
const char* base::MemFile::loadIntoMemory | ( | size_t * | rsize = NULL |
) |
Loads the data into memory, if it is not there already.
rsize | returns the data size in bytes |
size_t base::MemFile::read | ( | void * | buf, | |
size_t | count, | |||
bool | exact = false | |||
) |
Reads some bytes from the data.
buf | buffer to read bytes into | |
count | number of bytes to read | |
exact | flag: read exactly this many bytes |
size_t base::MemFile::read | ( | String & | buf, | |
size_t | count, | |||
bool | exact = false | |||
) |
Reads some bytes from the data.
buf | buffer to read bytes into | |
count | number of bytes to read | |
exact | flag: read exactly this many bytes |
bool base::MemFile::readln | ( | String & | rbuf, | |
bool | strip = false | |||
) |
Reads a line of data.
rbuf | buffer to read the line into | |
strip | flag: strip CR/LF from the end of the line |
File* base::MemFile::saveToFile | ( | ) |
Saves the data to a file, if it is not there already.
void base::MemFile::seek | ( | size_t | pos | ) |
Sets the file positon.
pos | byte offset of new file position |
void base::MemFile::setLimit | ( | size_t | limit | ) |
Sets the memory limit.
limit | new memory limit in bytes |
void base::MemFile::setSource | ( | const MemFile * | source | ) |
Sets the data source from another memfile object.
source | the data source |
void base::MemFile::setSourceBuffer | ( | const char * | buf, | |
size_t | bufsize, | |||
bool | own = false | |||
) |
Associates this object with a new data source.
buf | the source buffer | |
bufsize | the number of bytes in the buffer | |
own | true if this object owns the buffer (=frees it when it is destroyed) |
void base::MemFile::setSourceFile | ( | File * | file, | |
bool | own = false | |||
) |
Associates this object with a new data source.
file | an open file object | |
own | true if this object owns the file (=deletes it when it is destroyed) |
void base::MemFile::setSourceFile | ( | const String & | name, | |
int | flags = File::ofReadOnly | |||
) |
Associates this object with a new data source.
name | the file name | |
flags | open flags |
void base::MemFile::setSourceString | ( | String * | str, | |
bool | own = false | |||
) |
Associates this object with a new data source.
str | the source buffer | |
own | true if this object owns the string (=frees it when it is destroyed) |
size_t base::MemFile::size | ( | ) | const |
Returns the file size.
Reimplemented in base_http::HTTPPart, and base_http::HTTPRequest.
void base::MemFile::truncate | ( | size_t | len | ) |
Truncates the data to a given size.
len | the size to truncate the data to |
size_t base::MemFile::write | ( | const void * | buf, | |
size_t | count, | |||
bool | exact = true | |||
) |
Writes some bytes to the file.
buf | bytes to write | |
count | number of bytes to write | |
exact | flag: write exactly this many bytes, throw an error if they cannot be written |
size_t base::MemFile::write | ( | const String & | buf, | |
bool | exact = true | |||
) |
Writes some bytes to the file.
buf | bytes to write | |
exact | flag: write exactly this many bytes, throw an error if they cannot be written |