File: rtstreams.cpp

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (370 lines) | stat: -rwxr-xr-x 7,164 bytes parent folder | download
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
/*
 * rtstreams.cpp
 * 
 * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
 * and Martin Trautmann (martintrautmann@gmx.de) 
 * 
 * This file may be distributed and/or modified under the terms of the 
 * GNU General Public License version 2 as published by the Free Software 
 * Foundation. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 */

/////////// streams.cpp: implementing simple input & output streams
#include "rtstreams.h"
#include "rtcollect.h"
#include "rtstring.h"

#ifdef __SYMBIAN32__
  #include "rtstreams.epoc.cpp"
#else
  #include "rtstreams.stdlib.cpp"
#endif

namespace lrt {

///////////// InputStream /////////////
bool InputStream::markSupported()
{
	return false;
}

void InputStream::mark() {}
void InputStream::reset() {}

int InputStream::read(Array<char> &b, int off, int len)
{
	int numRead = 0;
	for(int i = off; i < off + len; i++)
	{
		int c = read();
		if(c != -1) {
			numRead++;
			b[i] = (char)c;
		}
	}
	if(numRead == 0) numRead = -1;

	return numRead;
}

int InputStream::read(Array<char> &b)
{
	return read(b, 0, b.length());
}

bool InputStream::fail() 
{
	return _fail;
}

void InputStream::close() {}

InputStream::~InputStream() 
{
	close();
}

/// protected ////
InputStream::InputStream() : _fail(false) {}


////////////// FilterInputStream //////////////

FilterInputStream::FilterInputStream(InputStream *in) : in(in), _detach(false)
{
}

bool FilterInputStream::markSupported()
{
	if(in) 
		return in->markSupported();
	else 
		return false;
}

void FilterInputStream::mark()
{
	if(in) in->mark();
}

void FilterInputStream::reset()
{
	if(in) in->reset();
}

int FilterInputStream::read()
{
	if(in) 
		return in->read();
	else 
		return -1;
}

int FilterInputStream::read(Array<char> &b, int off, int len)
{
	if(in)
		return in->read(b, off, len);
	else
		return 0;
}

bool FilterInputStream::fail()
{
	if(in)
		return in->fail();
	else
		return true; 
}

InputStream* FilterInputStream::detach()
{
	_detach = true; 
	return in;
}

bool FilterInputStream::eos()
{
	return in->eos();
}

void FilterInputStream::close()
{
	if(!_detach)
		in->close();
}

FilterInputStream::~FilterInputStream()
{
	close();
	if(!_detach)
		delete in;
}

//////////////////// ParseInputStream /////////////////////////

const String ParseInputStream::whitespace("\r\n\t ");

ParseInputStream::ParseInputStream(InputStream *in, const String &separators, 
	const char commentStart) : FilterInputStream(in), separators(separators), 
	commentStart(commentStart), lineNum(1), markLineNum(1), buf(-1), 
	ignoreComments(false), autoTrim(false), eol(false)
{
	this->separators += commentStart; // make sure that comment char is a separator
}

void ParseInputStream::setIgnoreComments(bool val)
{
	ignoreComments = val;
}

void ParseInputStream::setAutoTrim(bool val)
{
	autoTrim = val;
}

void ParseInputStream::mark()
{
	markLineNum = lineNum;
	FilterInputStream::mark();
}

void ParseInputStream::reset()
{
	FilterInputStream::reset();
	lineNum = markLineNum;
}

int ParseInputStream::read()
{
	if(eol) { lineNum++; eol = false; }
	int ret;
	if(buf >= 0) { ret = buf; buf = -1; }
	else ret = FilterInputStream::read();
	if(ret == '\n') eol = true;
	return ret;
}

//! Can't handle multiple successive unreads! Characters will get lost!
void ParseInputStream::unread(int c)
{
	buf = c;
	if(c == '\n') eol = false;
}

String ParseInputStream::getWord(const String& specialSeps)
{
	const String& separators = (specialSeps == "" ? this->separators : specialSeps);
	String ret;
	bool eof = false;
	bool first = true;
	int c;
	while(true) {
		c = read();
		if(c < 0) { eof = true; break; }
		
		if(first) { // for first char of word, things are a bit different...
			if(c == commentStart) {
				if(!ignoreComments) // return the whole comment as a word
				{ unread(c); return getLine(); }
				else // skip comment: return first word of next line
				{ skipRestOfLine(); return getWord(specialSeps); }
			}
			else {
				ret += (char)c; // separators are allowed as first char
				if(separators.indexOf((char)c) >= 0) break;
			}
			first = false;
		}
		else { // more chars to the word
			if(separators.indexOf((char)c) >= 0) 
			{ unread(c); break; } // break on separator, put it to next word
			ret += (char)c;
		}
	}

	if(autoTrim) {
		ret = ret.trim();
	}
	if(!eof && (ret.length() == 0)) // word is empty => fetch next word
		return getWord(specialSeps);
	else return ret; // return this word
}

String ParseInputStream::getLine(bool skipEmpty)
{
	String ret;
	bool eof = false;
	int c;
	while(true) {
		c = read();
		if(c < 0) { eof = true; break; }
		if(c == '\n') break; // never ever append the newline to the string
		ret += (char)c;
	}
	if(ignoreComments) {
		int commentIndex = ret.lastIndexOf(commentStart);
		if(commentIndex >= 0)
			ret = ret.substring(0, commentIndex);
	}
	if(autoTrim) {
		ret = ret.trim();
	}
	if(skipEmpty && !eof && (ret.length() == 0)) // line is empty => return next line
		return getLine(skipEmpty);
	else return ret;	// return this line
}

int ParseInputStream::getLineNumber()
{
	return lineNum;
}

bool ParseInputStream::skipRestOfLine()
{
	int c;
	while(true) {
		c = read();
		if(c < 0) break;
		if(c == '\n') break;
	}
	return (c >= 0);
}

bool ParseInputStream::eos()
{
	if(buf >= 0) return false;
	buf = read();
	return (buf < 0);
}


//////////////////// OutputStream /////////////////////////////

bool OutputStream::write(const Array<char> &b, int off, int len)
{
	bool ret = true;
	for(int i = off; i < off + len; i++)
		ret &= write(b[i]);
	return ret;
}

bool OutputStream::write(const Array<char> &b)
{
	return write(b, 0, b.length());
}

bool OutputStream::write(const char* c)
{
	return write(String(c));
}

void OutputStream::flush() {}

bool OutputStream::fail()
{
	return _fail;
}

void OutputStream::close() {}

OutputStream::~OutputStream()
{
	close();
}

//////// protected //////////
OutputStream::OutputStream()  : _fail(false) {}


///////////////// FileOutputStream ///////////////////////////

bool FileOutputStream::write(const Array<char> &b)
{
	return write(b, 0, b.length());
}

////////////// FilterOutputStream //////////////

FilterOutputStream::FilterOutputStream(OutputStream *out) : out(out)
{
}

bool FilterOutputStream::write(int b)
{
	return out->write(b);
}
	
bool FilterOutputStream::write(const Array<char> &b, int off, int len)
{
	return out->write(b, off, len);
}

bool FilterOutputStream::write(const Array<char> &b)
{
	return out->write(b, 0, b.length());
}

	
void FilterOutputStream::flush()
{
	out->flush();
}

void FilterOutputStream::close()
{
	out->close();
}

FilterOutputStream::~FilterOutputStream()
{
	close();
	delete out;
}


} // namespace