~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/lib/std/shl/Serial.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
12
12
// - special damages arising in any way out of the use of this software.     -
13
13
// ---------------------------------------------------------------------------
14
 
// - copyright (c) 1999-2012 amaury darsch                                   -
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
15
15
// ---------------------------------------------------------------------------
16
16
 
17
17
#include "Set.hpp"
27
27
#include "Strfifo.hpp"
28
28
#include "Relatif.hpp"
29
29
#include "Boolean.hpp"
 
30
#include "Message.hpp"
30
31
#include "Character.hpp"
31
32
#include "NameTable.hpp"
32
33
#include "QuarkZone.hpp"
33
34
#include "PrintTable.hpp"
34
35
#include "InputStream.hpp"
35
36
#include "cmem.hpp"
 
37
#include "ccnv.hpp"
36
38
 
37
39
namespace afnix {
38
40
 
41
43
  // -------------------------------------------------------------------------
42
44
 
43
45
  // the maximum number of serial codes
44
 
  static const long SERIAL_CBKMAX = 256;
 
46
  static const long SRL_CBAK_MAX = 256;
 
47
  // the default block size
 
48
  static const long SRL_BLOK_SIZ = 1014;
 
49
 
45
50
  // the array of serial callback
46
51
  static Serial::t_genser* p_sercbk = nilp;
47
52
 
48
53
  // the serial callback deallocator
49
54
  static void del_serial_cbk (void) {
50
 
    delete p_sercbk;
 
55
    delete [] p_sercbk;
51
56
    p_sercbk = nilp;
52
57
  }
53
58
 
54
59
  // the serial callback allocator
55
60
  static void new_serial_cbk (void) {
56
61
    if (p_sercbk == nilp) {
57
 
      p_sercbk = new Serial::t_genser[SERIAL_CBKMAX];
58
 
      for (long i = 0; i < SERIAL_CBKMAX; i++) p_sercbk[i] = nilp;
 
62
      p_sercbk = new Serial::t_genser[SRL_CBAK_MAX];
 
63
      for (long i = 0; i < SRL_CBAK_MAX; i++) p_sercbk[i] = nilp;
59
64
      c_gcleanup (del_serial_cbk);
60
65
    }
61
66
  }
76
81
    return (cbk ());
77
82
  }
78
83
 
 
84
  // get a block data size by type
 
85
  static long get_block_dsiz (const Serial::Block::t_btyp btyp) {
 
86
    long result = 0;
 
87
    switch (btyp) {
 
88
    case Serial::Block::BYTE:
 
89
    case Serial::Block::BOOL:
 
90
      result = sizeof (t_byte);
 
91
      break;
 
92
    case Serial::Block::LONG:
 
93
      result = sizeof (t_long);
 
94
      break;
 
95
    case Serial::Block::REAL:
 
96
      result = sizeof (t_real);
 
97
      break;
 
98
    case Serial::Block::RPT2:
 
99
      result = 2 * sizeof (t_long) + sizeof (t_real);
 
100
      break;
 
101
    }
 
102
    return result;
 
103
  }
 
104
   
 
105
  // -------------------------------------------------------------------------
 
106
  // - block section                                                         -
 
107
  // -------------------------------------------------------------------------
 
108
 
 
109
  // create a default empty block
 
110
 
 
111
  Serial::Block::Block (void) {
 
112
    d_btyp = BYTE;
 
113
    d_size = 0;
 
114
    d_dsiz = 0;
 
115
    d_bsiz = 0;
 
116
    d_blen = 0;
 
117
    p_byte = nilp;
 
118
  }
 
119
 
 
120
  // create an empty block by sid
 
