cp/guest: clean up guest_create's console handling
[hvf.git] / build / re2c / stream_lc.h
blob26271c951055034dbf33782ffe93e65a63dc9e18
1 /*
2 Author: Marcus Boerger <helly@users.sourceforge.net>
3 */
5 /* $Id: stream_lc.h 858 2008-04-03 20:53:44Z helly $ */
7 #ifndef _stream_lc_h
8 #define _stream_lc_h
10 #include <iosfwd>
11 #include <fstream>
12 #include <assert.h>
13 #include <stdio.h>
15 namespace re2c
18 template<class _E, class _Tr = std::char_traits<_E> >
19 class basic_null_streambuf
20 : public std::basic_streambuf<_E, _Tr>
22 public:
23 basic_null_streambuf()
24 : std::basic_streambuf<_E, _Tr>()
29 typedef basic_null_streambuf<char> null_streambuf;
31 template<class _E, class _Tr = std::char_traits<_E> >
32 class basic_null_stream
33 : public std::basic_ostream<_E, _Tr>
35 public:
36 basic_null_stream()
37 : std::basic_ostream<_E, _Tr>(null_buf = new basic_null_streambuf<_E, _Tr>())
41 virtual ~basic_null_stream()
43 delete null_buf;
46 basic_null_stream& put(_E)
48 // nothing to do
49 return *this;
52 basic_null_stream& write(const _E *, std::streamsize)
54 // nothing to do
55 return *this;
58 protected:
59 basic_null_streambuf<_E, _Tr> * null_buf;
62 typedef basic_null_stream<char> null_stream;
64 class line_number
66 public:
67 virtual ~line_number()
71 virtual uint get_line() const = 0;
74 template<class _E, class _Tr = std::char_traits<_E> >
75 class basic_filebuf_lc
76 : public std::basic_streambuf<_E, _Tr>
77 , public line_number
79 public:
80 typedef std::basic_streambuf<_E, _Tr> _Mybase;
81 typedef basic_filebuf_lc<_E, _Tr> _Myt;
82 typedef _E char_type;
83 typedef _Tr traits_type;
84 typedef typename _Tr::int_type int_type;
85 typedef typename _Tr::pos_type pos_type;
86 typedef typename _Tr::off_type off_type;
88 basic_filebuf_lc(FILE *_fp = 0)
89 : _Mybase()
90 , fp(_fp)
91 , must_close(false)
92 , fline(1)
96 virtual ~basic_filebuf_lc()
98 sync();
99 if (must_close)
101 close();
105 uint get_line() const
107 return fline + 1;
110 bool is_open() const
112 return fp != 0;
115 _Myt* open(const char *filename, std::ios_base::openmode mode = std::ios_base::out)
117 if (fp != 0)
119 return 0;
121 const char * fmode = (mode & std::ios_base::out)
122 ? "wt"
123 : "rt";
124 if ((fp = fopen(filename, fmode)) == 0)
126 return 0;
129 must_close = true;
130 return this;
133 _Myt* open(FILE * _fp)
135 if (fp != 0)
137 return 0;
139 fp = _fp;
140 must_close = false;
141 return this;
144 _Myt* close()
146 sync();
148 if (fp == 0 || fclose(fp) != 0)
150 fp = 0;
151 return 0;
153 else
155 fp = 0;
156 return this;
160 protected:
162 virtual int_type overflow(int_type c = _Tr::eof())
164 if (c == '\n')
166 ++fline;
168 if (_Tr::eq_int_type(_Tr::eof(), c))
170 return _Tr::not_eof(c);
172 else
174 buffer += _Tr::to_char_type(c);
175 return c;
179 virtual int_type pbackfail(int_type c = _Tr::eof())
181 assert(0);
182 c = 0;
183 return _Tr::eof();
186 virtual int_type underflow() // don't point past it
188 int c;
190 if (buffer.length())
192 return buffer[0];
194 if (fp == 0 || ((c = fgetc(fp)) == EOF))
196 return _Tr::eof();
198 buffer += (char)c;
199 return c;
202 virtual int_type uflow() // point past it
204 int c;
206 if (buffer.length())
208 c = buffer[0];
209 buffer.erase(0, 1);
210 return c;
212 if (fp == 0 || ((c = fgetc(fp)) == EOF))
214 return _Tr::eof();
216 else if (c == '\n')
218 ++fline;
220 return c;
223 #if 0
224 virtual std::streamsize xsgetn(_E* buf, std::streamsize n)
226 std::streamsize r = 0;
227 while(n--)
229 int_type c = underflow();
230 if (_Tr::eq_int_type(_Tr::eof(), c))
232 break;
234 buf[r++] = c;
236 buf[r] = '\0';
237 return r;
239 #endif
241 virtual pos_type seekoff(off_type off, std::ios_base::seekdir whence,
242 std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
244 return fseek(fp, (long)off, whence);
247 virtual pos_type seekpos(pos_type fpos,
248 std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
250 return fseek(fp, (long)fpos, SEEK_SET);
253 virtual _Mybase * setbuf(_E *, std::streamsize)
255 assert(0);
256 return this;
259 virtual int sync()
261 if (buffer.length() != 0) {
262 fwrite(buffer.c_str(), sizeof(_E), buffer.length(), fp);
264 buffer.clear();
265 return fp == 0
266 || _Tr::eq_int_type(_Tr::eof(), overflow())
267 || 0 <= fflush(fp) ? 0 : -1;
270 virtual std::streamsize xsputn(const _E *buf, std::streamsize cnt)
272 if (buffer.length() != 0) {
273 fwrite(buffer.c_str(), sizeof(_E), buffer.length(), fp);
275 buffer.clear();
276 /*fline += std::count(buf, buf + cnt, '\n');*/
277 for (std::streamsize pos = 0; pos < cnt; ++pos)
279 if (buf[pos] == '\n')
281 ++fline;
284 if (cnt != 0) {
285 return fwrite(buf, sizeof(_E), cnt, fp);
286 } else {
287 return 0;
291 private:
293 FILE * fp;
294 bool must_close;
295 uint fline;
296 std::basic_string<_E, _Tr> buffer;
299 typedef basic_filebuf_lc<char> filebuf_lc;
301 template<
302 class _E,
303 class _BaseStream,
304 std::ios_base::openmode _DefOpenMode,
305 class _Tr = std::char_traits<_E> >
306 class basic_fstream_lc
307 : public _BaseStream
308 , public line_number
310 public:
311 typedef basic_fstream_lc<_E, _BaseStream, _DefOpenMode, _Tr> _Myt;
312 typedef std::basic_ios<_E, _Tr> _Myios;
313 typedef _BaseStream _Mybase;
314 typedef basic_filebuf_lc<_E, _Tr> _Mybuf;
316 basic_fstream_lc()
317 : _Mybase(mybuf = new _Mybuf())
321 virtual ~basic_fstream_lc()
323 delete mybuf;
326 bool is_open() const
328 return mybuf->is_open();
331 _Myt& open(const char * filename, std::ios_base::openmode mode = _DefOpenMode)
333 if ((mode & _DefOpenMode) == 0 || mybuf->open(filename, mode) == 0)
335 _Myios::setstate(std::ios_base::failbit);
337 return *this;
340 _Myt& open(FILE *fp)
342 if (mybuf->open(fp) == 0)
344 _Myios::setstate(std::ios_base::failbit);
346 return *this;
349 void close()
351 if (mybuf->close() == 0)
353 _Myios::setstate(std::ios_base::failbit);
357 uint get_line() const
359 return mybuf->get_line();
362 protected:
363 mutable _Mybuf *mybuf;
366 template<class _E, class _Tr = std::char_traits<_E> >
367 class basic_ofstream_lc
368 : public basic_fstream_lc<_E, std::basic_ostream<_E, _Tr>, std::ios_base::out, _Tr>
372 typedef basic_ofstream_lc<char> ofstream_lc;
374 template<class _E, class _Tr = std::char_traits<_E> >
375 class basic_ifstream_lc
376 : public basic_fstream_lc<_E, std::basic_istream<_E, _Tr>, std::ios_base::in, _Tr>
380 typedef basic_ifstream_lc<char> ifstream_lc;
382 class file_info
384 public:
386 static std::string escape(const std::string& _str)
388 std::string str(_str);
389 size_t l = str.length();
390 for (size_t p = 0; p < l; ++p)
392 if (str[p] == '\\')
394 str.insert(++p, "\\");
395 ++l;
398 return str;
401 file_info()
402 : ln(NULL)
406 file_info(const std::string& _fname, const line_number* _ln, bool _escape = true)
407 : fname(_escape ? escape(_fname) : _fname)
408 , ln(_ln)
412 file_info(const file_info& oth, const line_number* _ln = NULL)
413 : fname(oth.fname)
414 , ln(_ln)
418 file_info& operator = (const file_info& oth)
420 *(const_cast<std::string*>(&this->fname)) = oth.fname;
421 ln = oth.ln;
422 return *this;
425 void set_fname(const std::string& _fname)
427 *(const_cast<std::string*>(&this->fname)) = _fname;
430 const std::string fname;
431 const line_number* ln;
434 std::ostream& operator << (std::ostream& o, const file_info& li);
436 } // end namespace re2c
438 #endif /* _stream_lc_h */