Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   Namespace Members   Compound Members  

PLMMemFile Class Reference

A simple class for memory-file manipulation. More...

#include <PLMMemFile.hpp>

Inheritance diagram for PLMMemFile:

Inheritance graph
[legend]
List of all members.

Public Types

enum  PRFOpenMode {
  PRF_ERROR = 0, PRF_READ = 1, PRF_WRITE = 2, PRF_RW = 3,
  PRF_TRUNC = 4, PRF_CREATE = 8, PRF_APPEND = 16
}
enum  PRFState {
  ERR_CLOSED = 0x10, ERR_MODE = 0x20, ERR_OPEN = 0x30, ERR_READ = 0x07,
  ERR_WRITE = 0x05, ERR_SEEK = 0x03, ERR_OK = 0x01
}
enum  PRFRef { PRF_START = SEEK_SET, PRF_CUR = SEEK_CUR, PRF_END = SEEK_END }

Public Methods

 PLMMemFile (u32 size, u32 mode=PLMResFile::PRF_READ, u8 *address=NULL, bool own=true, const char *name=NULL)
bool Open (u32 size, u32 mode=PLMResFile::PRF_READ, u8 *address=NULL, bool own=true, const char *name=NULL)
u32 Size () const
virtual void Close ()
virtual bool Seek (int offset, int reference=PLMResFile::PRF_START)
virtual void SeekStart ()
virtual void SeekEnd ()
virtual u32 GetPos () const
virtual void Flush ()
virtual u32 Read (void *dest, u32 size)
virtual u32 Write (const void *src, u32 size)
virtual u32 GetLine (char *dest, u32 size, char sep= '\n')
virtual u32 GetLine (PLMString &dest, u32 maxsize=PLM::INDEX_ERROR, char sep= '\n')
virtual void PrintInfo () const
const char * Name () const
u32 Mode () const
bool Good () const
virtual bool Eof () const
u32 State () const
virtual u32 WriteStr (const PLMString &src, u32 size=PLM::INDEX_ERROR)
u8 Read8 ()
u16 Read16 ()
u32 Read32 ()
u16 ReadBE16 ()
u32 ReadBE32 ()
float ReadFl ()
double ReadDbl ()
bool Write8 (u8 c)
bool Write16 (u16 s)
bool Write32 (u32 i)
bool WriteBE16 (u16 s)
bool WriteBE32 (u32 i)
bool WriteFl (float f)
bool WriteDbl (double d)

Detailed Description

A simple class for memory-file manipulation.

This class allows to perform standard file operations on a chunk of memory. Operations include read and write in various formats, non-string versions perform automatic byte-ordering conversion (file is always assumed to be in little endian order). You can also seek a specific position in the file, the beginning and the end.


Member Enumeration Documentation

enum PLMResFile::PRFOpenMode [inherited]
 

File opening modes, you can add TRUNC and others with '|' (bitwise-or).

Enumeration values:
PRF_ERROR  error (can be set to this one after a bad operation)
PRF_READ  read only mode (Write*() not allowed)
PRF_WRITE  write only mode (Read*() not allowed), file is truncated if it already exists
PRF_RW  read + write mode
PRF_TRUNC  (write modes only), remove content if the file already exists
PRF_CREATE  (write modes only), force creation = return false if the file already exists
PRF_APPEND  (write modes only), file is not truncated and cursor is moved at the end of the file

enum PLMResFile::PRFState [inherited]
 

Object internal state.

Enumeration values:
ERR_CLOSED  (closed) the file is closed or not yet opened
ERR_MODE  (closed) wrong opening mode given to the constructor
ERR_OPEN  (closed) error while opening a file
ERR_READ  (open) a read error has occured
ERR_WRITE  (open) a write error has occured
ERR_SEEK  (open) error in seeking methods
ERR_OK  (open) all is ok, the file is open

enum PLMResFile::PRFRef [inherited]
 