121
 
 
122
  Serial::Block::Block (const t_byte sid) {
 
123
    switch (sid) {
 
124
    case SERIAL_OBLK_ID:
 
125
      d_btyp = BYTE;
 
126
      break;
 
127
    case SERIAL_BBLK_ID:
 
128
      d_btyp = BOOL;
 
129
      break;
 
130
    case SERIAL_LBLK_ID:
 
131
      d_btyp = LONG;
 
132
      break;
 
133
    case SERIAL_RBLK_ID:
 
134
      d_btyp = REAL;
 
135
      break;
 
136
    case SERIAL_RPT2_ID:
 
137
      d_btyp = RPT2;
 
138
      break;
 
139
    default:
 
140
      throw Exception ("serial-error", "invalid serial type for block");
 
141
      break;
 
142
    }
 
143
    d_size = 0;
 
144
    d_dsiz = get_block_dsiz (d_btyp);
 
145
    d_bsiz = 0;
 
146
    d_blen = 0;
 
147
    p_byte = nilp;
 
148
  }
 
149
 
 
150
  // create a serial block by size and type
 
151
 
 
152
  Serial::Block::Block (const long size, const t_btyp btyp) {
 
153
    // create the block
 
154
    d_btyp = btyp;
 
155
    d_size = (size <= 0) ? 0 : size;
 
156
    d_dsiz = get_block_dsiz (d_btyp);
 
157
    d_blen = 0;
 
158
    d_bsiz = size * d_dsiz;
 
159
    p_byte = (d_bsiz == 0) ? nilp : new t_byte[d_bsiz];
 
160
    // clear the block
 
161
    clear ();
 
162
  }
 
163
  
 
164
  // copy construct this serial block
 
165
 
 
166
  Serial::Block::Block (const Block& that) {
 
167
    d_btyp = that.d_btyp;
 
168
    d_size = that.d_size;
 
169
    d_dsiz = that.d_dsiz;
 
170
    d_bsiz = that.d_bsiz;
 
171
    d_blen = that.d_blen;
 
172
    p_byte = (d_bsiz == 0) ? nilp : new t_byte[d_bsiz];
 
173
    for (long k = 0; k < d_bsiz; k++) p_byte[k] = that.p_byte[k];
 
174
  }
 
175
 
 
176
  // destroy this serial block
 
177
 
 
178
  Serial::Block::~Block (void) {
 
179
    if (d_bsiz > 0) delete [] p_byte;
 
180
  }
 
181
 
 
182
  // assign a serial block to this one
 
183
 
 
184
  Serial::Block& Serial::Block::operator = (const Block& that) {
 
185
    // check for self-assignation
 
186
    if (this == &that) return *this;
 
187
    // clean locally
 
188
    if (d_bsiz > 0) delete[] p_byte;
 
189
    // assign locally
 
190
    d_btyp = that.d_btyp;
 
191
    d_size = that.d_size;
 
192
    d_dsiz = that.d_dsiz;
 
193
    d_bsiz = that.d_bsiz;
 
194
    d_blen = that.d_blen;
 
195
    p_byte = (d_bsiz == 0) ? nilp : new t_byte[d_bsiz];
 
196
    for (long k = 0; k < d_bsiz; k++) p_byte[k] = that.p_byte[k];
 
197
    // done
 
198
    return *this;
 
199
  }
 
200
 
 
201
  // clear the serial block
 
202
 
 
203
  void Serial::Block::clear (void) {
 
204
    d_blen = 0;
 
205
    for (long k = 0; k < d_bsiz; k++) p_byte[k] = nilc;
 
206
  }
 
207
 
 
208
  // return true if the block is empty
 
209
 
 
210
  bool Serial::Block::empty (void) const {
 
211
    return (d_blen == 0);
 
212
  }
 
213
 
 
214
  // return true if the block is full
 
215
 
 
216
  bool Serial::Block::full (void) const {
 
217
    return (d_blen >= d_size);
 
218
  }
 
219
 
 
220
  // return the block length
 
221
 
 
222
  long Serial::Block::length (void) const {
 
223
    return d_blen;
 
224
  }
 
225
 
 
226
  // add a byte to the block
 
