File: iostreamb.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (489 lines) | stat: -rw-r--r-- 12,227 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
 * Copyright (c) 1991 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Stanford and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Stanford and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

#include <Dispatch/iostreamb.h>

// Make sure that these assumptions about the sizes of integral types
// hold for your machine.  If your machine does not have 32-bit and
// 16-bit integers, then you cannot use binary I/O because other
// machines will expect the integers to be these sizes.  If your
// machine does not have 8-bit characters, then I don't know how
// you're going to communicate with other machines!

typedef long		INT32;
typedef short		INT16;

typedef unsigned long	UINT32;
typedef unsigned short	UINT16;

// Assume the peer machine has the same endian architecture but
// disable binary I/O by default anyway.  The two machines should
// always negotiate these two options before enabling binary I/O.

iosb::iosb() :
    _binary(false),
    _swapped(false) {}

iosb::~iosb() {}

// Get and set iosb's extra state information.  Verify that the
// assumptions about sizes of integral types hold before enabling
// binary I/O; if they do not hold, then refuse to enable binary I/O.

boolean iosb::binary() const {
    return _binary;
}

void iosb::binary(boolean binary) {
    if (binary) {
	// don't use consts; cfront has wrong idea of their values
	UINT32 max32 = ~0;
	UINT16 max16 = ~0;

	if (max32 != 0xffffffff || max16 != 0xffff) {
	    binary = false;
	}
    }
    _binary = binary;
}

boolean iosb::swapped() const {
    return _swapped;
}

void iosb::swapped(boolean swapped) {
    _swapped = swapped;
}

// Provide a constructor for derived classes to use and a constructor
// for public use.

istreamb::istreamb() {}

istreamb::istreamb(streambuf* b) {
    init(b);
}

istreamb::~istreamb() {}

// Redefine these functions to extract binary data where possible for
// faster I/O throughput and to discard delimiters automatically.
// Binary integral values occupy the length implied by their type and
// already fit this machine's endian architecture.  Formatted integral
// and floating point values end in a blank character that is
// extracted.  Strings end in a null character that is extracted.

istreamb& istreamb::operator>>(char& c) {
    get(c);			// assume c is 8 bits long on all machines
    return *this;
}

istreamb& istreamb::operator>>(unsigned char& uc) {
    get(uc);			// assume uc is 8 bits long on all machines
    return *this;
}