References for Seek().

Enumeration values:
PRF_START  offset is counted from the beginning of the file
PRF_CUR  offset is added to current position
PRF_END  offset is counted from the end of the file (should be negative or NULL)


Constructor & Destructor Documentation

PLMMemFile::PLMMemFile u32    size,
u32    mode = PLMResFile::PRF_READ,
u8 *    address = NULL,
bool    own = true,
const char *    name = NULL
 

Create a memory file of size bytes, in mode mode.

If address is NULL, the buffer is allocated, otherwise address is used and will be deleted when the object is destroyed if own is true.

Parameters:
size  the size of the buffer in bytes
mode  the opening mode (see PLMResFile::PRFOpenMode for a description), PRF_APPEND is ignored, PRF_CREATE and PRF_TRUNC mean to initialise the buffer with 0s (write mode only).
address  a pointer on a pre-allocated buffer to use, or NULL to let the object allocate memory itself (own is then ignored, set to true)
own  tell if the pre-allocated buffer pointed to by address is owned by the object (i.e. will be deleted on Close()). Ignored if address is NULL (set to true).
name  a filename to associate with this object (if NULL, default = "Memory file") Use Good() to check the object state before any use.


Member Function Documentation

bool PLMMemFile::Open u32    size,
u32    mode = PLMResFile::PRF_READ,
u8 *    address = NULL,
bool    own = true,
const char *    name = NULL
 

Try to open a new memory file, size bytes long, with specified mode.

The current file is closed first, if any was open, even if Open() fails. See the constructor PLMMemFile() for details on the parameters.

Returns:
true on success, false on error (can not allocate memory).

u32 PLMMemFile::Size   [inline]
 

Get current size (allocated memory).

virtual void PLMMemFile::Close   [virtual]
 

Close the file, set state to ERR_CLOSED. No method will work until Open() is called.

Reimplemented from PLMResFile.

virtual bool PLMMemFile::Seek int    offset,
int    reference = PLMResFile::PRF_START
[virtual]
 

Move the cursor to a specific position inside the file.

Parameters:
offset  number of bytes to move (can be < 0 to go back)
reference  reference to compute the final position, use PLMResFile::PRFRef enum.
Returns:
true on success, false on error and the position is not modified.

Implements PLMResFile.

virtual void PLMMemFile::SeekStart   [virtual]
 

Move the cursor to the beginning of the file (starting offset).

Implements PLMResFile.

virtual void PLMMemFile::SeekEnd   [virtual]
 

Move the cursor to the end of the file (ending offset).

Implements PLMResFile.

virtual u32 PLMMemFile::GetPos   [virtual]
 

Get current cursor position (offset from the start).

Implements PLMResFile.

virtual void PLMMemFile::Flush   [inline, virtual]
 

Update version on disk with data in the buffer.

Implements PLMResFile.

virtual u32 PLMMemFile::Read void *    dest,
u32    size
[virtual]
 

Read data from the file.

Try to read size bytes and write them to dest.

Returns:
the number of bytes read, 0 means error or end of file.

Implements PLMResFile.

virtual u32 PLMMemFile::Write const void *    src,
u32    size
[virtual]
 

Write data to the file.

Try to write size bytes read from src.

Returns:
the number of bytes written, 0 means error or end of file.

Implements PLMResFile.

virtual u32 PLMMemFile::GetLine char *    dest,
u32    size,
char    sep = '\n'
[virtual]
 

Read characters from a text file until sep is found or size-1 characters are read, and write them to dest.

Note that sep is replaced by a final '\0' in dest.

Returns:
  • 0 = error, can not read source,
  • 1 = empty line (sep is the first character encountered),
  • n = number of characters written (including \0).

Implements PLMResFile.

virtual u32 PLMMemFile::GetLine PLMString   dest,
u32    maxsize = PLM::INDEX_ERROR,
char    sep = '\n'
[virtual]
 

