00001 #include "LoadSave.h"
00002 #include <iostream>
00003 #include <string.h>
00004
00005 LoadSave::~LoadSave() {}
00006
00007 unsigned int LoadSave::checkCreator(const char* creator, const char buf[], unsigned int len, bool isLoading) const {
00008 unsigned int sz=0,last;
00009 char* type=NULL;
00010 if(!(last=decode(type,buf+sz,len))) return 0; else sz+=last;
00011 if(strncmp(type,creator,strlen(creator))!=0) {
00012 if(isLoading)
00013 std::cout << "*** WARNING " << creator << "::LoadBuffer - corruption detected" << std::endl;
00014 return 0;
00015 }
00016 delete [] type;
00017 return sz;
00018 }
00019
00020 unsigned int LoadSave::checkCreator(const char* creator, FILE* f, bool isLoading) const {
00021 unsigned int sz=0,last;
00022 unsigned int origpos=ftell(f);
00023 char* type=NULL;
00024 if(!(last=decode(type,f))) {
00025 fseek(f,origpos,SEEK_SET);
00026 return 0;
00027 } else
00028 sz+=last;
00029 if(strncmp(type,creator,strlen(creator))!=0) {
00030 if(isLoading)
00031 std::cout << "*** WARNING " << creator << "::LoadBuffer - corruption detected" << std::endl;
00032 fseek(f,origpos,SEEK_SET);
00033 return 0;
00034 }
00035 delete [] type;
00036 return sz;
00037 }
00038
00039 unsigned int LoadSave::saveCreator(const char* creator, char buf[], unsigned int len) const {
00040 return encode(std::string(creator),buf,len);
00041 }
00042
00043 unsigned int LoadSave::saveCreator(const char* creator, FILE* f) const {
00044 return encode(creator,f);
00045 }
00046
00047 unsigned int LoadSave::LoadFile(const char* file) {
00048 int err;
00049 std::cout << "Loading: " << file << std::endl;
00050 FILE* f = fopen(file,"r");
00051 if(f==NULL) {
00052 std::cout << "*** WARNING could not open file for loading \"" << file << "\"" << std::endl;
00053 return 0;
00054 }
00055 unsigned int sz = LoadFileStream(f);
00056 if(sz==0)
00057 std::cout << "*** WARNING loading of " << file << " failed " << std::endl;
00058 err=fclose(f);
00059 if(err!=0) {
00060 std::cout << "*** WARNING error " << err << " while closing " << file << std::endl;
00061 return 0;
00062 }
00063 return sz;
00064 }
00065 unsigned int LoadSave::SaveFile(const char* file) const {
00066 int err;
00067 std::cout << "Saving: " << file << std::endl;
00068 FILE* f = fopen(file,"w");
00069 if(f==NULL) {
00070 std::cout << "*** WARNING could not open file for saving \"" << file << "\"" << std::endl;
00071 return 0;
00072 }
00073 unsigned int sz = SaveFileStream(f);
00074 if(sz==0)
00075 std::cout << "*** WARNING saving of " << file << " failed " << std::endl;
00076 err=fclose(f);
00077 if(err!=0) {
00078 std::cout << "*** WARNING error " << err << " while closing " << file << std::endl;
00079 return 0;
00080 }
00081 return sz;
00082 }
00083
00084 unsigned int LoadSave::LoadFileStream(FILE* f) {
00085 unsigned int cap=128;
00086 unsigned int sz=0;
00087 unsigned int origpos=ftell(f);
00088 char * buf = new char[cap];
00089 if(buf==NULL) {
00090 std::cout << "*** WARNING could not allocate " << cap << "+ bytes for LoadFile";
00091 return 0;
00092 }
00093 unsigned int read=fread(&buf[sz],1,cap-sz,f);
00094 while(read==cap-sz) {
00095 char * newbuf = new char[cap*2];
00096 if(newbuf==NULL) {
00097 std::cout << "*** WARNING could not allocate " << cap*2 << "+ bytes for LoadFile";
00098 return 0;
00099 }
00100 memcpy(newbuf,buf,cap);
00101 delete [] buf;
00102 buf=newbuf;
00103 sz=cap;
00104 cap*=2;
00105 read=fread(&buf[sz],1,cap-sz,f);
00106 }
00107 sz+=read;
00108 unsigned int resp=LoadBuffer(buf,sz);
00109 delete [] buf;
00110 if(resp!=sz)
00111 fseek(f,origpos+resp,SEEK_SET);
00112 return resp;
00113 }
00114 unsigned int LoadSave::SaveFileStream(FILE* f) const {
00115 unsigned int sz=getBinSize();
00116 char * buf = new char[sz];
00117 memset(buf,0xF0,sz);
00118 if(buf==NULL) {
00119 std::cout << "*** WARNING could not allocate " << sz << " bytes for LoadFile";
00120 return 0;
00121 }
00122 unsigned int resp=SaveBuffer(buf,sz);
00123 if(resp==0) {
00124 std::cout << "*** WARNING SaveBuffer didn't write any data (possibly due to overflow or other error)" << std::endl;
00125 fwrite(buf,1,sz,f);
00126 } else {
00127 unsigned int wrote=fwrite(buf,1,resp,f);
00128 if(wrote!=resp)
00129 std::cout << "*** WARNING short write (wrote " << wrote << ", expected " << resp << ")" << std::endl;
00130 }
00131 delete [] buf;
00132 return resp;
00133 }
00134
00135 bool LoadSave::ChkAdvance(int res, const char** buf, unsigned int* len, const char* msg) {
00136 if(res>0) {
00137 *buf+=res;
00138 *len-=res;
00139 return true;
00140 } else {
00141 printf("%s",msg);
00142 return false;
00143 }
00144 }
00145
00146 bool LoadSave::ChkAdvance(int res, const char** buf, unsigned int* len, const char* msg, int arg1) {
00147 if(res>0) {
00148 *buf+=res;
00149 *len-=res;
00150 return true;
00151 } else {
00152 printf(msg,arg1);
00153 return false;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169