227
 
 
228
  void Serial::Block::add (const t_byte bval) {
 
229
    if (d_btyp != BYTE) {
 
230
      throw Exception ("serial-error", "inconsistent serial block type");
 
231
    }
 
232
    if (d_blen >= d_size) {
 
233
      throw Exception ("serial-error", "cannot add in full serial block");
 
234
    }
 
235
    p_byte[d_blen++] = bval;
 
236
  }
 
237
 
 
238
  // get a byte value by index
 
239
 
 
240
  t_byte Serial::Block::getbyte (const long index) const {
 
241
    if (d_btyp != BYTE) {
 
242
      throw Exception ("serial-error", "inconsistent serial block type");
 
243
    }
 
244
    if ((index < 0) || (index >= d_blen)) {
 
245
      throw Exception ("serial-error", "invalid index in serial block");
 
246
    }
 
247
    return p_byte[index];
 
248
  }
 
249
 
 
250
  // add a boolean to the block
 
251
 
 
252
  void Serial::Block::add (const bool bval) {
 
253
    if (d_btyp != BOOL) {
 
254
      throw Exception ("serial-error", "inconsistent serial block type");
 
255
    }
 
256
    if (d_blen >= d_size) {
 
257
      throw Exception ("serial-error", "cannot add in full serial block");
 
258
    }
 
259
    p_byte[d_blen++] = bval ? 0x01 : nilc;
 
260
  }
 
261
 
 
262
  // get a boolean value by index
 
263
 
 
264
  bool Serial::Block::getbool (const long index) const {
 
265
    if (d_btyp != BOOL) {
 
266
      throw Exception ("serial-error", "inconsistent serial block type");
 
267
    }
 
268
    if ((index < 0) || (index >= d_blen)) {
 
269
      throw Exception ("serial-error", "invalid index in serial block");
 
270
    }
 
271
    return (p_byte[index] == nilc) ? false : true;
 
272
  }
 
273
 
 
274
  // add an integer to the block
 
275
 
 
276
  void Serial::Block::add (const t_long lval) {
 
277
    if (d_btyp != LONG) {
 
278
      throw Exception ("serial-error", "inconsistent serial block type");
 
279
    }
 
280
    if (d_blen >= d_size) {
 
281
      throw Exception ("serial-error", "cannot add in full serial block");
 
282
    }
 
283
    c_ohton (lval, &(p_byte[d_blen++ * d_dsiz]));
 
284
  }
 
285
 
 
286
  // get an integer value by index
 
287
 
 
288
  t_long Serial::Block::getlong (const long index) const {
 
289
    if (d_btyp != LONG) {
 
290
      throw Exception ("serial-error", "inconsistent serial block type");
 
291
    }
 
292
    if ((index < 0) || (index >= d_blen)) {
 
293
      throw Exception ("serial-error", "invalid index in serial block");
 
294
    }
 
295
    return c_ontoh (&(p_byte[index * d_dsiz]));
 
296
  }
 
297
 
 
298
  // add a real to the block
 
299
 
 
300
  void Serial::Block::add (const t_real rval) {
 
301
    if (d_btyp != REAL) {
 
302
      throw Exception ("serial-error", "inconsistent serial block type");
 
303
    }
 
304
    if (d_blen >= d_size) {
 
305
      throw Exception ("serial-error", "cannot add in full serial block");
 
306
    }
 
307
    c_rhton (rval, &(p_byte[d_blen++ * d_dsiz]));
 
308
  }
 
309
 
 
310
  // add a real point to the block
 
311
 
 
312
  void Serial::Block::add (const t_long xval, const t_long yval, 
 
313
                           const t_real rval) {
 
314
    if (d_btyp != RPT2) {
 
315
      throw Exception ("serial-error", "inconsistent serial block type");
 
316
    }
 
317
    if (d_blen >= d_size) {
 
318
      throw Exception ("serial-error", "cannot add in full serial block");
 
319
    }
 
320
    // add the x coordinate
 
321
    c_ohton (xval, &(p_byte[d_blen * d_dsiz]));
 
322
    // add the y coordinate
 
323
    c_ohton (yval, &(p_byte[(d_blen * d_dsiz) + sizeof (t_long)]));
 
324
    // add the real value
 
325
    c_rhton (rval, &(p_byte[(d_blen++ * d_dsiz) + (2 * sizeof (t_long))]));
 
326
  }
 