Read characters from a text file until sep is found or maxsize characters are read, and write them to dest (which is cleared).

The file cursor then points just after 'sep'. If maxsize = PLM::INDEX_ERROR, read until sep is found (or memory is full).

Returns:
  • 0 = error, can not read source,
  • 1 = empty line (sep is the first character encountered),
  • n = number of characters read (may be different from written = dest.Length()).

Implements PLMResFile.

virtual void PLMMemFile::PrintInfo   [virtual]
 

Print some debug info.

Implements PLMResFile.

const char * PLMResFile::Name   [inline, inherited]
 

Get the current file name (the name used to open it).

u32 PLMResFile::Mode   [inline, inherited]
 

Returns current opening mode (see PLMResFile::PRFOpenMode enum).

bool PLMResFile::Good   [inline, inherited]
 

Returns current state of the file. False means you can not perform any operation.

bool PLMResFile::Eof   [inline, virtual, inherited]
 

Check if the file cursor is at the end.

Reimplemented in PLMFile.

u32 PLMResFile::State   [inline, inherited]
 

Get the current state of the object. See PLMResFile::PRFState enum.

virtual u32 PLMResFile::WriteStr const PLMString   src,
u32    size = PLM::INDEX_ERROR
[virtual, inherited]
 

Write a string to the file.

Try to write size characters read from src. Use size = PLM::INDEX_ERROR to write the whole string.

Returns:
the number of bytes written, 0 means error or end of file.

u8 PLMResFile::Read8   [inherited]
 

Read one byte and move the cursor (+1 byte).

u16 PLMResFile::Read16   [inherited]
 

Read one small integer and move the cursor (+2 bytes).

Data is read in little endian and converted if necessary.

u32 PLMResFile::Read32   [inherited]
 

Read one integer and move the cursor (+4 bytes).

Data is read in little endian and converted if necessary.

u16 PLMResFile::ReadBE16   [inherited]
 

Read one small integer and move the cursor (+2 bytes).

Data is read in big endian and converted if necessary.

u32 PLMResFile::ReadBE32   [inherited]
 

Read one integer and move the cursor (+4 bytes).

Data is read in big endian and converted if necessary.

float PLMResFile::ReadFl   [inherited]
 

Read one float and move the cursor (+4 bytes).

Data is read in little endian and converted if necessary.

double PLMResFile::ReadDbl   [inherited]
 

Read one double and move the cursor (+8 bytes).

Data is read in little endian and converted if necessary.

bool PLMResFile::Write8 u8    c [inherited]
 

Write one byte and move the cursor (+1 byte), returns false on error (bad state).

bool PLMResFile::Write16 u16    s [inherited]
 

Write one small integer and move the cursor (+2 bytes), returns false on error (bad state).

Data is written in little endian and converted if necessary.

bool PLMResFile::Write32 u32    i [inherited]
 

Write one integer and move the cursor (+4 bytes), returns false on error (bad state).

Data is written in little endian and converted if necessary.

bool PLMResFile::WriteBE16 u16    s [inherited]
 

Write one small integer and move the cursor (+2 bytes), returns false on error (bad state).

Data is written in big endian and converted if necessary.

bool PLMResFile::WriteBE32 u32    i [inherited]
 

Write one integer and move the cursor (+4 bytes), returns false on error (bad state).

Data is written in big endian and converted if necessary.

bool PLMResFile::WriteFl float    f [inherited]
 

Write one float and move the cursor (+4 bytes), returns false on error (bad state).

Data is written in little endian and converted if necessary.

bool PLMResFile::WriteDbl double    d [inherited]
 

Write one double and move the cursor (+8 bytes), returns false on error (bad state).

Data is written in little endian and converted if necessary.


The documentation for this class was generated from the following file:
  • PLMMemFile.hpp

Generated by Doxygen 1.2.18 on Tue Oct 26 12:07:44 2004.