summaryrefslogtreecommitdiff
path: root/stream_lc.h
blob: 26271c951055034dbf33782ffe93e65a63dc9e18 (plain)
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
/*
 Author: Marcus Boerger <helly@users.sourceforge.net>
*/

/* $Id: stream_lc.h 858 2008-04-03 20:53:44Z helly $ */

#ifndef _stream_lc_h
#define _stream_lc_h

#include <iosfwd>
#include <fstream>
#include <assert.h>
#include <stdio.h>

namespace re2c
{

template<class _E, class _Tr = std::char_traits<_E> >
class basic_null_streambuf
	: public std::basic_streambuf<_E, _Tr>
{
public:
	basic_null_streambuf()
		: std::basic_streambuf<_E, _Tr>()
	{
	}	
};

typedef basic_null_streambuf<char> null_streambuf;

template<class _E, class _Tr = std::char_traits<_E> >
class basic_null_stream
	: public std::basic_ostream<_E, _Tr>
{
public:
	basic_null_stream()
		: std::basic_ostream<_E, _Tr>(null_buf = new basic_null_streambuf<_E, _Tr>())
	{
	}
	
	virtual ~basic_null_stream()
	{
		delete null_buf;
	}

	basic_null_stream& put(_E)
	{
		// nothing to do
		return *this;
	}
	
	basic_null_stream& write(const _E *, std::streamsize)
	{
		// nothing to do
		return *this;
	}

protected:
	basic_null_streambuf<_E, _Tr> * null_buf;
};

typedef basic_null_stream<char> null_stream;

class line_number
{
public:
	virtual ~line_number()
	{
	}

