00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "global.h"
00026 #ifdef HAVE_HDRSTOP
00027 #pragma hdrstop
00028 #endif
00029
00030 #include <stdexcept>
00031 #include "file.hpp"
00032
00033 using namespace std;
00034
00035 CFile::CFile(const char* filename,const char* mode):
00036 file(NULL)
00037 {
00038 file = fopen(filename,mode);
00039 if (file == NULL)
00040 throw runtime_error("Unable to open file");
00041 }
00042
00043 void CFile::Close()
00044 {
00045 if (file)
00046 fclose(file);
00047 file = NULL;
00048 }
00049
00050 void CFile::Seek(long offset,int whence) const
00051 {
00052 if ( fseek(file,offset,whence)!=0 )
00053 throw runtime_error("Unable to seek");
00054 }
00055
00056 long CFile::Tell() const
00057 {
00058 long pos = ftell(file);
00059 if ( pos == -1L )
00060 throw runtime_error("Seek error");
00061 return pos;
00062 }
00063
00064 char *CFile::ReadString() const
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 }
00088
00089
00090 char *CFile::ReadStringFlag(unsigned flag) const
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
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
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
00135 if (len == 0)
00136 {
00137 *p++ = 0;
00138 if (charBit)
00139 *p++ = 0;
00140 break;
00141 }
00142
00143
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 }
00157
00158 void CFile::RawRead(void *ptr,size_t size) const
00159 {
00160 if ( fread(ptr,1,size,file) != size )
00161 throw runtime_error("Unable to read from file");
00162 }
00163
00164 void CFile::RawWrite(const void *ptr,size_t size)
00165 {
00166 if ( fwrite(ptr,1,size,file) != size )
00167 throw runtime_error("Unable to write to file");
00168 }
00169
00170 CFile::~CFile()
00171 {
00172 Close();
00173 }