327
 
 
328
  // get a real value by index
 
329
 
 
330
  t_real Serial::Block::getreal (const long index) const {
 
331
    // check index
 
332
    if ((index < 0) || (index >= d_blen)) {
 
333
      throw Exception ("serial-error", "invalid index in serial block");
 
334
    }
 
335
    // check for real
 
336
    if (d_btyp == REAL) 
 
337
      return c_ontor (&(p_byte[index * d_dsiz]));
 
338
    // must be 
 
339
    if (d_btyp == RPT2) 
 
340
      return c_ontor (&(p_byte[(index * d_dsiz) + (2 * sizeof (t_long))]));
 
341
    // invalid type
 
342
    throw Exception ("serial-error", "inconsistent serial block type");
 
343
  }
 
344
 
 
345
  // get the block serial code
 
346
 
 
347
  t_byte Serial::Block::serialid (void) const {
 
348
    t_byte sid = nilc;
 
349
    switch (d_btyp) {
 
350
    case BYTE:
 
351
      sid = SERIAL_BBLK_ID;
 
352
      break;
 
353
    case BOOL:
 
354
      sid = SERIAL_BOOL_ID;
 
355
      break;
 
356
    case LONG:
 
357
      sid = SERIAL_LBLK_ID;
 
358
      break;
 
359
    case REAL:
 
360
      sid = SERIAL_RBLK_ID;
 
361
      break;
 
362
    case RPT2:
 
363
      sid = SERIAL_RPT2_ID;
 
364
      break;
 
365
    }
 
366
    return sid;
 
367
  }
 
368
 
 
369
  // serialize this block
 
370
 
 
371
  void Serial::Block::wrstream (OutputStream& os) const {
 
372
    // write size and length
 
373
    Serial::wrlong (d_size, os);
 
374
    Serial::wrlong (d_blen, os);
 
375
    // write the byte array
 
376
    if (d_size > 0) {
 
377
      long bsiz = os.write ((char*) p_byte, d_bsiz);
 
378
      if (bsiz != d_bsiz) {
 
379
        throw Exception ("serial-error", "inconsistent size in serial block");
 
380
      }
 
381
    }      
 
382
  }
 
383
 
 
384
  // deserialize this block
 
385
 
 
386
  void Serial::Block::rdstream (InputStream& is) {
 
387
    // read size and length
 
388
    d_dsiz = 0;
 
389
    d_size = Serial::rdlong (is);
 
390
    d_dsiz = get_block_dsiz (d_btyp);
 
391
    d_blen = Serial::rdlong (is);
 
392
    d_bsiz = d_size * d_dsiz;
 
393
    // allocate the byte array
 
394
    if (d_bsiz > 0) {
 
395
      // allocate the array
 
396
      p_byte = new t_byte[d_bsiz];
 
397
      // get the array from the stream
 
398
      long bsiz = is.copy ((char*) p_byte, d_bsiz);
 
399
      if (bsiz != d_bsiz) {
 
400
        throw Exception ("serial-error", "inconsistent size in serial block");
 
401
      }
 
402
    } else {
 
403
      p_byte = nilp;
 
404
    }
 
405
  }
 
406
 
79
407
  // -------------------------------------------------------------------------
80
408
  // - public section                                                        -
81
409
  // -------------------------------------------------------------------------
100
428
    case SERIAL_BYTE_ID:
101
429
      return new Byte;
102
430
      break;
 
431
    case SERIAL_EOSC_ID:
 
432
      return nilp ;
 
433
      break;
103
434
    case SERIAL_INTG_ID:
104
435
      return new Integer;
105
436
      break;
148
479
    case SERIAL_PTBL_ID:
