2 Author: Marcus Boerger <helly@users.sourceforge.net>
5 /* $Id: stream_lc.h 858 2008-04-03 20:53:44Z helly $ */
18 template<class _E
, class _Tr
= std::char_traits
<_E
> >
19 class basic_null_streambuf
20 : public std::basic_streambuf
<_E
, _Tr
>
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
>
37 : std::basic_ostream
<_E
, _Tr
>(null_buf
= new basic_null_streambuf
<_E
, _Tr
>())
41 virtual ~basic_null_stream()
46 basic_null_stream
& put(_E
)
52 basic_null_stream
& write(const _E
*, std::streamsize
)
59 basic_null_streambuf
<_E
, _Tr
> * null_buf
;
62 typedef basic_null_stream
<char> null_stream
;
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
>
80 typedef std::basic_streambuf
<_E
, _Tr
> _Mybase
;
81 typedef basic_filebuf_lc
<_E
, _Tr
> _Myt
;
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)
96 virtual ~basic_filebuf_lc()
105 uint
get_line() const
115 _Myt
* open(const char *filename
, std::ios_base::openmode mode
= std::ios_base::out
)
121 const char * fmode
= (mode
& std::ios_base::out
)
124 if ((fp
= fopen(filename
, fmode
)) == 0)
133 _Myt
* open(FILE * _fp
)
148 if (fp
== 0 || fclose(fp
) != 0)
162 virtual int_type
overflow(int_type c
= _Tr::eof())
168 if (_Tr::eq_int_type(_Tr::eof(), c
))
170 return _Tr::not_eof(c
);
174 buffer
+= _Tr::to_char_type(c
);
179 virtual int_type
pbackfail(int_type c
= _Tr::eof())
186 virtual int_type
underflow() // don't point past it
194 if (fp
== 0 || ((c
= fgetc(fp
)) == EOF
))
202 virtual int_type
uflow() // point past it
212 if (fp
== 0 || ((c
= fgetc(fp
)) == EOF
))
224 virtual std::streamsize
xsgetn(_E
* buf
, std::streamsize n
)
226 std::streamsize r
= 0;
229 int_type c
= underflow();
230 if (_Tr::eq_int_type(_Tr::eof(), c
))
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
)
261 if (buffer
.length() != 0) {
262 fwrite(buffer
.c_str(), sizeof(_E
), buffer
.length(), fp
);
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
);
276 /*fline += std::count(buf, buf + cnt, '\n');*/
277 for (std::streamsize pos
= 0; pos
< cnt
; ++pos
)
279 if (buf
[pos
] == '\n')
285 return fwrite(buf
, sizeof(_E
), cnt
, fp
);
296 std::basic_string
<_E
, _Tr
> buffer
;
299 typedef basic_filebuf_lc
<char> filebuf_lc
;
304 std::ios_base::openmode _DefOpenMode
,
305 class _Tr
= std::char_traits
<_E
> >
306 class basic_fstream_lc
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
;
317 : _Mybase(mybuf
= new _Mybuf())
321 virtual ~basic_fstream_lc()
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
);
342 if (mybuf
->open(fp
) == 0)
344 _Myios::setstate(std::ios_base::failbit
);
351 if (mybuf
->close() == 0)
353 _Myios::setstate(std::ios_base::failbit
);
357 uint
get_line() const
359 return mybuf
->get_line();
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
;
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
)
394 str
.insert(++p
, "\\");
406 file_info(const std::string
& _fname
, const line_number
* _ln
, bool _escape
= true)
407 : fname(_escape
? escape(_fname
) : _fname
)
412 file_info(const file_info
& oth
, const line_number
* _ln
= NULL
)
418 file_info
& operator = (const file_info
& oth
)
420 *(const_cast<std::string
*>(&this->fname
)) = oth
.fname
;
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 */