istreamb& istreamb::operator>>(short& s) {
    if (_binary) {
	INT16 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    s = (short)value;
	    if (s != value) {
		setstate(ios::failbit); // overflow: value won't fit in short
	    }
	}
    } else {
	istream::operator>>(s);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(unsigned short& us) {
    if (_binary) {
	UINT16 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    us = (unsigned short)value;
	    if (us != value) {
		setstate(ios::failbit); // overflow: value won't fit in ushort
	    }
	}
    } else {
	istream::operator>>(us);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(int& i) {
    if (_binary) {
	INT32 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    i = (int)value;
	    if (i != value) {
		setstate(ios::failbit); // overflow: value won't fit in int
	    }
	}
    } else {
	istream::operator>>(i);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(unsigned int& ui) {
    if (_binary) {
	UINT32 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    ui = (unsigned int)value;
	    if (ui != value) {
		setstate(ios::failbit); // overflow: value won't fit in uint
	    }
	}
    } else {
	istream::operator>>(ui);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(long& l) {
    if (_binary) {
	INT32 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    l = (long)value;
	    if (l != value) {
		setstate(ios::failbit); // overflow: value won't fit in long
	    }
	}
    } else {
	istream::operator>>(l);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(unsigned long& ul) {
    if (_binary) {
	UINT32 value;
	read((char*)&value, sizeof(value));
	if (good()) {
	    ul = (unsigned long)value;
	    if (ul != value) {
		setstate(ios::failbit); // overflow: value won't fit in ulong
	    }
	}
    } else {
	istream::operator>>(ul);
	get();
    }
    return *this;
}

istreamb& istreamb::operator>>(float& f) {
    istream::operator>>(f);
    get();
    return *this;
}

istreamb& istreamb::operator>>(double& d) {
    istream::operator>>(d);
    get();
    return *this;
}

istreamb& istreamb::operator>>(char* p) {
    const int MAXINT = (int)(((unsigned)-1) >> 1);
    const int w = width(0);
    getline(p, w ? w : MAXINT, '\0');
    if (w && gcount() == w - 1) {
	setstate(ios::failbit);
    }
    return *this;
}

istreamb& istreamb::operator>>(unsigned char* up) {
    const int MAXINT = (int)(((unsigned)-1) >> 1);
    const int w = width(0);
    getline(up, w ? w : MAXINT, '\0');
    if (w && gcount() == w - 1) {
	setstate(ios::failbit);
    }
    return *this;
}

// Redefine the rest of the overloaded operator>> functions that we
// want to keep in the derived class.

istreamb& istreamb::operator>>(istream& (*f)(istream&)) {
    istream::operator>>(f);
    return *this;
}

istreamb& istreamb::operator>>(ios& (*f)(ios&)) {
    istream::operator>>(f);
    return *this;
}

// Provide a constructor for derived classes to use and a constructor
// for public use.

ostreamb::ostreamb() {}

ostreamb::ostreamb(streambuf* b) {
    init(b);
}

ostreamb::~ostreamb() {}

// Decrement the width by one to take into account the delimiter
// character that all the inserters insert after a formatted value.

inline void ostreamb::fixwidth() {
    register int w = width();
    if (w) {
	width(w - 1);
    }
}

// Redefine these functions to insert binary data where possible for
// faster I/O throughput and to delimit formatted data automatically.
// Binary integral values will occupy the length implied by their type
// and fit the peer's endian architecture.  Formatted integral and
// floating point values will end in a blank character.  Strings will
// end in a null character.  You need not, and should not, insert
// explicit delimiters.

ostreamb& ostreamb::operator<<(char c) {
    put(c);			// assume c is 8 bits long on all machines
    return *this;
}

ostreamb& ostreamb::operator<<(unsigned char uc) {
    put(uc);			// assume uc is 8 bits long on all machines
    return *this;
}

ostreamb& ostreamb::operator<<(short s) {
    if (_binary) {
	INT16 value = (INT16)s;
	if (value != s) {
	    setstate(ios::failbit); // overflow: number won't fit in 16 bits
	}
	else if (_swapped) {
	    INT16 copy = value;
	    ((char*)&value)[0] = ((char*)&copy)[1];
	    ((char*)&value)[1] = ((char*)&copy)[0];
	}
	width(0);
	write((char*)&value, sizeof(value));
    }
    else {
	fixwidth();
	ostream::operator<<(s);
	put(' ');
    }
    return *this;
}

ostreamb& ostreamb::operator<<(unsigned short us) {
    if (_binary) {
	UINT16 value = (UINT16)us;
	if (value != us) {
	    setstate(ios::failbit); // number won't fit
	}
	else if (_swapped) {
	    UINT16 copy = value;
	    ((char*)&value)[0] = ((char*)&copy)[1];
	    ((char*)&value)[1] = ((char*)&copy)[0];
	}
	width(0);
	write((char*)&value, sizeof(value));
    }
    else {
	fixwidth();
	ostream::operator<<(us);
	put(' ');
    }
    return *this;
}

ostreamb& ostreamb::operator<<(int i) {
    return *this << (long)i;
}

ostreamb& ostreamb::operator<<(unsigned int ui) {
    return *this << (unsigned long)ui;
}

ostreamb& ostreamb::operator<<(long l) {
    if (_binary) {
	INT32 value = (INT32)l;
	if (value != l) {
	    setstate(ios::failbit); // overflow: number won't fit in 32 bits
	}
	else if (_swapped) {
	    INT32 copy = value;
	    ((char*)&value)[0] = ((char*)&copy)[3];
	    ((char*)&value)[1] = ((char*)&copy)[2];
	    ((char*)&value)[2] = ((char*)&copy)[1];
	    ((char*)&value)[3] = ((char*)&copy)[0];
	}
	width(0);
	write((char*)&value, sizeof(value));
    }
    else {
	fixwidth();
	ostream::operator<<(l);
	put(' ');
    }
    return *this;
}

ostreamb& ostreamb::operator<<(unsigned long ul) {
    if (_binary) {
	UINT32 value = (UINT32)ul;
	if (value != ul) {
	    setstate(ios::failbit); // overflow: number won't fit in 32 bits
	}
	else if (_swapped) {
	    UINT32 copy = value;
	    ((char*)&value)[0] = ((char*)&copy)[3];
	    ((char*)&value)[1] = ((char*)&copy)[2];
	    ((char*)&value)[2] = ((char*)&copy)[1];
	    ((char*)&value)[3] = ((char*)&copy)[0];
	}
	width(0);
	write((char*)&value, sizeof(value));
    }
    else {
	fixwidth();
	ostream::operator<<(ul);
	put(' ');
    }
    return *this;
}

ostreamb& ostreamb::operator<<(float f) {
    fixwidth();
    ostream::operator<<(f);
    put(' ');
    return *this;
}

ostreamb& ostreamb::operator<<(double d) {
    fixwidth();
    ostream::operator<<(d);
    put(' ');
    return *this;
}

ostreamb& ostreamb::operator<<(const char* p) {
    fixwidth();
    ostream::operator<<(p);
    put('\0');
    return *this;
}

ostreamb& ostreamb::operator<<(const unsigned char* up) {
    fixwidth();
    ostream::operator<<((const char*)up); // 2.0 ostream omitted unsigned char*
    put('\0');
    return *this;
}

// Redefine the rest of the overloaded operator<< functions that we
// want to keep in the derived class.

ostreamb& ostreamb::operator<<(ostream& (*f)(ostream&)) {
    ostream::operator<<(f);
    return *this;
}

ostreamb& ostreamb::operator<<(ios& (*f)(ios&)) {
    ostream::operator<<(f);
    return *this;
}

// Provide a constructor for derived classes to use and a constructor
// for public use.

iostreamb::iostreamb() {}

iostreamb::iostreamb(streambuf* b) {
    init(b);
}

iostreamb::~iostreamb() {}

// Negotiate the option of binary I/O with the remote iostreamb.  If
// binary I/O is set, compare endians to enable swapping if necessary.
// Ordinarily we don't need an explicit flush when we're switching
// from insertion to extraction, but we do here because the first
// underflow may read both format and remoteEndian, thus preventing
// underflow from being called again and flushing localEndian.  Tying
// the stream to itself won't work either because 1) ipfx won't call
// flush if remoteEndian is already available, and 2) opfx will make
// the stream flush itself before every insertion because it doesn't
// check for streams tied to themselves.  Sigh....

void iostreamb::negotiate(boolean b) {
    if (!good()) {
	return;
    }

    iosb::binary(b);
    char format = iosb::binary() ? 'T' : 'F';

    *this << format; flush();
    *this >> format;

    if (format != 'T' && format != 'F') {
	setstate(ios::badbit);
    }
    else if (format == 'F') {
	iosb::binary(false);
    }

    if (iosb::binary()) {
	int indian = 1;
	char localEndian = (*(char*)&indian) ? 'l' : 'B';
	char remoteEndian = localEndian;

	*this << localEndian; flush();
	*this >> remoteEndian;

	if (remoteEndian != 'l' && remoteEndian != 'B') {
	    setstate(ios::badbit);
	}
	else if (remoteEndian != localEndian) {
	    swapped(true);
	}
    }
}