149
480
      return new PrintTable;
150
481
      break;
 
482
    case SERIAL_MESG_ID:
 
483
      return new Message;
 
484
      break;
 
485
    case SERIAL_BBLK_ID:
 
486
    case SERIAL_LBLK_ID:
 
487
    case SERIAL_RBLK_ID:
 
488
      throw Exception ("serial-error", "cannot map internal serial block");
 
489
      break;
151
490
    default:
152
491
      break;
153
492
    }
176
515
    os.write ((char) SERIAL_NILP_ID);
177
516
  }
178
517
 
 
518
  // serialize a boolean to an output stream
 
519
 
 
520
  void Serial::wrbool (const bool value, class OutputStream& os) {
 
521
    Boolean bobj (value);
 
522
    bobj.wrstream (os);
 
523
  }
 
524
 
 
525
  // serialize a boolean array with a block
 
526
  
 
527
  void Serial::wrbool (const long size, const bool* data, OutputStream& os) {
 
528
    // check for nil
 
529
    if (size == 0L) return;
 
530
    // create an operating block
 
531
    Serial::Block blok (SRL_BLOK_SIZ, Serial::Block::BOOL);
 
532
    for (long k = 0L; k < size; k++) {
 
533
      blok.add (data[k]);
 
534
      if (blok.full () == false) continue;
 
535
      Serial::wrblok (blok, os);
 
536
      blok.clear ();
 
537
    }
 
538
    if (blok.empty () == false) Serial::wrblok (blok, os);      
 
539
  }
 
540
 
 
541
  // deserialize a boolean
 
542
 
 
543
  bool Serial::rdbool (InputStream& is) {
 
544
    Boolean bobj;
 
545
    bobj.rdstream (is);
 
546
    return bobj.tobool ();
 
547
  }
 
548
 
 
549
  // deserialize a boolean array
 
550
 
 
551
  bool* Serial::rdbool (InputStream& is, const long size) {
 
552
    // check for nill
 
553
    if (size == 0L) return nilp;
 
554
    // create a data block
 
555
    bool* result = new bool[size];
 
556
    try {
 
557
      // read the data block
 
558
      for (long i = 0; i < size; i++) {
 
559
        Block blok = Serial::rdblok (is);
 
560
        long  blen = blok.length ();
 
561
        for (long k = 0; k < blen; k++) result[i+k] = blok.getbool (k);
 
562
        i += (blen - 1);
 
563
      }
 
564
      return result;
 
565
    } catch (...) {
 
566
      delete [] result;
 
567
      throw;
 
568
    }
 
569
  }
 
570
 
 
571
  // serialize a character to an output stream
 
572
 
 
573
  void Serial::wrchar (const t_quad value, class OutputStream& os) {
 
574
    Character cobj (value);
 
575
    cobj.wrstream (os);
 
576
  }
 
577
 
 
578
  // deserialize a boolean
 
579
 
 
580
  t_quad Serial::rdchar (InputStream& is) {
 
581
    Character cobj;
 
582
    cobj.rdstream (is);
 
583
    return cobj.toquad ();
 
584
  }
 
585
 
 
586
  // serialize an integer to an output stream
 
587
 
 
588
  void Serial::wrlong (const t_long value, OutputStream& os) {
 
589
    Integer iobj (value);
 
590
    iobj.wrstream (os);
 
591
  }
 
592
 
 
593
  // serialize an integer array with a block
 
594
  
 
595
  void Serial::wrlong (const long size, const long* data, OutputStream& os) {
 
596
    // check for nil
 
597
    if (size == 0L) return;
 
598
    // create an operating block
 
599
    Serial::Block blok (SRL_BLOK_SIZ, Serial::Block::LONG);
 
600
    for (long k = 0L; k < size; k++) {
 
601
      blok.add ((t_long) data[k]);
 
602
      if (blok.full () == false) continue;
 
603
      Serial::wrblok (blok, os);
 
604
      blok.clear ();
 
605
    }
 
606
    if (blok.empty () == false) Serial::wrblok (blok, os);      
 
607
  }
 
