Make your own free website on Tripod.com
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

CFile Class Reference

#include <file.hpp>

Inheritance diagram for CFile:

ICFile CSignFile List of all members.

Public Methods

 CFile (const char *filename, const char *mode)
 ~CFile ()
void Seek (long offset, int whence=SEEK_SET) const
char * ReadString () const
char * ReadStringFlag (unsigned flag) const
long Tell () const
void RawRead (void *ptr, size_t size) const
void RawWrite (const void *ptr, size_t size)
FILE * GetFile ()

Private Methods

void Close ()

Private Attributes

FILE * file

Constructor & Destructor Documentation

CFile::CFile const char *    filename,
const char *    mode
 

Definition at line 35 of file file.cpp.

References file.

00035                                                  :
00036   file(NULL)
00037 {
00038   file = fopen(filename,mode);
00039   if (file == NULL)
00040     throw runtime_error("Unable to open file");
00041 }

CFile::~CFile  
 

Definition at line 170 of file file.cpp.

References Close().

00171 {
00172   Close();
00173 }


Member Function Documentation

void CFile::Close   [private]
 

Definition at line 43 of file file.cpp.

References file.

Referenced by ~CFile().

00044 {
00045   if (file)
00046     fclose(file);
00047   file = NULL;
00048 }

FILE* CFile::GetFile   [inline]
 

Definition at line 64 of file file.hpp.

References file.

Referenced by OutputImport().

00064 { return file; };

void CFile::RawRead void *    ptr,
size_t    size
const [virtual]
 

Implements ICFile.

Reimplemented in CSignFile.

Definition at line 158 of file file.cpp.

References file.

Referenced by ObjectModule::ObjectModule(), CSignFile::RawRead(), and ReadStringFlag().

00159 {
00160   if ( fread(ptr,1,size,file) != size )
00161     throw runtime_error("Unable to read from file");
00162 }

void CFile::RawWrite const void *    ptr,
size_t    size
[virtual]
 

Implements ICFile.

Definition at line 164 of file file.cpp.

References file.

Referenced by WriteIcons().

00165 {
00166   if ( fwrite(ptr,1,size,file) != size )
00167     throw runtime_error("Unable to write to file");
00168 }

char * CFile::ReadString   const [virtual]
 

Implements ICFile.

Reimplemented in CSignFile.

Definition at line 64 of file file.cpp.

References file.

Referenced by CSignFile::ReadString().

00065 {
00066   char *pbegin = new char[16];
00067   char *p      = pbegin;
00068   char *pend   = pbegin + 16;
00069   int c;
00070   for (;;)
00071   {
00072     c = fgetc(file);
00073     if (c==EOF)
00074       throw runtime_error("Error reading string");
00075     if (p==pend) {
00076       int size = (pend-pbegin);
00077       pend = new char[size+16];
00078       memcpy(pend,pbegin,size*sizeof(char));
00079       delete [] pbegin;
00080       pbegin = pend;
00081       pend = (p = pbegin + size) + 16;
00082     }
00083     *p++ = (char)c;
00084     if (c==0)
00085       return pbegin;
00086   }
00087 }

char * CFile::ReadStringFlag unsigned    flag const [virtual]
 

Implements ICFile.

Reimplemented in CSignFile.

Definition at line 90 of file file.cpp.

References file, RawRead(), ICFile::strLen16, ICFile::strLen32, ICFile::strLen8, ICFile::strLenMask, and ICFile::strUcs2.

Referenced by CSignFile::ReadStringFlag().

00091 {
00092   unsigned char charBit = flag & strUcs2 ? 1:0;
00093   char *pbegin = new char[16<<charBit];
00094   char *p      = pbegin;
00095   char *pend   = pbegin + (16<<charBit);
00096   int c,cc;
00097   unsigned len = ~0u;
00098 
00099   uint32_t ui32;
00100   uint16_t ui16;
00101   uint8_t ui8;
00102 
00103   switch (flag & strLenMask)
00104   {
00105   case strLen8:  RawRead(&ui8, sizeof(ui8) ); len = ui8;  break;
00106   case strLen16: RawRead(&ui16,sizeof(ui16)); len = ui16; break;
00107   case strLen32: RawRead(&ui32,sizeof(ui32)); len = ui32; break;
00108   }
00109 
00110   cc = 0;
00111   for (;;--len)
00112   {
00113     // read char
00114     c = fgetc(file);
00115     if (c==EOF)
00116       throw runtime_error("Error reading string");
00117     if (charBit)
00118     {
00119       cc = fgetc(file);
00120       if (cc==EOF)
00121         throw runtime_error("Error reading string");
00122     }
00123 
00124     // extend buffer if needed
00125     if (p==pend) {
00126       int size = (pend-pbegin);
00127       pend = new char[size+(16<<charBit)];
00128       memcpy(pend,pbegin,size*sizeof(char));
00129       delete [] pbegin;
00130       pbegin = pend;
00131       pend = (p = pbegin + size) + (16<<charBit);
00132     }
00133 
00134     // check end string
00135     if (len == 0)
00136     {
00137       *p++ = 0;
00138       if (charBit)
00139         *p++ = 0;
00140       break;
00141     }
00142 
00143     // write char
00144     *p++ = (char)c;
00145     if (charBit)
00146       *p++ = (char)cc;
00147 
00148     if ( (flag&strLenMask) == 0 )
00149     {
00150       if ( c==0 && cc==0 )
00151         break;
00152     }
00153   }
00154 
00155   return pbegin;
00156 }

void CFile::Seek long    offset,
int    whence = SEEK_SET
const [virtual]
 

Implements ICFile.

Definition at line 50 of file file.cpp.

References file.

Referenced by MarkImport(), ObjectModule::ObjectModule(), and RawDump().

00051 {
00052   if ( fseek(file,offset,whence)!=0 )
00053     throw runtime_error("Unable to seek");
00054 }

long CFile::Tell   const [virtual]
 

Implements ICFile.

Definition at line 56 of file file.cpp.

References file.

Referenced by RawDump(), CSignFile::RawRead(), CSignFile::ReadString(), and CSignFile::ReadStringFlag().

00057 {
00058   long pos = ftell(file);
00059   if ( pos ==  -1L )
00060     throw runtime_error("Seek error");
00061   return pos;
00062 }


Member Data Documentation

FILE* CFile::file [private]
 

Definition at line 67 of file file.hpp.

Referenced by CFile(), Close(), GetFile(), RawRead(), RawWrite(), ReadString(), ReadStringFlag(), Seek(), and Tell().


The documentation for this class was generated from the following files:
Generated on Mon Jan 13 22:20:36 2003 for perdr by doxygen1.2.15