ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/Common/fdstream.hpp
Revision: 2
Committed: 2007-04-25T21:28:10-07:00 (15 years, 3 months ago) by douglas
File size: 6510 byte(s)
Log Message:
Woo!

File Contents

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