608
 
 
609
  // deserialize an integer
 
610
  
 
611
  t_long Serial::rdlong (InputStream& is) {
 
612
    Integer iobj;
 
613
    iobj.rdstream (is);
 
614
    return iobj.tolong ();
 
615
  }
 
616
 
 
617
  // deserialize an integer array
 
618
 
 
619
  long* Serial::rdlong (InputStream& is, const long size) {
 
620
    // check for nill
 
621
    if (size == 0L) return nilp;
 
622
    // create a data block
 
623
    long* result = new long[size];
 
624
    try {
 
625
      // read the data block
 
626
      for (long i = 0; i < size; i++) {
 
627
        Block blok = Serial::rdblok (is);
 
628
        long  blen = blok.length ();
 
629
        for (long k = 0; k < blen; k++) result[i+k] = blok.getlong (k);
 
630
        i += (blen - 1);
 
631
      }
 
632
      return result;
 
633
    } catch (...) {
 
634
      delete [] result;
 
635
      throw;
 
636
    }
 
637
  }
 
638
 
 
639
  // serialize a real to an output stream
 
640
 
 
641
  void Serial::wrreal (const t_real value, OutputStream& os) {
 
642
    Real robj (value);
 
643
    robj.wrstream (os);
 
644
  }
 
645
 
 
646
  // serialize a real array with a block
 
647
  
 
648
  void Serial::wrreal (const long size, const t_real* data, OutputStream& os) {
 
649
    // check for nil
 
650
    if (size == 0L) return;
 
651
    // create an operating block
 
652
    Serial::Block blok (SRL_BLOK_SIZ, Serial::Block::REAL);
 
653
    for (long k = 0L; k < size; k++) {
 
654
      blok.add (data[k]);
 
655
      if (blok.full () == false) continue;
 
656
      Serial::wrblok (blok, os);
 
657
      blok.clear ();
 
658
    }
 
659
    if (blok.empty () == false) Serial::wrblok (blok, os);      
 
660
  }
 
661
 
 
662
  // deserialize a real
 
663
  
 
664
  t_real Serial::rdreal (InputStream& is) {
 
665
    Real robj;
 
666
    robj.rdstream (is);
 
667
    return robj.toreal ();
 
668
  }
 
669
 
 
670
  // deserialize a real data array
 
671
 
 
672
  t_real* Serial::rdreal (InputStream& is, const long size) {
 
673
    // check for nill
 
674
    if (size == 0L) return nilp;
 
675
    // create a data block
 
676
    t_real* result = new t_real[size];
 
677
    try {
 
678
      // read the data block
 
679
      for (long i = 0; i < size; i++) {
 
680
        Block blok = Serial::rdblok (is);
 
681
        long  blen = blok.length ();
 
682
        for (long k = 0; k < blen; k++) result[i+k] = blok.getreal (k);
 
683
        i += (blen - 1);
 
684
      }
 
685
      return result;
 
686
    } catch (...) {
 
687
      delete [] result;
 
688
      throw;
 
689
    }
 
690
  }
 
691
 
 
692
  // serialize a block to an output stream
 
693
 
 
694
  void Serial::wrblok (const Block& blok, OutputStream& os) {
 
695
    // write the serial id
 
696
    os.write ((char) blok.serialid ());
 
697
    // serialize the object
 
698
    blok.wrstream (os);
 
699
  }
 
700
 
 
701
  // deserialize a block
 
702
 
 
703
  Serial::Block Serial::rdblok (InputStream& is) {
 
704
    // get a block by serial id
 
705
    t_byte sid = is.read ();
 
706
    Serial::Block blok (sid);
 
707
    // read in the blok
 
708
    blok.rdstream (is);
 
709
    return blok;
 
710
  }
 
711
 
179
712
  // -------------------------------------------------------------------------
180
713
  // - class section                                                         -
181
714
  // -------------------------------------------------------------------------