84
// get a block data size by type
85
static long get_block_dsiz (const Serial::Block::t_btyp btyp) {
88
case Serial::Block::BYTE:
89
case Serial::Block::BOOL:
90
result = sizeof (t_byte);
92
case Serial::Block::LONG:
93
result = sizeof (t_long);
95
case Serial::Block::REAL:
96
result = sizeof (t_real);
98
case Serial::Block::RPT2:
99
result = 2 * sizeof (t_long) + sizeof (t_real);
105
// -------------------------------------------------------------------------
107
// -------------------------------------------------------------------------
109
// create a default empty block
111
Serial::Block::Block (void) {
120
// create an empty block by sid
122
Serial::Block::Block (const t_byte sid) {
140
throw Exception ("serial-error", "invalid serial type for block");
144
d_dsiz = get_block_dsiz (d_btyp);
150
// create a serial block by size and type
152
Serial::Block::Block (const long size, const t_btyp btyp) {
155
d_size = (size <= 0) ? 0 : size;
156
d_dsiz = get_block_dsiz (d_btyp);
158
d_bsiz = size * d_dsiz;
159
p_byte = (d_bsiz == 0) ? nilp : new t_byte[d_bsiz];
164
// copy construct this serial block
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];
176
// destroy this serial block
178
Serial::Block::~Block (void) {
179
if (d_bsiz > 0) delete [] p_byte;
182
// assign a serial block to this one
184
Serial::Block& Serial::Block::operator = (const Block& that) {
185
// check for self-assignation
186
if (this == &that) return *this;
188
if (d_bsiz > 0) delete[] p_byte;
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];
201
// clear the serial block
203
void Serial::Block::clear (void) {
205
for (long k = 0; k < d_bsiz; k++) p_byte[k] = nilc;
208
// return true if the block is empty
210
bool Serial::Block::empty (void) const {
211
return (d_blen == 0);
214
// return true if the block is full
216
bool Serial::Block::full (void) const {
217
return (d_blen >= d_size);
220
// return the block length
222
long Serial::Block::length (void) const {
226
// add a byte to the block
228
void Serial::Block::add (const t_byte bval) {
229
if (d_btyp != BYTE) {
230
throw Exception ("serial-error", "inconsistent serial block type");
232
if (d_blen >= d_size) {
233
throw Exception ("serial-error", "cannot add in full serial block");
235
p_byte[d_blen++] = bval;
238
// get a byte value by index
240
t_byte Serial::Block::getbyte (const long index) const {
241
if (d_btyp != BYTE) {
242
throw Exception ("serial-error", "inconsistent serial block type");
244
if ((index < 0) || (index >= d_blen)) {
245
throw Exception ("serial-error", "invalid index in serial block");
247
return p_byte[index];
250
// add a boolean to the block
252
void Serial::Block::add (const bool bval) {
253
if (d_btyp != BOOL) {
254
throw Exception ("serial-error", "inconsistent serial block type");
256
if (d_blen >= d_size) {
257
throw Exception ("serial-error", "cannot add in full serial block");
259
p_byte[d_blen++] = bval ? 0x01 : nilc;
262
// get a boolean value by index
264
bool Serial::Block::getbool (const long index) const {
265
if (d_btyp != BOOL) {
266
throw Exception ("serial-error", "inconsistent serial block type");
268
if ((index < 0) || (index >= d_blen)) {
269
throw Exception ("serial-error", "invalid index in serial block");
271
return (p_byte[index] == nilc) ? false : true;
274
// add an integer to the block
276
void Serial::Block::add (const t_long lval) {
277
if (d_btyp != LONG) {
278
throw Exception ("serial-error", "inconsistent serial block type");
280
if (d_blen >= d_size) {
281
throw Exception ("serial-error", "cannot add in full serial block");
283
c_ohton (lval, &(p_byte[d_blen++ * d_dsiz]));
286
// get an integer value by index
288
t_long Serial::Block::getlong (const long index) const {
289
if (d_btyp != LONG) {
290
throw Exception ("serial-error", "inconsistent serial block type");
292
if ((index < 0) || (index >= d_blen)) {
293
throw Exception ("serial-error", "invalid index in serial block");
295
return c_ontoh (&(p_byte[index * d_dsiz]));
298
// add a real to the block
300
void Serial::Block::add (const t_real rval) {
301
if (d_btyp != REAL) {
302
throw Exception ("serial-error", "inconsistent serial block type");
304
if (d_blen >= d_size) {
305
throw Exception ("serial-error", "cannot add in full serial block");
307
c_rhton (rval, &(p_byte[d_blen++ * d_dsiz]));
310
// add a real point to the block
312
void Serial::Block::add (const t_long xval, const t_long yval,
314
if (d_btyp != RPT2) {
315
throw Exception ("serial-error", "inconsistent serial block type");
317
if (d_blen >= d_size) {
318
throw Exception ("serial-error", "cannot add in full serial block");
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))]));
328
// get a real value by index
330
t_real Serial::Block::getreal (const long index) const {
332
if ((index < 0) || (index >= d_blen)) {
333
throw Exception ("serial-error", "invalid index in serial block");
337
return c_ontor (&(p_byte[index * d_dsiz]));
340
return c_ontor (&(p_byte[(index * d_dsiz) + (2 * sizeof (t_long))]));
342
throw Exception ("serial-error", "inconsistent serial block type");
345
// get the block serial code
347
t_byte Serial::Block::serialid (void) const {
351
sid = SERIAL_BBLK_ID;
354
sid = SERIAL_BOOL_ID;
357
sid = SERIAL_LBLK_ID;
360
sid = SERIAL_RBLK_ID;
363
sid = SERIAL_RPT2_ID;
369
// serialize this block
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
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");
384
// deserialize this block
386
void Serial::Block::rdstream (InputStream& is) {
387
// read size and length
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
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");
79
407
// -------------------------------------------------------------------------
80
408
// - public section -
81
409
// -------------------------------------------------------------------------
176
515
os.write ((char) SERIAL_NILP_ID);
518
// serialize a boolean to an output stream
520
void Serial::wrbool (const bool value, class OutputStream& os) {
521
Boolean bobj (value);
525
// serialize a boolean array with a block
527
void Serial::wrbool (const long size, const bool* data, OutputStream& os) {
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++) {
534
if (blok.full () == false) continue;
535
Serial::wrblok (blok, os);
538
if (blok.empty () == false) Serial::wrblok (blok, os);
541
// deserialize a boolean
543
bool Serial::rdbool (InputStream& is) {
546
return bobj.tobool ();
549
// deserialize a boolean array
551
bool* Serial::rdbool (InputStream& is, const long size) {
553
if (size == 0L) return nilp;
554
// create a data block
555
bool* result = new bool[size];
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);
571
// serialize a character to an output stream
573
void Serial::wrchar (const t_quad value, class OutputStream& os) {
574
Character cobj (value);
578
// deserialize a boolean
580
t_quad Serial::rdchar (InputStream& is) {
583
return cobj.toquad ();
586
// serialize an integer to an output stream
588
void Serial::wrlong (const t_long value, OutputStream& os) {
589
Integer iobj (value);
593
// serialize an integer array with a block
595
void Serial::wrlong (const long size, const long* data, OutputStream& os) {
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);
606
if (blok.empty () == false) Serial::wrblok (blok, os);
609
// deserialize an integer
611
t_long Serial::rdlong (InputStream& is) {
614
return iobj.tolong ();
617
// deserialize an integer array
619
long* Serial::rdlong (InputStream& is, const long size) {
621
if (size == 0L) return nilp;
622
// create a data block
623
long* result = new long[size];
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);
639
// serialize a real to an output stream
641
void Serial::wrreal (const t_real value, OutputStream& os) {
646
// serialize a real array with a block
648
void Serial::wrreal (const long size, const t_real* data, OutputStream& os) {
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++) {
655
if (blok.full () == false) continue;
656
Serial::wrblok (blok, os);
659
if (blok.empty () == false) Serial::wrblok (blok, os);
662
// deserialize a real
664
t_real Serial::rdreal (InputStream& is) {
667
return robj.toreal ();
670
// deserialize a real data array
672
t_real* Serial::rdreal (InputStream& is, const long size) {
674
if (size == 0L) return nilp;
675
// create a data block
676
t_real* result = new t_real[size];
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);
692
// serialize a block to an output stream
694
void Serial::wrblok (const Block& blok, OutputStream& os) {
695
// write the serial id
696
os.write ((char) blok.serialid ());
697
// serialize the object
701
// deserialize a block
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);
179
712
// -------------------------------------------------------------------------
180
713
// - class section -
181
714
// -------------------------------------------------------------------------