File: textPersistenceManager.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (587 lines) | stat: -rw-r--r-- 12,102 bytes parent folder | download | duplicates (9)
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: textPersistenceManager.C,v 1.20 2005/02/06 09:45:00 oliver Exp $
//

#include <BALL/CONCEPT/textPersistenceManager.h>

// #define BALL_DEBUG_PERSISTENCE

using namespace std;

namespace BALL 
{

	const char* TextPersistenceManager::INDENT_STRING = "                                                                                                       ";
	const Size TextPersistenceManager::MAX_INDENT = (Size)strlen(TextPersistenceManager::INDENT_STRING);

	TextPersistenceManager::TextPersistenceManager()
		:	PersistenceManager(),
			indent_depth_(0)
	{
	}

	TextPersistenceManager::TextPersistenceManager(istream& is)
		:	PersistenceManager(is),
			indent_depth_(0)
	{
	}

	TextPersistenceManager::TextPersistenceManager(ostream& os)
		:	PersistenceManager(os),
			indent_depth_(0)
	{
	}

	TextPersistenceManager::TextPersistenceManager(istream& is, ostream& os)
		:	PersistenceManager(is, os),
			indent_depth_(0)
	{
	}

	bool TextPersistenceManager::expect(const String& value)
	{
		static String s;
		*istr_ >> s;
		if ((s != value) && (istr_->good()))
		{
			Log.error() << "textPersistenceManager::expect: " 
				<< " at position " << istr_->tellg() << ": read " << s << ", expected " << value << endl;
		}
		
		return (s == value) && (istr_->good());
	}

	const char* TextPersistenceManager::indent() 
	{
		return &INDENT_STRING[MAX_INDENT - std::min(MAX_INDENT, 2 * indent_depth_)];
	}
			
  void TextPersistenceManager::writeHeader
		(const char* type_name, const char* name, LongSize ptr)
  {
    *ostr_ << indent();

    if (name == 0)
    {
      *ostr_ << "BASEOBJECT ";
		} 
		else 
		{
      *ostr_ << "OBJECT ";
		}

    *ostr_ << type_name << " @ " << dec << ptr;
    if ((name != 0) && (*name != (char)0))
    {
      *ostr_ << " " << name;
		} 
		else 
		{
      *ostr_ << " -";
		}

    *ostr_ << endl;

    indent_depth_++;
	}
 
	void TextPersistenceManager::writeTrailer(const char* name)
	{
		// decrease indentation by one column
		indent_depth_--;
		*ostr_ << indent();
		
		// and write the object/base object trailer
		if (name == 0) 
		{
			*ostr_ << "END_BASEOBJECT" << endl;
		} 
		else 
		{
			*ostr_ << "END_OBJECT" << endl;
		}
	}

	void TextPersistenceManager::writeStreamHeader()
	{
		// write a start tag
		*ostr_ << "START" << endl;
	}

	bool TextPersistenceManager::checkStreamHeader()
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkStreamHeader()" << endl;
#		endif

		// read a string from the input stream
		String s;
		*istr_ >> s;

		// and check whether it is the start tag we expected
		return (s == "START");
	}

	void TextPersistenceManager::writeStreamTrailer()
	{
		// write a END trailer to the stream
		*ostr_ << "END" << endl;
	}

	bool TextPersistenceManager::checkStreamTrailer()
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkStreamTrailer()" << endl;
#		endif

		// read the next string from the stream
		String s;
		*istr_ >> s;

		// and check for the END tag
		return (s == "END");
	}

	bool TextPersistenceManager::checkTrailer(const char* name) 
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			if (name != 0)
			{
				Log.info() << "entering checkTrailer(" << name << ")" << endl;
			} 
			else 
			{
				Log.info() << "entering checkTrailer()" << endl;
			}
#		endif

		// check for the object or base object trailer
		if (name == 0) 
		{
			return expect("END_BASEOBJECT");
		} 
		else 
		{
			return expect("END_OBJECT");
		}
	}

	bool TextPersistenceManager::checkHeader(const char* type_name, const char* name, LongSize& ptr)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			if (name == 0)
			{
				Log.info() << "entering checkHeader(" << type_name << ", 0)" << endl;
			} 
			else 
			{
				Log.info() << "entering checkHeader(" << type_name << ", " << name << ")" << endl;
			}
