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
00026
00027
00028
00029
00030
00031 #ifndef SALT_FILECLASSES_H
00032 #define SALT_FILECLASSES_H
00033
00034
00035
00036
00037
00038 #include <cstdio>
00039 #include <cstdlib>
00040
00041 namespace salt
00042 {
00043
00047 class RFile
00048 {
00049 public:
00050 virtual ~RFile() {};
00051
00052
00053
00057 virtual bool Open(const char*fn=NULL, char*mode="rb") = 0;
00058
00060 virtual void Close() = 0;
00061
00065 virtual void Destroy() = 0;
00066
00070 virtual int Eof() = 0;
00071
00073 virtual long Tell() = 0;
00074
00078 virtual int GetPos(long *pos) = 0;
00079
00086 virtual int Seek(long offset, int origin) = 0;
00087
00089 virtual void Rewind() = 0;
00090
00092 virtual long Size() = 0;
00093
00098 virtual char* Gets(char*buffer,int n) = 0;
00099
00103 virtual int Getc() = 0;
00104
00108 virtual void* GetHandle() = 0;
00109
00113 virtual size_t Read(void *buffer,size_t size,size_t count) = 0;
00114
00116 virtual size_t Read(void *buffer,size_t bytes) = 0;
00117
00119 int Igetw()
00120 {
00121 int b1, b2;
00122
00123 if ((b1 = Getc()) != EOF)
00124 if ((b2 = Getc()) != EOF)
00125 return ((b2 << 8) | b1);
00126 return EOF;
00127 }
00128
00130 long Igetl()
00131 {
00132 int b1, b2, b3, b4;
00133
00134 if ((b1 = Getc()) != EOF)
00135 if ((b2 = Getc()) != EOF)
00136 if ((b3 = Getc()) != EOF)
00137 if ((b4 = Getc()) != EOF)
00138 return (((long)b4<<24) | ((long)b3<<16) | ((long)b2<<8) | (long)b1);
00139 return EOF;
00140 }
00141
00143 int Mgetw()
00144 {
00145 int b1, b2;
00146
00147 if ((b1 = Getc()) != EOF)
00148 if ((b2 = Getc()) != EOF)
00149 return ((b1 << 8) | b2);
00150 return EOF;
00151 }
00152
00154 long Mgetl()
00155 {
00156 int b1, b2, b3, b4;
00157
00158 if ((b1 = Getc()) != EOF)
00159 if ((b2 = Getc()) != EOF)
00160 if ((b3 = Getc()) != EOF)
00161 if ((b4 = Getc()) != EOF)
00162 return (((long)b1<<24) | ((long)b2<<16) | ((long)b3<<8) | (long)b4);
00163 return EOF;
00164 }
00165 };
00166
00171 class MemFile : public RFile
00172 {
00173 public:
00174 MemFile(const char*fn=NULL, char*mode="rb");
00175 MemFile(FILE*f);
00176 MemFile(RFile *f);
00177 ~MemFile();
00178
00179 bool Open(const char*fn=NULL, char*mode="rb");
00180 bool Open(void*buffer, long s);
00181 void Close();
00182 void Destroy();
00183
00184 int Eof();
00185 long Tell();
00186 int GetPos(long *pos);
00187 int Seek(long offset, int origin);
00188 void Rewind();
00189 long Size();
00190
00191 char* Gets(char*buffer,int n);
00192 int Getc();
00193
00194 void* GetHandle() { return mHandle; }
00195
00196 size_t Read(void *buffer,size_t size,size_t count);
00197 size_t Read(void *buffer,size_t count) { return Read(buffer, 1,count); }
00198 private:
00199
00201 void* mHandle;
00202
00204 unsigned char* mCharHandle;
00205
00207 long mSize;
00208
00210 long mPosition;
00211 };
00212
00216 class WFile : public RFile
00217 {
00218 public:
00219 virtual ~WFile() {};
00220
00222 virtual int Puts(const char*s) = 0;
00223
00225 virtual int Putc(int c) = 0;
00226
00228 int Iputw(int w)
00229 {
00230 int b1, b2;
00231
00232 b1 = (w & 0xFF00) >> 8;
00233 b2 = w & 0x00FF;
00234
00235 if (Putc(b2)==b2)
00236 if (Putc(b1)==b1)
00237 return w;
00238 return EOF;
00239 }
00240
00242 virtual size_t Write(void *buffer,size_t size,size_t count) = 0;
00243
00245 virtual size_t Write(void *buffer,size_t count) = 0;
00246
00248 long Iputl(long l)
00249 {
00250 int b1, b2, b3, b4;
00251
00252 b1 = (int)((l & 0xFF000000L) >> 24);
00253 b2 = (int)((l & 0x00FF0000L) >> 16);
00254 b3 = (int)((l & 0x0000FF00L) >> 8);
00255 b4 = (int)l & 0x00FF;
00256
00257 if (Putc(b4)==b4)
00258 if (Putc(b3)==b3)
00259 if (Putc(b2)==b2)
00260 if (Putc(b1)==b1)
00261 return l;
00262 return EOF;
00263 }
00264
00266 int Mputw(int w)
00267 {
00268 int b1, b2;
00269
00270 b1 = (w & 0xFF00) >> 8;
00271 b2 = w & 0x00FF;
00272
00273 if (Putc(b1)==b1)
00274 if (Putc(b2)==b2)
00275 return w;
00276 return EOF;
00277 }
00278
00280 long Mputl(long l)
00281 {
00282 int b1, b2, b3, b4;
00283
00284 b1 = (int)((l & 0xFF000000L) >> 24);
00285 b2 = (int)((l & 0x00FF0000L) >> 16);
00286 b3 = (int)((l & 0x0000FF00L) >> 8);
00287 b4 = (int)l & 0x00FF;
00288
00289 if (Putc(b1)==b1)
00290 if (Putc(b2)==b2)
00291 if (Putc(b3)==b3)
00292 if (Putc(b4)==b4)
00293 return l;
00294 return EOF;
00295 }
00296 };
00297
00299 class StdFile : public WFile
00300 {
00301 public:
00302 StdFile(FILE*f);
00303 StdFile(const char*fn=NULL, char*mode="rb");
00304 virtual ~StdFile();
00305
00306 bool Open(const char*fn=NULL, char*mode="rb");
00307 void Close();
00308 void Destroy();
00309
00310 int Eof();
00311 long Tell();
00312 int GetPos(long *pos);
00313 int Seek(long offset, int origin);
00314 void Rewind();
00315 long Size();
00316
00317 char* Gets(char*buffer,int n);
00318 int Getc();
00319
00320 int Puts(const char*s);
00321 int Putc(int c);
00322
00323 size_t Read(void *buffer,size_t size,size_t count);
00324 size_t Read(void *buffer,size_t count) { return Read(buffer,1,count); }
00325
00326 size_t Write(void *buffer,size_t size,size_t count);
00327 size_t Write(void *buffer,size_t count) { return Write(buffer,1,count); }
00328
00329 void* GetHandle();
00330
00331 protected:
00333 FILE *mHandle;
00334 };
00335
00336 }
00337
00338 #endif //FILECLASSES_H__