1 /*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cstdio>
18
19 #include "stdafx.h"
20
21 #include "zorbaserialization/serialize_basic_types.h"
22 #include "zorbaserialization/archiver.h"
23 #include "zorbaserialization/base64impl.h"
24
25 #include "diagnostics/xquery_diagnostics.h"
26 #include "diagnostics/util_macros.h"
27 #include "diagnostics/assert.h"
28
29
30 namespace zorba
31 {
32
33 namespace serialization
34 {
35
36 #if 0
37 /*******************************************************************************
38
39 ********************************************************************************/
40 void operator&(Archiver& ar, int64_t& obj)
41 {
42 if (ar.is_serializing_out())
43 {
44 ar.add_simple_temp_field(TYPE_INT64, &obj);
45 }
46 else
47 {
48 ar.read_next_simple_temp_field(TYPE_INT64, &obj);
49 }
50 }
51
52
53 /*******************************************************************************
54
55 ********************************************************************************/
56 void operator&(Archiver& ar, uint64_t& obj)
57 {
58 if (ar.is_serializing_out())
59 {
60 ar.add_simple_temp_field(TYPE_UINT64, &obj);
61 }
62 else
63 {
64 ar.read_next_simple_temp_field(TYPE_UINT64, &obj);
65 }
66 }
67
68
69 /*******************************************************************************
70
71 ********************************************************************************/
72 void operator&(Archiver& ar, int32_t& obj)
73 {
74 if (ar.is_serializing_out())
75 {
76 ar.add_simple_temp_field(TYPE_INT32, &obj);
77 }
78 else
79 {
80 ar.read_next_simple_temp_field(TYPE_INT32, &obj);
81 }
82 }
83
84
85 /*******************************************************************************
86
87 ********************************************************************************/
88 void operator&(Archiver& ar, uint32_t& obj)
89 {
90 if (ar.is_serializing_out())
91 {
92 ar.add_simple_temp_field(TYPE_UINT32, &obj);
93 }
94 else
95 {
96 ar.read_next_simple_temp_field(TYPE_UINT32, &obj);
97 }
98 }
99
100
101 /*******************************************************************************
102
103 ********************************************************************************/
104 void operator&(Archiver& ar, int16_t& obj)
105 {
106 if (ar.is_serializing_out())
107 {
108 ar.add_simple_temp_field(TYPE_INT16, &obj);
109 }
110 else
111 {
112 ar.read_next_simple_temp_field(TYPE_INT16, &obj);
113 }
114 }
115
116
117 /*******************************************************************************
118
119 ********************************************************************************/
120 void operator&(Archiver& ar, uint16_t& obj)
121 {
122 if (ar.is_serializing_out())
123 {
124 ar.add_simple_temp_field(TYPE_UINT16, &obj);
125 }
126 else
127 {
128 ar.read_next_simple_temp_field(TYPE_UINT16, &obj);
129 }
130 }
131 #endif
132
133
134 /*******************************************************************************
135
136 ********************************************************************************/
operator &(Archiver & ar,short & obj)137 void operator&(Archiver& ar, short& obj)
138 {
139 assert(sizeof(short) == 2);
140
141 if (ar.is_serializing_out())
142 {
143 int16_t int16v = obj;
144 ar.add_simple_temp_field(TYPE_INT16, &int16v);
145 }
146 else
147 {
148 int16_t int16v;
149 ar.read_next_simple_temp_field(TYPE_INT16, &int16v);
150
151 obj = static_cast<short>(int16v);
152 }
153 }
154
155
156 /*******************************************************************************
157
158 ********************************************************************************/
operator &(Archiver & ar,unsigned short & obj)159 void operator&(Archiver& ar, unsigned short& obj)
160 {
161 assert(sizeof(unsigned short) == 2);
162
163 if (ar.is_serializing_out())
164 {
165 uint16_t uint16v = obj;
166 ar.add_simple_temp_field(TYPE_UINT16, &uint16v);
167 }
168 else
169 {
170 uint16_t uint16v;
171 ar.read_next_simple_temp_field(TYPE_UINT16, &uint16v);
172
173 obj = static_cast<unsigned short>(uint16v);
174 }
175 }
176
177
178 /*******************************************************************************
179
180 ********************************************************************************/
operator &(Archiver & ar,int & obj)181 void operator&(Archiver& ar, int& obj)
182 {
183 if (ar.is_serializing_out())
184 {
185 int64_t int64v = obj;
186 ar.add_simple_temp_field(TYPE_INT64, &int64v);
187 }
188 else
189 {
190 int64_t int64v;
191 ar.read_next_simple_temp_field(TYPE_INT64, &int64v);
192
193 obj = static_cast<int>(int64v);
194 }
195 }
196
197
198 /*******************************************************************************
199
200 ********************************************************************************/
operator &(Archiver & ar,unsigned int & obj)201 void operator&(Archiver& ar, unsigned int& obj)
202 {
203 if (ar.is_serializing_out())
204 {
205 uint64_t uint64v = obj;
206 ar.add_simple_temp_field(TYPE_UINT64, &uint64v);
207 }
208 else
209 {
210 uint64_t uint64v;
211 ar.read_next_simple_temp_field(TYPE_UINT64, &uint64v);
212
213 obj = static_cast<unsigned int>(uint64v);
214 }
215 }
216
217
218 /*******************************************************************************
219
220 ********************************************************************************/
operator &(Archiver & ar,long & obj)221 void operator&(Archiver& ar, long& obj)
222 {
223 if (ar.is_serializing_out())
224 {
225 int64_t int64v = obj;
226 ar.add_simple_temp_field(TYPE_INT64, &int64v);
227 }
228 else
229 {
230 int64_t int64v;
231 ar.read_next_simple_temp_field(TYPE_INT64, &int64v);
232
233 obj = static_cast<long>(int64v);
234 }
235 }
236
237
238 /*******************************************************************************
239
240 ********************************************************************************/
operator &(Archiver & ar,ulong & obj)241 void operator&(Archiver& ar, ulong& obj)
242 {
243 if (ar.is_serializing_out())
244 {
245 uint64_t uint64v = obj;
246 ar.add_simple_temp_field(TYPE_UINT64, &uint64v);
247 }
248 else
249 {
250 uint64_t uint64v;
251 ar.read_next_simple_temp_field(TYPE_UINT64, &uint64v);
252
253 obj = static_cast<ulong>(uint64v);
254 }
255 }
256
257
258 /*******************************************************************************
259
260 ********************************************************************************/
operator &(Archiver & ar,long long & obj)261 void operator&(Archiver& ar, long long& obj)
262 {
263 assert(sizeof(long long) == 8);
264
265 if (ar.is_serializing_out())
266 {
267 int64_t int64v = obj;
268 ar.add_simple_temp_field(TYPE_INT64, &int64v);
269 }
270 else
271 {
272 int64_t int64v;
273 ar.read_next_simple_temp_field(TYPE_INT64, &int64v);
274
275 obj = static_cast<long long>(int64v);
276 }
277 }
278
279
280 /*******************************************************************************
281
282 ********************************************************************************/
operator &(Archiver & ar,unsigned long long & obj)283 void operator&(Archiver& ar, unsigned long long& obj)
284 {
285 assert(sizeof(unsigned long long) == 8);
286
287 if (ar.is_serializing_out())
288 {
289 uint64_t uint64v = obj;
290 ar.add_simple_temp_field(TYPE_UINT64, &uint64v);
291 }
292 else
293 {
294 uint64_t uint64v;
295 ar.read_next_simple_temp_field(TYPE_UINT64, &uint64v);
296
297 obj = static_cast<unsigned long long>(uint64v);
298 }
299 }
300
301 #if 0
302 /*******************************************************************************
303
304 ********************************************************************************/
305 void serialize_csize(Archiver& ar, csize& obj)
306 {
307 assert(sizeof(csize) <= 8);
308
309 if (ar.is_serializing_out())
310 {
311 uint64_t uint64v = obj;
312 ar.add_simple_temp_field(TYPE_UINT64, &uint64v);
313 }
314 else
315 {
316 uint64_t uint64v;
317 ar.read_next_simple_temp_field(TYPE_UINT64, &uint64v);
318
319 obj = static_cast<csize>(uint64v);
320 }
321 }
322 #endif
323
324 /*******************************************************************************
325
326 ********************************************************************************/
serialize_enum(Archiver & ar,uint32_t & obj)327 void serialize_enum(Archiver& ar, uint32_t& obj)
328 {
329 if (ar.is_serializing_out())
330 {
331 ar.add_simple_temp_field(TYPE_ENUM, &obj);
332 }
333 else
334 {
335 ar.read_next_simple_temp_field(TYPE_ENUM, &obj);
336 }
337 }
338
339
340 /*******************************************************************************
341
342 ********************************************************************************/
operator &(Archiver & ar,char & obj)343 void operator&(Archiver& ar, char& obj)
344 {
345 if (ar.is_serializing_out())
346 {
347 ar.add_simple_temp_field(TYPE_CHAR, &obj);
348 }
349 else
350 {
351 ar.read_next_simple_temp_field(TYPE_CHAR, &obj);
352 }
353 }
354
355
356 /*******************************************************************************
357
358 ********************************************************************************/
operator &(Archiver & ar,unsigned char & obj)359 void operator&(Archiver& ar, unsigned char& obj)
360 {
361 if (ar.is_serializing_out())
362 {
363 ar.add_simple_temp_field(TYPE_UCHAR, &obj);
364 }
365 else
366 {
367 ar.read_next_simple_temp_field(TYPE_UCHAR, &obj);
368 }
369 }
370
371
372 /*******************************************************************************
373
374 ********************************************************************************/
operator &(Archiver & ar,float & obj)375 void operator&(Archiver& ar, float& obj)
376 {
377 if (ar.is_serializing_out())
378 {
379 FloatImpl<float> zorba_float(obj);
380 zstring float_str = zorba_float.toString();
381
382 if (isdigit(float_str.c_str()[0]))
383 {
384 char strtemp[100];
385 sprintf(strtemp, "%.7e", (double)obj);
386 float_str = strtemp;
387 }
388
389 ar.add_simple_temp_field(TYPE_ZSTRING, &float_str);
390 }
391 else
392 {
393 zstring float_str;
394
395 ar.read_next_simple_temp_field(TYPE_ZSTRING, &float_str);
396
397 FloatImpl<float> zorba_float(float_str.c_str());
398 obj = zorba_float.getNumber();
399 }
400 }
401
402
403 /*******************************************************************************
404
405 ********************************************************************************/
operator &(Archiver & ar,double & obj)406 void operator&(Archiver& ar, double& obj)
407 {
408 if (ar.is_serializing_out())
409 {
410 FloatImpl<double> zorba_double(obj);
411 zstring double_str = zorba_double.toString();
412
413 if (isdigit(double_str.c_str()[0]))
414 {
415 char strtemp[100];
416 sprintf(strtemp, "%.16e", obj);
417 double_str = strtemp;
418 }
419
420 ar.add_simple_temp_field(TYPE_ZSTRING, &double_str);
421 }
422 else
423 {
424 zstring double_str;
425
426 ar.read_next_simple_temp_field(TYPE_ZSTRING, &double_str);
427
428 FloatImpl<double> zorba_double(double_str.c_str());
429 obj = zorba_double.getNumber();
430 }
431 }
432
433
434 /*******************************************************************************
435
436 ********************************************************************************/
operator &(Archiver & ar,bool & obj)437 void operator&(Archiver& ar, bool& obj)
438 {
439 if (ar.is_serializing_out())
440 {
441 ar.add_simple_temp_field(TYPE_BOOL, &obj);
442 }
443 else
444 {
445 ar.read_next_simple_temp_field(TYPE_BOOL, &obj);
446 }
447 }
448
449
450
451 /*******************************************************************************
452
453 ********************************************************************************/
operator &(Archiver & ar,zstring & obj)454 void operator&(Archiver& ar, zstring& obj)
455 {
456 if (ar.is_serializing_out())
457 {
458 ar.add_simple_temp_field(TYPE_ZSTRING, &obj);
459 }
460 else
461 {
462 ar.read_next_simple_temp_field(TYPE_ZSTRING, &obj);
463 }
464 }
465
466
467 /*******************************************************************************
468
469 ********************************************************************************/
operator &(Archiver & ar,std::string & obj)470 void operator&(Archiver& ar, std::string& obj)
471 {
472 if (ar.is_serializing_out())
473 {
474 ar.add_simple_temp_field(TYPE_STD_STRING, &obj);
475 }
476 else
477 {
478 ar.read_next_simple_temp_field(TYPE_STD_STRING, &obj);
479 }
480 }
481
482
483 /*******************************************************************************
484 This is needed for theDefaultCollation in the static context
485 ********************************************************************************/
operator &(Archiver & ar,std::string * & obj)486 void operator&(Archiver& ar, std::string*& obj)
487 {
488 if (ar.is_serializing_out())
489 {
490 ar.add_simple_ptr_field(TYPE_STD_STRING, obj);
491 }
492 else
493 {
494 ar.read_next_simple_ptr_field(TYPE_STD_STRING, (void**)&obj);
495 }
496 }
497
498
499 /*******************************************************************************
500
501 ********************************************************************************/
operator &(Archiver & ar,XQPCollator * & obj)502 void operator&(Archiver& ar, XQPCollator*& obj)
503 {
504 if (ar.is_serializing_out())
505 {
506 if (obj == NULL)
507 {
508 ar.add_simple_ptr_field(TYPE_NULL, NULL);
509 return;
510 }
511
512 ar.add_simple_ptr_field(TYPE_COLLATOR, obj);
513 }
514 else
515 {
516 ar.read_next_simple_ptr_field(TYPE_COLLATOR, (void**)&obj);
517 }
518 }
519
520
521 /*******************************************************************************
522
523 ********************************************************************************/
operator &(Archiver & ar,MAPM & obj)524 void operator&(Archiver& ar, MAPM& obj)
525 {
526 if (ar.is_serializing_out())
527 {
528 int nr_digits = obj.significant_digits();
529 char* lBuffer = (char*)malloc(nr_digits + 20);
530
531 obj.toString(lBuffer, nr_digits);
532
533 if (strchr(lBuffer, '.'))
534 {
535 //save only necessary decimals
536 char* e_ptr = strrchr(lBuffer, 'E');
537 char* tail = e_ptr ? e_ptr-1 : lBuffer+strlen(lBuffer)-1;
538
539 while (*tail == '0')
540 tail--;
541
542 if (*tail == '.')
543 tail++;
544
545 if (e_ptr)
546 {
547 int i;
548 for(i = 0; e_ptr[i]; ++i)
549 tail[i+1] = e_ptr[i];
550
551 tail[i+1] = 0;
552 }
553 else
554 tail[1] = 0;
555 }
556
557 zstring value = lBuffer;
558
559 ar.add_simple_temp_field(TYPE_ZSTRING, &value);
560
561 free(lBuffer);
562 }
563 else
564 {
565 zstring value;
566
567 ar.read_next_simple_temp_field(TYPE_ZSTRING, &value);
568
569 obj = value.c_str();
570 }
571 }
572
573
574 }
575 }
576