#		endif
		// check for the header keyword OBJECT/BASEOBJECT
		if (name == 0) 
		{
			if (!expect("BASEOBJECT"))
			{
				// abort immediately if not found
				return false;	
			}
		} 
		else 
		{
			if (!expect("OBJECT"))
			{
				// abort immediately if not found
				return false;
			}
		}

		// read the next string. it should contain the
		// type name ofthe object
		String s;
		*istr_ >> s;

		// compare it with the type we expected
		if (s != type_name) 
		{
#			ifdef BALL_DEBUG_PERSISTENCE
				Log.error() << "Expected object of type " << type_name 
										<< " but found definition for " << s << "!" << endl;
#			endif

			// we didn't find the right object - abort
			return false;
		}

		// abort if we cannot read an @ sign 
		// (indicating the pointer that follows)
		if (!expect("@"))
		{
			return false;
		}
		
		// read the object pointer
		get(ptr);
		if (ptr == 0) 
		{
#			ifdef BALL_DEBUG_PERSISTENCE
				Log.error() << "Could not read a valid object pointer: " 
										<< dec << (LongSize)ptr << "!" << endl;
#			endif

			// abort if it was not a valid pointer
			return false;
		}

		// if we check for a baseclass, name should be 0
		if (name == 0)
		{
			if (!expect("-"))
			{
				return false;
			}
		} 
		else 
		{
			// since an empty object name cannot be read
			// as a string, we convert it to a "-"
			if (!strcmp(name, ""))
			{
				if (!expect("-"))
				{
					return false;
				}
			} 
			else if (!expect(name))
			{
				// it the object name is incorrect - abort
				return false;
			}
		}

		// everything went well - we read a complete object header
		return true;
	}

	bool TextPersistenceManager::getObjectHeader(String& type_name, LongSize& ptr)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering getObjectHeader()" << endl;
#		endif

		if (!expect("OBJECT"))
		{
			return false;
		}

		*istr_ >> type_name;
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "read type name: " << type_name << endl;
#		endif

		// retrieve the 
		if (!expect("@"))
		{
			return false;
		}
		
		get(ptr);
		if (ptr == 0) 
		{
#			ifdef BALL_DEBUG_PERSISTENCE
				Log.error() << "Could not read a valid object pointer: " 
										<< dec << (LongSize)ptr << "!" << endl;
#			endif

			return false;
		} 
#		ifdef BALL_DEBUG_PERSISTENCE
		else 
		{
			Log.info() << "Read pointer: " << ptr << endl;
		}
#		endif

		return expect("-");
	}

	void TextPersistenceManager::writeName(const char* name)
	{
		if ((name == 0) || !strcmp(name, ""))
		{
			*ostr_ << " - ";
		} 
		else 
		{
			*ostr_ << ' ' << name << ' ';
		}
	}

	bool TextPersistenceManager::checkName(const char* name)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkName()" << endl;
#endif

		String s;
		*istr_ >> s;
			
		if ((name == 0) || !strcmp(name, ""))
		{
			return (s == "-");
		} 
		
		return (s == name);
	}

	void TextPersistenceManager::writeStorableHeader(const char* type_name, const char* name)
	{
		*ostr_ << indent() << "STORABLE";
		writeName(type_name);
		writeName(name);
		*ostr_ << endl;
		++indent_depth_;
	}

	bool TextPersistenceManager::checkStorableHeader(const char* type_name, const char* name)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkStorableHeader()" << endl;
#		endif

		return (expect("STORABLE") && checkName(type_name) && checkName(name));
	}

	void TextPersistenceManager::writeStorableTrailer()
	{
		// decrease indentation by one column and write a trailer string
		--indent_depth_;
		*ostr_ << indent() << "END_STORABLE" << endl;
	}

	bool TextPersistenceManager::checkStorableTrailer()
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkStorableTrailer()" << endl;
#		endif

		// check for the trailer "END_STORABLE"
		return expect("END_STORABLE");
	}

	void TextPersistenceManager::writePrimitiveHeader(const char* type_name, const char* name)
	{
		*ostr_ << indent() << type_name;
		writeName(name);
		*ostr_ << "=";
	}

	bool TextPersistenceManager::checkPrimitiveHeader(const char* type_name, const char* name)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkPrimitiveHeader(" << name << ")" << endl;
#		endif

		if (!strcmp(name, ""))
		{
			return (expect(type_name) && expect("-") && expect("="));
		} 
		else 
		{
			return (expect(type_name) && expect(name) && expect("="));
		}
	}

	void TextPersistenceManager::writePrimitiveTrailer()
	{
		*ostr_ << endl;
	}

	bool TextPersistenceManager::checkPrimitiveTrailer()
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkPrimitiveTrailer()" << endl;
#		endif

		return true;
	}

	void TextPersistenceManager::writeObjectPointerHeader(const char* type_name, const char* name)
	{
		*ostr_ << indent() << type_name << " *";
		writeName(name);
	}
	
	bool TextPersistenceManager::checkObjectPointerHeader(const char* type_name, const char* name)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkObjectPointerHeader()" << endl;