	virtual uint get_line() const = 0;
};

template<class _E, class _Tr = std::char_traits<_E> >
class basic_filebuf_lc
	: public std::basic_streambuf<_E, _Tr>
	, public line_number
{
public:
	typedef std::basic_streambuf<_E, _Tr> _Mybase;
	typedef basic_filebuf_lc<_E, _Tr> _Myt;
	typedef _E char_type;
	typedef _Tr traits_type;
	typedef typename _Tr::int_type int_type;
	typedef typename _Tr::pos_type pos_type;
	typedef typename _Tr::off_type off_type;

	basic_filebuf_lc(FILE *_fp = 0)
		: _Mybase()
		, fp(_fp)
		, must_close(false)
		, fline(1)
	{
	}

	virtual ~basic_filebuf_lc()
	{
		sync();
		if (must_close)
		{
			close();
		}
	}

	uint get_line() const
	{
		return fline + 1;
	}

	bool is_open() const
	{
		return fp != 0;
	}

	_Myt* open(const char *filename, std::ios_base::openmode mode = std::ios_base::out)
	{
		if (fp != 0)
		{
			return 0;
		}
		const char * fmode = (mode & std::ios_base::out)
		                   ? "wt"
		                   : "rt";
		if ((fp = fopen(filename, fmode)) == 0)
		{
			return 0;
		}

		must_close = true;
		return this;
	}

	_Myt* open(FILE * _fp)
	{
		if (fp != 0)
		{
			return 0;
		}
		fp = _fp;
		must_close = false;
		return this;
	}

	_Myt* close()
	{
		sync();

		if (fp == 0 || fclose(fp) != 0)
		{
			fp = 0;
			return 0;
		}
		else
		{
			fp = 0;
			return this;
		}
	}

protected:

	virtual int_type overflow(int_type c = _Tr::eof())
	{
		if (c == '\n')
		{
			++fline;
		}
		if (_Tr::eq_int_type(_Tr::eof(), c))
		{
			return _Tr::not_eof(c);
		}
		else
		{
			buffer += _Tr::to_char_type(c);
			return c;
		}
	}

	virtual int_type pbackfail(int_type c = _Tr::eof())
	{
		assert(0);
		c = 0;
		return _Tr::eof();
	}

	virtual int_type underflow() // don't point past it
	{
		int c;

		if (buffer.length())
		{
			return buffer[0];
		}
		if (fp == 0 || ((c = fgetc(fp)) == EOF))
		{
			return _Tr::eof();
		}
		buffer += (char)c;
		return c;
	}

	virtual int_type uflow() // point past it
	{
		int c;

		if (buffer.length())
		{
			c = buffer[0];
			buffer.erase(0, 1);
			return c;
		}
		if (fp == 0 || ((c = fgetc(fp)) == EOF))
		{
			return _Tr::eof();
		}
		else if (c == '\n')
		{
			++fline;
		}
		return c;
	}

#if 0
	virtual std::streamsize xsgetn(_E* buf, std::streamsize n)
	{
		std::streamsize r = 0;
		while(n--)
		{
			int_type c = underflow();
			if (_Tr::eq_int_type(_Tr::eof(), c))
			{
				break;
			}
			buf[r++] = c;
		}
		buf[r] = '\0';
		return r;
	}
#endif

	virtual pos_type seekoff(off_type off, std::ios_base::seekdir whence,
		std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
	{
		return fseek(fp, (long)off, whence);
	}

	virtual pos_type seekpos(pos_type fpos,
		std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
	{
		return fseek(fp, (long)fpos, SEEK_SET);
	}

	virtual _Mybase * setbuf(_E *, std::streamsize)
	{
		assert(0);
		return this;
	}

	virtual int sync()
	{
		if (buffer.length() != 0) {
			fwrite(buffer.c_str(), sizeof(_E), buffer.length(), fp);
		}
		buffer.clear();
		return fp == 0
			|| _Tr::eq_int_type(_Tr::eof(), overflow())
			|| 0 <= fflush(fp) ? 0 : -1;
	}

	virtual std::streamsize xsputn(const _E *buf, std::streamsize cnt)
	{
		if (buffer.length() != 0) {
			fwrite(buffer.c_str(), sizeof(_E), buffer.length(), fp);
		}
		buffer.clear();
		/*fline += std::count(buf, buf + cnt, '\n');*/
		for (std::streamsize pos = 0; pos < cnt; ++pos) 
		{
			if (buf[pos] == '\n')
			{
				++fline;
			}
		}
		if (cnt != 0) {
			return fwrite(buf, sizeof(_E), cnt, fp);
		} else {
			return 0;
		}
	}

private:

	FILE * fp;
	bool   must_close;
	uint   fline;
	std::basic_string<_E, _Tr> buffer;
};

typedef basic_filebuf_lc<char> filebuf_lc;

template<
	class _E, 
	class _BaseStream,
	std::ios_base::openmode  _DefOpenMode,
	class _Tr = std::char_traits<_E> >
class basic_fstream_lc
	: public _BaseStream
	, public line_number
{
public:
	typedef basic_fstream_lc<_E, _BaseStream, _DefOpenMode, _Tr> _Myt;
	typedef std::basic_ios<_E, _Tr> _Myios;
	typedef _BaseStream _Mybase;
	typedef basic_filebuf_lc<_E, _Tr> _Mybuf;

	basic_fstream_lc()
		: _Mybase(mybuf = new _Mybuf())
	{
	}

	virtual ~basic_fstream_lc()
	{
		delete mybuf;
	}

	bool is_open() const
	{
		return mybuf->is_open();
	}

	_Myt& open(const char * filename, std::ios_base::openmode mode = _DefOpenMode)
	{
		if ((mode & _DefOpenMode) == 0 || mybuf->open(filename, mode) == 0)
		{
			_Myios::setstate(std::ios_base::failbit);
		}
		return *this;
	}
	
	_Myt& open(FILE *fp)
	{
		if (mybuf->open(fp) == 0)
		{
			_Myios::setstate(std::ios_base::failbit);
		}
		return *this;
	}
	
	void close()
	{
		if (mybuf->close() == 0)
		{
			_Myios::setstate(std::ios_base::failbit);
		}
	}
	
	uint get_line() const
	{
		return mybuf->get_line();
	}

protected:
	mutable _Mybuf *mybuf;
};

template<class _E, class _Tr = std::char_traits<_E> >
class basic_ofstream_lc
	: public basic_fstream_lc<_E, std::basic_ostream<_E, _Tr>, std::ios_base::out, _Tr>
{
};

typedef basic_ofstream_lc<char> ofstream_lc;

template<class _E, class _Tr = std::char_traits<_E> >
class basic_ifstream_lc
	: public basic_fstream_lc<_E, std::basic_istream<_E, _Tr>, std::ios_base::in, _Tr>
{
};

typedef basic_ifstream_lc<char> ifstream_lc;

class file_info
{
public:

	static std::string escape(const std::string& _str)
	{
		std::string str(_str);
		size_t l = str.length();
		for (size_t p = 0; p < l; ++p)
		{
			if (str[p] == '\\')
			{
				str.insert(++p, "\\");
				++l;
			}
		}
		return str;
	}

	file_info()
		: ln(NULL)
	{
	}

	file_info(const std::string& _fname, const line_number* _ln, bool _escape = true)
		: fname(_escape ? escape(_fname) : _fname)
		, ln(_ln)
	{
	}

	file_info(const file_info& oth, const line_number* _ln = NULL)
		: fname(oth.fname)
		, ln(_ln)
	{
	}

	file_info& operator = (const file_info& oth)
	{
		*(const_cast<std::string*>(&this->fname)) = oth.fname;
		ln = oth.ln;
		return *this;
	}

	void set_fname(const std::string& _fname)
	{
		*(const_cast<std::string*>(&this->fname)) = _fname;
	}

	const std::string  fname;
	const line_number* ln;
};

std::ostream& operator << (std::ostream& o, const file_info& li);

} // end namespace re2c

#endif /* _stream_lc_h */