ddc
bserialize.h
Go to the documentation of this file.
1 //
2 // This file is part of DDC.
3 //
4 // DDC is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // DDC is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // ========== Dialing Syntax Analysis (www.aot.ru)
18 // ========== Copyright by Alexey Sokirko, Bryan Jurish (2011)
19 
20 #ifndef bserialze_h
21 #define bserialze_h
22 
23 #include "utilit.h"
24 
25 // ============== DWORD =====================
26 inline size_t get_size_in_bytes (const DWORD& t)
27 {
28  return sizeof(DWORD);
29 };
30 inline size_t save_to_bytes(const DWORD& i, BYTE* buf)
31 {
32  *((DWORD*)buf) = i;
33  return get_size_in_bytes(i);
34 }
35 
36 inline size_t restore_from_bytes(DWORD& i, const BYTE* buf)
37 {
38  i = *((DWORD*)buf);
39  return get_size_in_bytes(i);
40 };
41 
42 
43 
44 // ============== int =====================
45 inline size_t get_size_in_bytes (const int& t)
46 {
47  return sizeof(int);
48 };
49 inline size_t save_to_bytes(const int& i, BYTE* buf)
50 {
51  *((int*)buf) = i;
52  return get_size_in_bytes(i);
53 }
54 
55 inline size_t restore_from_bytes(int& i, const BYTE* buf)
56 {
57  i = *((int*)buf);
58  return get_size_in_bytes(i);
59 };
60 
61 
62 // ============== CHAR =====================
63 inline size_t get_size_in_bytes (const char& t)
64 {
65  return 1;
66 };
67 
68 inline size_t save_to_bytes(const char& i, BYTE* buf)
69 {
70  *((char*)buf) = i;
71  return get_size_in_bytes(i);
72 };
73 
74 inline size_t restore_from_bytes(char& i, const BYTE* buf)
75 {
76  i = *((char*)buf);
77  return get_size_in_bytes(i);
78 };
79 
80 
81 // ============== BYTE =====================
82 inline size_t get_size_in_bytes (const BYTE& t)
83 {
84  return 1;
85 };
86 
87 inline size_t save_to_bytes(const BYTE& i, BYTE* buf)
88 {
89  *((BYTE*)buf) = i;
90  return get_size_in_bytes(i);
91 };
92 
93 inline size_t restore_from_bytes(BYTE& i, const BYTE* buf)
94 {
95  i = *((BYTE*)buf);
96  return get_size_in_bytes(i);
97 };
98 
99 
100 
101 // ============== WORD =====================
102 inline size_t get_size_in_bytes (const WORD& t)
103 {
104  return 2;
105 };
106 
107 inline size_t save_to_bytes(const WORD& i, BYTE* buf)
108 {
109  *((WORD*)buf) = i;
110  return get_size_in_bytes(i);
111 };
112 
113 inline size_t restore_from_bytes(WORD& i, const BYTE* buf)
114 {
115  i = *((WORD*)buf);
116  return get_size_in_bytes(i);
117 };
118 
119 
120 // ============== QWORD =====================
121 inline size_t get_size_in_bytes (const QWORD& t)
122 {
123  return 8;
124 };
125 
126 inline size_t save_to_bytes(const QWORD& i, BYTE* buf)
127 {
128  *((QWORD*)buf) = i;
129  return get_size_in_bytes(i);
130 };
131 
132 inline size_t restore_from_bytes(QWORD& i, const BYTE* buf)
133 {
134  i = *((QWORD*)buf);
135  return get_size_in_bytes(i);
136 };
137 
138 // ============== DOUBLE =====================
139 inline size_t get_size_in_bytes (const double& t)
140 {
141  return sizeof(t);
142 };
143 inline size_t save_to_bytes(const double& i, BYTE* buf)
144 {
145  *((double*)buf) = i;
146  return get_size_in_bytes(i);
147 }
148 
149 inline size_t restore_from_bytes(double& i, const BYTE* buf)
150 {
151  i = *((double*)buf);
152  return get_size_in_bytes(i);
153 };
154 
155 
156 
157 // ============== PAIR =====================
158 template <class T>
159 inline size_t get_size_in_bytes (const pair<T, T>& t)
160 {
161  return get_size_in_bytes(t.first) * 2;
162 };
163 
164 template <class T>
165 inline size_t save_to_bytes(const pair<T, T>& t, BYTE* buf)
166 
167 
168 {
169  buf += save_to_bytes(t.first, buf);
170  buf += save_to_bytes(t.second, buf);
171  return get_size_in_bytes(t);
172 };
173 
174 template <class T>
175 inline size_t restore_from_bytes(pair<T, T>& t, const BYTE* buf)
176 {
177  buf += restore_from_bytes(t.first, buf);
178  buf += restore_from_bytes(t.second, buf);
179  return get_size_in_bytes(t);
180 };
181 
182 //
183 // ============== PAIR =====================
184 template <class T>
185 inline size_t get_size_in_bytes (const pair<QWORD, T>& t)
186 {
187  return get_size_in_bytes(t.first) + get_size_in_bytes(t.second);
188 };
189 
190 template <class T>
191 inline size_t save_to_bytes(const pair<QWORD, T>& t, BYTE* buf)
192 
193 
194 {
195  buf += save_to_bytes(t.first, buf);
196  buf += save_to_bytes(t.second, buf);
197  return get_size_in_bytes(t);
198 };
199 
200 template <class T>
201 inline size_t restore_from_bytes(pair<QWORD, T>& t, const BYTE* buf)
202 {
203  buf += restore_from_bytes(t.first, buf);
204  buf += restore_from_bytes(t.second, buf);
205  return get_size_in_bytes(t);
206 };
207 
208 
209 // ============== TRIPLE =====================
210 
211 
212 template <class T>
213 inline size_t get_size_in_bytes (const troika<T, T, T>& t)
214 {
215  return get_size_in_bytes(t.first) * 3;
216 };
217 
218 template <class T>
219 inline size_t save_to_bytes(const troika<T, T, T>& t, BYTE* buf)
220 {
221  buf += save_to_bytes(t.first, buf);
222  buf += save_to_bytes(t.second, buf);
223  buf += save_to_bytes(t.third, buf);
224  return get_size_in_bytes(t);
225 };
226 
227 template <class T>
228 inline size_t restore_from_bytes(troika<T, T, T>& t, const BYTE* buf)
229 {
230  buf += restore_from_bytes(t.first, buf);
231  buf += restore_from_bytes(t.second, buf);
232  buf += restore_from_bytes(t.third, buf);
233  return get_size_in_bytes(t);
234 };
235 
236 
237 const int VectorMaxStructSize = 200;
238 
239 template <class T>
240 bool BinaryReadItem (FILE* fp, T& V)
241 {
242  BYTE buffer[VectorMaxStructSize];
243  size_t size_of_t = get_size_in_bytes(V);
244  if (fread ((void*)buffer, size_of_t, 1, fp)!=1)
245  return false;
246  restore_from_bytes (V, buffer);
247  return true;
248 }
249 
250 template <class T>
251 void ReadVectorInner (FILE* fp, vector<T>& V, size_t Count)
252 {
253  try
254  {
255  T dummy;
256  size_t size_of_t = get_size_in_bytes(dummy);
257 
258  BYTE buffer[VectorMaxStructSize];
259  assert (size_of_t < VectorMaxStructSize);
260 
261  V.reserve(V.size() + Count);
262  if (V.capacity() < Count)
263  throw CExpc(Format("cannot allocate %u bytes in ReadVectorInner",size_of_t*Count));
264 
265  for (int i = 0; i < Count; i++)
266  {
267  if (fread ((void*)buffer, size_of_t, 1, fp)!=1)
268  throw CExpc(Format("cannot read %i item in ReadVectorInner",i));
269  restore_from_bytes (dummy, buffer);
270  V.push_back(dummy);
271  };
272  }
273  catch ( length_error &e )
274  {
275  fprintf (stderr, "ReadVectorInner:length_error exception is caught: %s\n", e.what( ));
276  throw;
277  }
278  catch ( exception &e )
279  {
280  fprintf (stderr, "ReadVectorInner: general std::exception is caught: %s\n", e.what( ));
281  throw;
282  };
283 
284 };
285 
286 template <class T>
288 {
289  T dummy;
290  return get_size_in_bytes(dummy);
291 }
292 
293 template <class T>
294 inline void ReadVector (const string& FileName, vector<T>& V)
295 {
296  V.clear();
297  file_off_t sz = FileSize(FileName.c_str());
298 
299  FILE* fp = fopen (FileName.c_str(),"rb");
300  if (!fp) return;
301 
302  T dummy;
303  size_t size_of_t = get_size_in_bytes(dummy);
304  size_t Count = (size_t)sz/size_of_t;
305  try {
306  ReadVectorInner(fp, V, Count);
307  fclose (fp);
308  fp = 0;
309  }
310  catch (...)
311  {
312  if (fp) fclose (fp);
313  throw;
314  }
315 
316 };
317 
318 
319 template <class T>
320 bool BinaryWriteItem (FILE* fp, const T& V)
321 {
322  BYTE buffer[VectorMaxStructSize];
323  save_to_bytes(V, buffer);
324  size_t size_of_t = get_size_in_bytes(V);
325  return fwrite((void*)buffer, size_of_t, 1, fp) == 1;
326 }
327 
328 template <class T>
329 bool WriteVectorInner (FILE* fp, const vector<T>& V)
330 {
331  T dummy;
332  size_t size_of_t = get_size_in_bytes(dummy);
333 
334  BYTE buffer[VectorMaxStructSize];
335  assert (size_of_t < VectorMaxStructSize);
336 
337  size_t count = V.size();
338 
339  for (size_t i =0; i < count; i++)
340  {
341  save_to_bytes(V[i], buffer);
342  if (!fwrite((void*)buffer, size_of_t, 1, fp)) return false;
343  };
344  return true;
345 };
346 
347 
348 template <class T>
349 inline bool WriteVector (const string& FileName, const vector<T>& V)
350 {
351  FILE* fp = fopen (FileName.c_str(),"wb");
352  if (!fp)
353  {
354  ErrorMessage ("Cannot write vector to "+FileName);
355  return false;
356  };
357  bool b = WriteVectorInner(fp, V);
358  fclose (fp);
359  return b;
360 };
361 
362 #endif
363 
364 /*--- emacs style variables ---
365  * Local Variables:
366  * mode: C++
367  * c-file-style: "ellemtel"
368  * c-basic-offset: 4
369  * tab-width: 8
370  * indent-tabs-mode: nil
371  * End:
372  */
T3 third
Definition: utilit.h:175
size_t get_size_in_bytes(const DWORD &t)
Definition: bserialize.h:26
string Format(const char *format,...)
Definition: ddcString.cpp:393
uint64_t QWORD
Definition: utilit.h:107
QWORD file_off_t
Definition: utilit.h:179
generic local exception class moo: derived from stdexcept runtime_error
Definition: utilit.h:183
const int VectorMaxStructSize
Definition: bserialize.h:237
size_t restore_from_bytes(DWORD &i, const BYTE *buf)
Definition: bserialize.h:36
size_t save_to_bytes(const DWORD &i, BYTE *buf)
Definition: bserialize.h:30
void ErrorMessage(const string &label, const string &message)
Definition: utilit.cpp:400
uint16_t WORD
Definition: utilit.h:106
bool WriteVector(const string &FileName, const vector< T > &V)
Definition: bserialize.h:349
Definition: utilit.h:173
bool BinaryWriteItem(FILE *fp, const T &V)
Definition: bserialize.h:320
void ReadVectorInner(FILE *fp, vector< T > &V, size_t Count)
Definition: bserialize.h:251
bool BinaryReadItem(FILE *fp, T &V)
Definition: bserialize.h:240
unsigned char BYTE
Definition: utilit.h:94
void ReadVector(const string &FileName, vector< T > &V)
Definition: bserialize.h:294
bool WriteVectorInner(FILE *fp, const vector< T > &V)
Definition: bserialize.h:329
file_off_t FileSize(const char *filename)
Definition: utilit.cpp:340
size_t GetSizeInBytes()
Definition: bserialize.h:287
uint32_t DWORD
Definition: utilit.h:105
Definition: lex_util.h:172