#		endif

		return (checkName(type_name) && expect("*") && checkName(name));
	}
	
	void TextPersistenceManager::writeObjectReferenceHeader(const char* type_name, const char* name)
	{
		*ostr_ << indent() << type_name << " &";
		writeName(name);
	}
	
	bool TextPersistenceManager::checkObjectReferenceHeader(const char* type_name, const char* name)
	{
#		ifdef BALL_DEBUG_PERSISTENCE
			Log.info() << "entering checkObjectReferenceHeader()" << endl;
#		endif

		return (checkName(type_name) && expect("&") && checkName(name));
	}
	
	void TextPersistenceManager::writeObjectPointerArrayHeader(const char* type_name, const char* name, Size size)
	{
		*ostr_ << indent() << type_name;
		writeName(name);
		*ostr_ << " [";
		put(size);
		*ostr_ << "] = {";
	}

	bool TextPersistenceManager::checkObjectPointerArrayHeader(const char* type_name, const char* name, Size& size)
	{
		size = 0;
		// check for the first part of the header
		if (!(checkName(type_name) && checkName(name) && expect("[")))
		{
			return false;
		}
		
		// read the number of array elements
		get(size);
		
		// read the rest of the header
		return (expect("]") && expect("=") && expect("{"));
	}
		
	void TextPersistenceManager::writeObjectPointerArrayTrailer()
	{
		*ostr_ << " }" << endl;
	}
			
	bool TextPersistenceManager::checkObjectPointerArrayTrailer()
	{
		return expect("}");
	}

	/// Layer 0: primitive put methods
	void TextPersistenceManager::put(const char c)
	{
		*ostr_ << ' ' << (int)c << ' ';
	}

	void TextPersistenceManager::put(const Byte c)
	{
		*ostr_ << ' ' << (int)c << ' ';
	}

	void TextPersistenceManager::put(const bool b)
	{	
		// write a boolean value as TRUE or FALSE
		if (b)
		{
			*ostr_ << " TRUE ";
		} 
		else 
		{
			*ostr_ << " FALSE ";
		}
	}
	
// define a macro for all numeric types
// just write it to a stream...
#define BALL_DEFINE_NUMBER_PUT(type)\
	void TextPersistenceManager::put(const type i)\
	{\
		*ostr_ << ' ' << i << ' ';\
	}\

	BALL_DEFINE_NUMBER_PUT(Size)
	BALL_DEFINE_NUMBER_PUT(Index)
	BALL_DEFINE_NUMBER_PUT(LongSize)
	BALL_DEFINE_NUMBER_PUT(Real)
	BALL_DEFINE_NUMBER_PUT(DoubleReal)

	void TextPersistenceManager::put(const string& s)
	{
		// strings are prefixed by their size
		*ostr_ << ' ' << s.size() << ' ';

		// then we write each characters sequentially
		for (Size i = 0; i < s.size(); i++)
		{
			ostr_->put(s[i]);
		}

		// and terminate everything with a blank
		*ostr_ << ' ';
	}

	void TextPersistenceManager::get(char& c)
	{
		int i;
		*istr_ >> i;
		c = (char)i;
	}

	void TextPersistenceManager::get(Byte& c)
	{
		int i;
		*istr_ >> i;
		c = (char)i;
	}

	void TextPersistenceManager::get(bool& b)
	{
		String s;
		*istr_ >> s;
		b = (s == "TRUE");
	}

	void TextPersistenceManager::get(string& s)
	{
		// read the number of characters
		int i;
		*istr_ >> i;

		// read the leading blank
		char c;	
		istr_->get(c);

		// clear the string
		s.erase();
		
		// read i characters into the string
		if (i > 0)
		{
			for (; i > 0; i--)
			{
				istr_->get(c);
				s += c;
			}
		}
	}

#define BALL_DEFINE_NUMBER_GET(type)\
	void TextPersistenceManager::get(type& i)\
	{\
		*istr_ >> i;\
	}\


	BALL_DEFINE_NUMBER_GET(Index)
	BALL_DEFINE_NUMBER_GET(Size)
	BALL_DEFINE_NUMBER_GET(LongSize)
	BALL_DEFINE_NUMBER_GET(Real)
	BALL_DEFINE_NUMBER_GET(DoubleReal)

} // namespace BALL