00001
00002 #ifndef INCLUDED_LoadSave_h
00003 #define INCLUDED_LoadSave_h
00004
00005 #include <string>
00006 #include <sys/param.h>
00007
00008 #ifndef LOADSAVE_SWAPBYTES
00009
00010 #ifdef BYTE_ORDER
00011 # if BYTE_ORDER == BIG_ENDIAN
00012 # define LOADSAVE_SWAPBYTES 1
00013 # elif BYTE_ORDER == LITTLE_ENDIAN
00014 # define LOADSAVE_SWAPBYTES 0
00015 # else
00016 # warning unknown byte ordering for current platform, assuming no swap
00017 # define LOADSAVE_SWAPBYTES 0
00018 # endif
00019 #else
00020
00021
00022 # ifdef PLATFORM_APERIOS
00023
00024 # define LOADSAVE_SWAPBYTES 0
00025 # else
00026 # warning unknown byte ordering for current platform, assuming no swap
00027 # define LOADSAVE_SWAPBYTES 0
00028 # endif
00029 #endif
00030
00031 #endif
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 class LoadSave {
00134 public:
00135
00136 static const unsigned int stringpad=sizeof(unsigned int)+1;
00137
00138
00139
00140 LoadSave() {}
00141 virtual ~LoadSave();
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 virtual unsigned int getBinSize() const =0;
00152
00153
00154
00155
00156 virtual unsigned int LoadBuffer(const char buf[], unsigned int len)=0;
00157
00158
00159
00160
00161 virtual unsigned int SaveBuffer(char buf[], unsigned int len) const =0;
00162
00163
00164
00165
00166
00167
00168 virtual unsigned int LoadFile(const char* filename);
00169 virtual unsigned int SaveFile(const char* filename) const;
00170
00171
00172
00173
00174
00175
00176
00177
00178 virtual unsigned int LoadFileStream(FILE* f);
00179
00180
00181
00182
00183
00184
00185
00186 virtual unsigned int SaveFileStream(FILE* f) const;
00187
00188
00189
00190
00191
00192
00193
00194
00195 static bool ChkAdvance(int res, const char** buf, unsigned int* len, const char* msg);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 static bool ChkAdvance(int res, const char** buf, unsigned int* len, const char* msg, int arg1);
00207
00208
00209
00210
00211
00212
00213
00214 virtual unsigned int creatorSize(const char creator[]) const { return strlen(creator)+stringpad; }
00215
00216
00217
00218
00219
00220
00221 virtual unsigned int checkCreator(const char* creator, const char buf[], unsigned int len, bool isLoading=true) const;
00222
00223
00224
00225
00226
00227 virtual unsigned int checkCreator(const char* creator, FILE* f, bool isLoading=true) const;
00228
00229
00230
00231
00232
00233 virtual unsigned int saveCreator(const char* creator, char buf[], unsigned int len) const;
00234
00235
00236
00237
00238 virtual unsigned int saveCreator(const char* creator, FILE* f) const;
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 inline static unsigned int encode(const LoadSave& x, char buf[], unsigned int cap) { return x.SaveBuffer(buf,cap); }
00252 inline static unsigned int decode(LoadSave& x, const char buf[], unsigned int cap) { return x.LoadBuffer(buf,cap); }
00253 inline static unsigned int encode(const LoadSave& x, FILE* f) { return x.SaveFileStream(f); }
00254 inline static unsigned int decode(LoadSave& x, FILE* f) { return x.LoadFileStream(f); }
00255
00256 #if LOADSAVE_SWAPBYTES
00257
00258 inline static unsigned int encode(const double x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(double*)buf,x); return sizeof(x); }
00259 inline static unsigned int decode(double& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const double*)buf); return sizeof(x);}
00260 inline static unsigned int encode(const double x, FILE* f) { double t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00261 inline static unsigned int decode(double& x, FILE* f) { double t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00262
00263 inline static unsigned int encode(const float x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(float*)buf,x); return sizeof(x); }
00264 inline static unsigned int decode(float& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const float*)buf); return sizeof(x);}
00265 inline static unsigned int encode(const float x, FILE* f) { float t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00266 inline static unsigned int decode(float& x, FILE* f) { float t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00267
00268 inline static unsigned int encode(const long x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(long*)buf,x); return sizeof(x); }
00269 inline static unsigned int decode(long& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const long*)buf); return sizeof(x);}
00270 inline static unsigned int encode(const long x, FILE* f) { long t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00271 inline static unsigned int decode(long& x, FILE* f) { long t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00272 inline static unsigned int encode(const unsigned long x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(unsigned long*)buf,x); return sizeof(x); }
00273 inline static unsigned int decode(unsigned long& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const unsigned long*)buf); return sizeof(x);}
00274 inline static unsigned int encode(const unsigned long x, FILE* f) { unsigned long t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00275 inline static unsigned int decode(unsigned long& x, FILE* f) { unsigned long t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00276 inline static unsigned int encode(const int x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(int*)buf,x); return sizeof(x); }
00277 inline static unsigned int decode(int& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const int*)buf); return sizeof(x);}
00278 inline static unsigned int encode(const int x, FILE* f) { int t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00279 inline static unsigned int decode(int& x, FILE* f) { int t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00280 inline static unsigned int encode(const unsigned int x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(unsigned int*)buf,x); return sizeof(x); }
00281 inline static unsigned int decode(unsigned int& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const unsigned int*)buf); return sizeof(x);}
00282 inline static unsigned int encode(const unsigned int x, FILE* f) { unsigned int t=0; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00283 inline static unsigned int decode(unsigned int& x, FILE* f) { unsigned int t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00284
00285 inline static unsigned int encode(const short x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(short*)buf,x); return sizeof(x); }
00286 inline static unsigned int decode(short& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const short*)buf); return sizeof(x);}
00287 inline static unsigned int encode(const short x, FILE* f) { short t; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00288 inline static unsigned int decode(short& x, FILE* f) { short t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00289 inline static unsigned int encode(const unsigned short x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(*(unsigned short*)buf,x); return sizeof(x); }
00290 inline static unsigned int decode(unsigned short& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; byteswap(x,*(const unsigned short*)buf); return sizeof(x);}
00291 inline static unsigned int encode(const unsigned short x, FILE* f) { unsigned short t; byteswap(t,x); return sizeof(x)*fwrite(&t,sizeof(x),1,f); }
00292 inline static unsigned int decode(unsigned short& x, FILE* f) { unsigned short t=0; if(fread(&t,sizeof(x),1,f)==0) return 0; byteswap(x,t); return sizeof(x);}
00293
00294 #else
00295
00296 inline static unsigned int encode(const double x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00297 inline static unsigned int decode(double& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00298 inline static unsigned int encode(const double x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00299 inline static unsigned int decode(double& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00300
00301 inline static unsigned int encode(const float x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00302 inline static unsigned int decode(float& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00303 inline static unsigned int encode(const float x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00304 inline static unsigned int decode(float& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00305
00306 inline static unsigned int encode(const long x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00307 inline static unsigned int decode(long& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00308 inline static unsigned int encode(const long x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00309 inline static unsigned int decode(long& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00310 inline static unsigned int encode(const unsigned long x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00311 inline static unsigned int decode(unsigned long& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00312 inline static unsigned int encode(const unsigned long x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00313 inline static unsigned int decode(unsigned long& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00314 inline static unsigned int encode(const int x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00315 inline static unsigned int decode(int& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00316 inline static unsigned int encode(const int x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00317 inline static unsigned int decode(int& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00318 inline static unsigned int encode(const unsigned int x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00319 inline static unsigned int decode(unsigned int& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00320 inline static unsigned int encode(const unsigned int x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00321 inline static unsigned int decode(unsigned int& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00322
00323 inline static unsigned int encode(const short x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00324 inline static unsigned int decode(short& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00325 inline static unsigned int encode(const short x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00326 inline static unsigned int decode(short& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00327 inline static unsigned int encode(const unsigned short x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; memcpy(buf,&x,sizeof(x)); return sizeof(x); }
00328 inline static unsigned int decode(unsigned short& x, const char buf[], unsigned int cap){ if(cap<sizeof(x)) return 0; memcpy(&x,buf,sizeof(x)); return sizeof(x); }
00329 inline static unsigned int encode(const unsigned short x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00330 inline static unsigned int decode(unsigned short& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00331
00332 #endif //end of big/little endian differences
00333
00334 inline static unsigned int encode(const std::string& x, char buf[], unsigned int cap) { if(cap<sizeof(unsigned int)+x.size()+1) return 0; memcpy(buf+encode(x.size(),buf,cap),x.c_str(),x.size()+1); return x.size()+stringpad; }
00335 inline static unsigned int decode(std::string& x, const char buf[], unsigned int cap) { if(cap<sizeof(unsigned int)) return 0; unsigned int sz=0; decode(sz,buf,cap); if(cap<sizeof(unsigned int)+sz+1) return 0; x.assign(buf+sizeof(unsigned int),sz); return x.size()+stringpad; }
00336 inline static unsigned int encode(const std::string& x, FILE* f) { encode(x.size(),f); return sizeof(unsigned int)+fwrite(x.c_str(),1,sizeof(x)+1,f); }
00337 inline static unsigned int decode(std::string& x, FILE* f) { unsigned int sz=0; decode(sz,f); char *tmp=new char[sz+1]; if(fread(tmp,1,sz+1,f)!=sz+1) { delete [] tmp; return 0; } x.assign(tmp,sz); delete [] tmp; return sz+stringpad; }
00338
00339 inline static unsigned int encode(const char* x, char buf[], unsigned int cap) { unsigned int sz=strlen(x); if(cap<sizeof(unsigned int)+sz+1) return 0; memcpy(buf+encode(sz,buf,cap),x,sz+1); return sz+stringpad; }
00340 inline static unsigned int decode(char*& x, const char buf[], unsigned int cap) { if(cap<sizeof(unsigned int)) return 0; unsigned int sz=0; decode(sz,buf,cap); if(cap<sizeof(unsigned int)+sz+1) return 0; x=new char[sz+1]; strncpy(x,buf+sizeof(unsigned int),sz+1); return sz+stringpad; }
00341 inline static unsigned int encode(const char* x, FILE* f) { unsigned int sz=strlen(x); encode(sz,f); return sizeof(unsigned int)+fwrite(x,1,sz+1,f); }
00342 inline static unsigned int decode(char*& x, FILE* f) { unsigned int sz=0; decode(sz,f); x=new char[sz+1]; if(fread(x,1,sz+1,f)!=sz+1) { delete [] x; x=NULL; return 0; } return sz+stringpad; }
00343
00344 inline static unsigned int encode(const char x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; buf[0]=x; return sizeof(x); }
00345 inline static unsigned int decode(char& x, const char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; x=buf[0]; return sizeof(x);}
00346 inline static unsigned int encode(const char x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00347 inline static unsigned int decode(char& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00348 inline static unsigned int encode(const unsigned char x, char buf[], unsigned int cap) { if(cap<sizeof(x)) return 0; buf[0]=(char)x; return sizeof(x); }
00349 inline static unsigned int decode(unsigned char& x, const char buf[], unsigned int cap){ if(cap<sizeof(x)) return 0; x=(unsigned char)buf[0]; return sizeof(x);}
00350 inline static unsigned int encode(const unsigned char x, FILE* f) { return sizeof(x)*fwrite(&x,sizeof(x),1,f); }
00351 inline static unsigned int decode(unsigned char& x, FILE* f) { return sizeof(x)*fread(&x,sizeof(x),1,f); }
00352
00353 inline static unsigned int encode(const bool x, char buf[], unsigned int cap) { if(cap<sizeof(char)) return 0; buf[0]=(char)(x?1:0); return sizeof(char); }
00354 inline static unsigned int decode(bool& x, const char buf[], unsigned int cap) { if(cap<sizeof(char)) return 0; x=(buf[0]!=(char)0); return sizeof(char);}
00355 inline static unsigned int encode(const bool x, FILE* f) { char t=(char)(x?1:0); return sizeof(char)*fwrite(&t,sizeof(char),1,f); }
00356 inline static unsigned int decode(bool& x, FILE* f) { char t='\0'; fread(&t,sizeof(char),1,f); x=(t!=(char)0); return sizeof(char); }
00357
00358 protected:
00359
00360 template<class T> inline static void byteswap(T& dstc, const T& srcc) {
00361 char* dst=(char*)&dstc;
00362 const char* src=(const char*)&srcc;
00363 for(unsigned int i=0; i<sizeof(T); i++)
00364 dst[sizeof(T)-1-i]=src[i];
00365 }
00366 };
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 #endif