Introduction
CxFile
is a C++ class to manage disc and memory files within the
same object. It uses the standard C run-time functions (no Windows APIs or MFC
classes), so it is very simple to understand how it works. It's also very simple
to add new storage types.
Principle of function
CxFile
has a protected member variable, void *m_stream
, it can be a FILE*
or a BYTE*
. The gender of the pointer
is stored in a m_StorageType
variable, assigned only once in the
lifetime of the object.
The structure of a generic method is:
long CxFile::GenericMethod()
{
if (m_stream==NULL) return -1;
switch(m_StorageType){
case 0:
case 1:
}
return -1;
}
For disc file handling, CxFile
acts as a wrapper for the stream
I/O routines. For memory file handling, some additional variables are required:
m_Position
, m_Size
, m_Edge
; rispectively
the position of the file pointer, the size of the file, the size of the memory
buffer. Each member must take care of these variables to ensure the reliability
of the data, the Read
and Write
methods are the most
critical for this aspect.
Constructors
The class offers 3 constructors.
CxFile(long type)
initializes the object for disc files (type=0)
or memory files (type=1), m_stream
is NULL
, so you must call
Open
to create the file.
CxFile(FILE* pFile)
attaches the object to an open file.
CxFile(BYTE* pBuffer, DWORD size)
attaches the object to an
existing memory buffer.
The main difference between the first and the other two constructors, is that
the first one will close/free m_stream
in the destructor, while the
others doesn't release m_stream
, even if you call explicitly Close()
.
Member Functions
void* Open(const char *filename, const char *mode)
long Close()
size_t Read(void *buffer, size_t size, size_t count)
size_t Write(const void *buffer, size_t size, size_t count)
long Flush()
long Seek(long offset, int origin)
long Tell()
long Size()
long GetPos(fpos_t *pos)
long Eof()
long Error()
long PutC(unsigned char c);
long GetC();
void* GetStream()
void* Alloc(DWORD nBytes)
void Free();
void Transfer(CxFile &from)