1 |
// Truck Computer Dooom! |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#ifndef _fdstream_hpp_ |
8 |
#define _fdstream_hpp_ |
9 |
|
10 |
#include <string> |
11 |
|
12 |
// XXX: I don't know why they didn't implement the rest of this for me |
13 |
#include <ext/stdio_filebuf.h> |
14 |
|
15 |
namespace ext |
16 |
{ |
17 |
|
18 |
using __gnu_cxx::stdio_filebuf; |
19 |
|
20 |
template <typename _CharT, typename _Traits = std::char_traits<_CharT> > |
21 |
struct basic_ifdstream : public std::basic_istream<_CharT, _Traits> |
22 |
{ |
23 |
typedef _CharT char_type; |
24 |
typedef _Traits traits_type; |
25 |
typedef typename traits_type::int_type int_type; |
26 |
typedef typename traits_type::pos_type pos_type; |
27 |
typedef typename traits_type::off_type off_type; |
28 |
typedef stdio_filebuf<char_type, traits_type> __filebuf_type; |
29 |
typedef std::basic_istream<char_type, traits_type> __istream_type; |
30 |
|
31 |
private: |
32 |
__filebuf_type _M_filebuf; |
33 |
|
34 |
public: |
35 |
basic_ifdstream() : __istream_type(), _M_filebuf() |
36 |
{ |
37 |
this->init(&_M_filebuf); |
38 |
} |
39 |
|
40 |
explicit basic_ifdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf() |
41 |
{ |
42 |
this->init(&_M_filebuf); |
43 |
this->open(__s, __mode); |
44 |
} |
45 |
|
46 |
basic_ifdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf() |
47 |
{ |
48 |
this->init(&_M_filebuf); |
49 |
this->open(__s, __mode); |
50 |
} |
51 |
|
52 |
basic_ifdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf(__fd, __mode) |
53 |
{ |
54 |
this->init(&_M_filebuf); |
55 |
} |
56 |
|
57 |
__filebuf_type *rdbuf() const |
58 |
{ |
59 |
return const_cast<__filebuf_type *>(&_M_filebuf); |
60 |
} |
61 |
|
62 |
bool is_open() const |
63 |
{ |
64 |
return _M_filebuf.is_open(); |
65 |
} |
66 |
|
67 |
void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::in) |
68 |
{ |
69 |
if (!_M_filebuf.open(__s, __mode | std::ios_base::in)) |
70 |
this->setstate(std::ios_base::failbit); |
71 |
else |
72 |
this->clear(); |
73 |
} |
74 |
|
75 |
void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in) |
76 |
{ |
77 |
this->open(__s.c_str(), __mode); |
78 |
} |
79 |
|
80 |
void close() |
81 |
{ |
82 |
if (!_M_filebuf.close()) |
83 |
this->setstate(std::ios_base::failbit); |
84 |
} |
85 |
}; |
86 |
|
87 |
template <typename _CharT, typename _Traits = std::char_traits<_CharT> > |
88 |
struct basic_ofdstream : public std::basic_ostream<_CharT, _Traits> |
89 |
{ |
90 |
typedef _CharT char_type; |
91 |
typedef _Traits traits_type; |
92 |
typedef typename traits_type::int_type int_type; |
93 |
typedef typename traits_type::pos_type pos_type; |
94 |
typedef typename traits_type::off_type off_type; |
95 |
typedef stdio_filebuf<char_type, traits_type> __filebuf_type; |
96 |
typedef std::basic_ostream<char_type, traits_type> __ostream_type; |
97 |
|
98 |
private: |
99 |
__filebuf_type _M_filebuf; |
100 |
|
101 |
public: |
102 |
basic_ofdstream() : __ostream_type(), _M_filebuf() |
103 |
{ |
104 |
this->init(&_M_filebuf); |
105 |
} |
106 |
|
107 |
explicit basic_ofdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf() |
108 |
{ |
109 |
this->init(&_M_filebuf); |
110 |
this->open(__s, __mode); |
111 |
} |
112 |
|
113 |
basic_ofdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf() |
114 |
{ |
115 |
this->init(&_M_filebuf); |
116 |
this->open(__s, __mode); |
117 |
} |
118 |
|
119 |
basic_ofdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf(__fd, __mode) |
120 |
{ |
121 |
this->init(&_M_filebuf); |
122 |
} |
123 |
|
124 |
__filebuf_type *rdbuf() const |
125 |
{ |
126 |
return const_cast<__filebuf_type *>(&_M_filebuf); |
127 |
} |
128 |
|
129 |
bool is_open() const |
130 |
{ |
131 |
return _M_filebuf.is_open(); |
132 |
} |
133 |
|
134 |
void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) |
135 |
{ |
136 |
if (!_M_filebuf.open(__s, __mode | std::ios_base::out)) |
137 |
this->setstate(std::ios_base::failbit); |
138 |
else |
139 |
this->clear(); |
140 |
} |
141 |
|
142 |
void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) |
143 |
{ |
144 |
this->open(__s.c_str(), __mode); |
145 |
} |
146 |
|
147 |
void close() |
148 |
{ |
149 |
if (!_M_filebuf.close()) |
150 |
this->setstate(std::ios_base::failbit); |
151 |
} |
152 |
}; |
153 |
|
154 |
template <typename _CharT, typename _Traits = std::char_traits<_CharT> > |
155 |
struct basic_fdstream : public std::basic_iostream<_CharT, _Traits> |
156 |
{ |
157 |
typedef _CharT char_type; |
158 |
typedef _Traits traits_type; |
159 |
typedef typename traits_type::int_type int_type; |
160 |
typedef typename traits_type::pos_type pos_type; |
161 |
typedef typename traits_type::off_type off_type; |
162 |
typedef stdio_filebuf<char_type, traits_type> __filebuf_type; |
163 |
typedef std::basic_ios<char_type, traits_type> __ios_type; |
164 |
typedef std::basic_iostream<char_type, traits_type> __iostream_type; |
165 |
|
166 |
private: |
167 |
__filebuf_type _M_filebuf; |
168 |
|
169 |
public: |
170 |
basic_fdstream() : __iostream_type(), _M_filebuf() |
171 |
{ |
172 |
this->init(&_M_filebuf); |
173 |
} |
174 |
|
175 |
explicit basic_fdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf() |
176 |
{ |
177 |
this->init(&_M_filebuf); |
178 |
this->open(__s, __mode); |
179 |
} |
180 |
|
181 |
basic_fdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf() |
182 |
{ |
183 |
this->init(&_M_filebuf); |
184 |
this->open(__s, __mode); |
185 |
} |
186 |
|
187 |
basic_fdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf(__fd, __mode) |
188 |
{ |
189 |
this->init(&_M_filebuf); |
190 |
} |
191 |
|
192 |
__filebuf_type *rdbuf() const |
193 |
{ |
194 |
return const_cast<__filebuf_type *>(&_M_filebuf); |
195 |
} |
196 |
|
197 |
bool is_open() const |
198 |
{ |
199 |
return _M_filebuf.is_open(); |
200 |
} |
201 |
|
202 |
void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) |
203 |
{ |
204 |
if (!_M_filebuf.open(__s, __mode)) |
205 |
this->setstate(std::ios_base::failbit); |
206 |
else |
207 |
this->clear(); |
208 |
} |
209 |
|
210 |
void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) |
211 |
{ |
212 |
this->open(__s.c_str(), __mode); |
213 |
} |
214 |
|
215 |
void close() |
216 |
{ |
217 |
if (!_M_filebuf.close()) |
218 |
this->setstate(std::ios_base::failbit); |
219 |
} |
220 |
}; |
221 |
|
222 |
typedef basic_ifdstream<char> ifdstream; |
223 |
typedef basic_ofdstream<char> ofdstream; |
224 |
typedef basic_fdstream<char> fdstream; |
225 |
|
226 |
} |
227 |
|
228 |
#endif//_fdstream_hpp_ |