00001
00002
00003
00004
00005 #ifndef bserialze_h
00006 #define bserialze_h
00007
00008 #include "utilit.h"
00009
00010
00011 inline size_t get_size_in_bytes (const DWORD& t)
00012 {
00013 return sizeof(DWORD);
00014 };
00015 inline size_t save_to_bytes(const DWORD& i, BYTE* buf)
00016 {
00017 *((DWORD*)buf) = i;
00018 return get_size_in_bytes(i);
00019 }
00020
00021 inline size_t restore_from_bytes(DWORD& i, const BYTE* buf)
00022 {
00023 i = *((DWORD*)buf);
00024 return get_size_in_bytes(i);
00025 };
00026
00027
00028
00029
00030 inline size_t get_size_in_bytes (const int& t)
00031 {
00032 return sizeof(int);
00033 };
00034 inline size_t save_to_bytes(const int& i, BYTE* buf)
00035 {
00036 *((int*)buf) = i;
00037 return get_size_in_bytes(i);
00038 }
00039
00040 inline size_t restore_from_bytes(int& i, const BYTE* buf)
00041 {
00042 i = *((int*)buf);
00043 return get_size_in_bytes(i);
00044 };
00045
00046
00047
00048 inline size_t get_size_in_bytes (const char& t)
00049 {
00050 return 1;
00051 };
00052
00053 inline size_t save_to_bytes(const char& i, BYTE* buf)
00054 {
00055 *((char*)buf) = i;
00056 return get_size_in_bytes(i);
00057 };
00058
00059 inline size_t restore_from_bytes(char& i, const BYTE* buf)
00060 {
00061 i = *((char*)buf);
00062 return get_size_in_bytes(i);
00063 };
00064
00065
00066
00067 inline size_t get_size_in_bytes (const BYTE& t)
00068 {
00069 return 1;
00070 };
00071
00072 inline size_t save_to_bytes(const BYTE& i, BYTE* buf)
00073 {
00074 *((BYTE*)buf) = i;
00075 return get_size_in_bytes(i);
00076 };
00077
00078 inline size_t restore_from_bytes(BYTE& i, const BYTE* buf)
00079 {
00080 i = *((BYTE*)buf);
00081 return get_size_in_bytes(i);
00082 };
00083
00084
00085
00086
00087 inline size_t get_size_in_bytes (const WORD& t)
00088 {
00089 return 2;
00090 };
00091
00092 inline size_t save_to_bytes(const WORD& i, BYTE* buf)
00093 {
00094 *((WORD*)buf) = i;
00095 return get_size_in_bytes(i);
00096 };
00097
00098 inline size_t restore_from_bytes(WORD& i, const BYTE* buf)
00099 {
00100 i = *((WORD*)buf);
00101 return get_size_in_bytes(i);
00102 };
00103
00104
00105
00106 inline size_t get_size_in_bytes (const QWORD& t)
00107 {
00108 return 8;
00109 };
00110
00111 inline size_t save_to_bytes(const QWORD& i, BYTE* buf)
00112 {
00113 *((QWORD*)buf) = i;
00114 return get_size_in_bytes(i);
00115 };
00116
00117 inline size_t restore_from_bytes(QWORD& i, const BYTE* buf)
00118 {
00119 i = *((QWORD*)buf);
00120 return get_size_in_bytes(i);
00121 };
00122
00123
00124 inline size_t get_size_in_bytes (const double& t)
00125 {
00126 return sizeof(t);
00127 };
00128 inline size_t save_to_bytes(const double& i, BYTE* buf)
00129 {
00130 *((double*)buf) = i;
00131 return get_size_in_bytes(i);
00132 }
00133
00134 inline size_t restore_from_bytes(double& i, const BYTE* buf)
00135 {
00136 i = *((double*)buf);
00137 return get_size_in_bytes(i);
00138 };
00139
00140
00141
00142
00143 template <class T>
00144 inline size_t get_size_in_bytes (const pair<T, T>& t)
00145 {
00146 return get_size_in_bytes(t.first) * 2;
00147 };
00148
00149 template <class T>
00150 inline size_t save_to_bytes(const pair<T, T>& t, BYTE* buf)
00151
00152
00153 {
00154 buf += save_to_bytes(t.first, buf);
00155 buf += save_to_bytes(t.second, buf);
00156 return get_size_in_bytes(t);
00157 };
00158
00159 template <class T>
00160 inline size_t restore_from_bytes(pair<T, T>& t, const BYTE* buf)
00161 {
00162 buf += restore_from_bytes(t.first, buf);
00163 buf += restore_from_bytes(t.second, buf);
00164 return get_size_in_bytes(t);
00165 };
00166
00167
00168
00169 template <class T>
00170 inline size_t get_size_in_bytes (const pair<QWORD, T>& t)
00171 {
00172 return get_size_in_bytes(t.first) + get_size_in_bytes(t.second);
00173 };
00174
00175 template <class T>
00176 inline size_t save_to_bytes(const pair<QWORD, T>& t, BYTE* buf)
00177
00178
00179 {
00180 buf += save_to_bytes(t.first, buf);
00181 buf += save_to_bytes(t.second, buf);
00182 return get_size_in_bytes(t);
00183 };
00184
00185 template <class T>
00186 inline size_t restore_from_bytes(pair<QWORD, T>& t, const BYTE* buf)
00187 {
00188 buf += restore_from_bytes(t.first, buf);
00189 buf += restore_from_bytes(t.second, buf);
00190 return get_size_in_bytes(t);
00191 };
00192
00193
00194
00195
00196
00197 template <class T>
00198 inline size_t get_size_in_bytes (const troika<T, T, T>& t)
00199 {
00200 return get_size_in_bytes(t.first) * 3;
00201 };
00202
00203 template <class T>
00204 inline size_t save_to_bytes(const troika<T, T, T>& t, BYTE* buf)
00205 {
00206 buf += save_to_bytes(t.first, buf);
00207 buf += save_to_bytes(t.second, buf);
00208 buf += save_to_bytes(t.third, buf);
00209 return get_size_in_bytes(t);
00210 };
00211
00212 template <class T>
00213 inline size_t restore_from_bytes(troika<T, T, T>& t, const BYTE* buf)
00214 {
00215 buf += restore_from_bytes(t.first, buf);
00216 buf += restore_from_bytes(t.second, buf);
00217 buf += restore_from_bytes(t.third, buf);
00218 return get_size_in_bytes(t);
00219 };
00220
00221
00222 const int VectorMaxStructSize = 200;
00223
00224 template <class T>
00225 bool BinaryReadItem (FILE* fp, T& V)
00226 {
00227 BYTE buffer[VectorMaxStructSize];
00228 size_t size_of_t = get_size_in_bytes(V);
00229 if (fread ((void*)buffer, size_of_t, 1, fp)!=1)
00230 return false;
00231 restore_from_bytes (V, buffer);
00232 return true;
00233 }
00234
00235 template <class T>
00236 void ReadVectorInner (FILE* fp, vector<T>& V, size_t Count)
00237 {
00238 #ifdef WIN32
00239 if (_setmode( _fileno( fp ), _O_BINARY ) != _O_BINARY)
00240 throw CExpc(Format("ReadVectorInner: File is not opened in binary mode!"));
00241 #endif
00242
00243 try
00244 {
00245 T dummy;
00246 size_t size_of_t = get_size_in_bytes(dummy);
00247
00248 BYTE buffer[VectorMaxStructSize];
00249 assert (size_of_t < VectorMaxStructSize);
00250
00251 V.reserve(V.size() + Count);
00252 if (V.capacity() < Count)
00253 throw CExpc(Format("cannot allocate %u bytes in ReadVectorInner",size_of_t*Count));
00254
00255 for (int i = 0; i < Count; i++)
00256 {
00257 if (fread ((void*)buffer, size_of_t, 1, fp)!=1)
00258 throw CExpc(Format("cannot read %i item in ReadVectorInner",i));
00259 restore_from_bytes (dummy, buffer);
00260 V.push_back(dummy);
00261 };
00262 }
00263 catch ( length_error &e )
00264 {
00265 fprintf (stderr, "ReadVectorInner:length_error exception is caught: %s\n", e.what( ));
00266 throw;
00267 }
00268 catch ( exception &e )
00269 {
00270 fprintf (stderr, "ReadVectorInner: general std::exception is caught: %s\n", e.what( ));
00271 throw;
00272 };
00273
00274 };
00275
00276 template <class T>
00277 size_t GetSizeInBytes()
00278 {
00279 T dummy;
00280 return get_size_in_bytes(dummy);
00281 }
00282
00283 template <class T>
00284 inline void ReadVector (const string& FileName, vector<T>& V)
00285 {
00286 V.clear();
00287 file_off_t sz = FileSize(FileName.c_str());
00288
00289 FILE* fp = fopen (FileName.c_str(),"rb");
00290 if (!fp) return;
00291
00292 T dummy;
00293 size_t size_of_t = get_size_in_bytes(dummy);
00294 size_t Count = (size_t)sz/size_of_t;
00295 try {
00296 ReadVectorInner(fp, V, Count);
00297 fclose (fp);
00298 fp = 0;
00299 }
00300 catch (...)
00301 {
00302 if (fp) fclose (fp);
00303 throw;
00304 }
00305
00306 };
00307
00308
00309 template <class T>
00310 bool BinaryWriteItem (FILE* fp, const T& V)
00311 {
00312 BYTE buffer[VectorMaxStructSize];
00313 save_to_bytes(V, buffer);
00314 size_t size_of_t = get_size_in_bytes(V);
00315 return fwrite((void*)buffer, size_of_t, 1, fp) == 1;
00316 }
00317
00318 template <class T>
00319 bool WriteVectorInner (FILE* fp, const vector<T>& V)
00320 {
00321 #ifdef WIN32
00322 if (_setmode( _fileno( fp ), _O_BINARY ) != _O_BINARY)
00323 {
00324 ErrorMessage("WriteVectorInner", "File is not opened in binary mode!");
00325 return false;
00326 };
00327 #endif
00328
00329 T dummy;
00330 size_t size_of_t = get_size_in_bytes(dummy);
00331
00332 BYTE buffer[VectorMaxStructSize];
00333 assert (size_of_t < VectorMaxStructSize);
00334
00335 size_t count = V.size();
00336
00337 for (size_t i =0; i < count; i++)
00338 {
00339 save_to_bytes(V[i], buffer);
00340 if (!fwrite((void*)buffer, size_of_t, 1, fp)) return false;
00341 };
00342 return true;
00343 };
00344
00345
00346 template <class T>
00347 inline bool WriteVector (const string& FileName, const vector<T>& V)
00348 {
00349 FILE* fp = fopen (FileName.c_str(),"wb");
00350 if (!fp)
00351 {
00352 ErrorMessage ("Cannot write vector to "+FileName);
00353 return false;
00354 };
00355 bool b = WriteVectorInner(fp, V);
00356 fclose (fp);
00357 return b;
00358 };
00359
00360 #endif
00361
00362