SDSL 3.0.3
Succinct Data Structure Library
Loading...
Searching...
No Matches
sfstream.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
8#ifndef INCLUDED_SDSL_SFSTREAM
9#define INCLUDED_SDSL_SFSTREAM
10
11#include <fstream>
12#include <string>
13
14#include <sdsl/ram_filebuf.hpp>
15#include <sdsl/ram_fs.hpp>
16
17namespace sdsl
18{
19
20class osfstream : public std::ostream
21{
22public:
23 typedef std::streambuf * buf_ptr_type;
24
25private:
26 buf_ptr_type m_streambuf = nullptr;
27 std::string m_file = "";
28
29public:
30 typedef void * voidptr;
32 osfstream() : std::ostream(nullptr)
33 {
34 this->init(m_streambuf);
35 }
36
38 osfstream(std::string const & file, std::ios_base::openmode mode = std::ios_base::out) : std::ostream(nullptr)
39 {
40 this->init(m_streambuf);
41 open(file, mode);
42 }
43
45 buf_ptr_type open(std::string const & file, std::ios_base::openmode mode = std::ios_base::out)
46 {
47 delete m_streambuf;
48 m_streambuf = nullptr;
49 m_file = file;
50 std::streambuf * success = nullptr;
51 if (is_ram_file(file))
52 {
53 m_streambuf = new ram_filebuf();
54 success = ((ram_filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::out);
55 }
56 else
57 {
58 m_streambuf = new std::filebuf();
59 success = ((std::filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::out);
60 }
61 if (success)
62 {
63 this->clear();
64 }
65 else
66 {
67 this->setstate(std::ios_base::failbit);
68 delete m_streambuf;
69 m_streambuf = nullptr;
70 }
71 this->rdbuf(m_streambuf);
72 return m_streambuf;
73 }
74
76 bool is_open()
77 {
78 if (nullptr == m_streambuf)
79 return false;
80 if (is_ram_file(m_file))
81 {
82 return ((ram_filebuf *)m_streambuf)->is_open();
83 }
84 else
85 {
86 return ((std::filebuf *)m_streambuf)->is_open();
87 }
88 }
89
91 void close()
92 {
93 bool fail = false;
94 if (nullptr == m_streambuf)
95 {
96 fail = true;
97 }
98 else
99 {
100 if (is_ram_file(m_file))
101 {
102 fail = !((ram_filebuf *)m_streambuf)->close();
103 }
104 else
105 {
106 fail = !((std::filebuf *)m_streambuf)->close();
107 }
108 }
109 if (fail)
110 this->setstate(std::ios::failbit);
111 }
112
115 {
116 delete m_streambuf; // streambuf closes the file on destruction
117 }
118
120 operator voidptr() const
121 {
122 return m_streambuf;
123 }
124
125 osfstream & seekp(pos_type pos)
126 {
127 ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
128 try
129 {
130 if (!this->fail())
131 {
132 pos_type p = 0;
133 if (is_ram_file(m_file))
134 {
135 p = ((ram_filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::out);
136 }
137 else
138 {
139 p = ((std::filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::out);
140 }
141 if (p == pos_type(off_type(-1)))
142 {
143 err |= ios_base::failbit;
144 this->setstate(err);
145 }
146 }
147 }
148 catch (...)
149 {
150 if (err)
151 {
152 this->setstate(err);
153 }
154 }
155 return *this;
156 }
157
158 osfstream & seekp(off_type off, ios_base::seekdir way)
159 {
160 ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
161 try
162 {
163 if (!this->fail())
164 {
165 pos_type p = 0;
166 if (is_ram_file(m_file))
167 {
168 p = ((ram_filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::out);
169 }
170 else
171 {
172 p = ((std::filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::out);
173 }
174 if (p == pos_type(off_type(-1)))
175 {
176 err |= ios_base::failbit;
177 this->setstate(err);
178 }
179 }
180 }
181 catch (...)
182 {
183 if (err)
184 {
185 this->setstate(err);
186 }
187 }
188 return *this;
189 }
190
191 std::streampos tellp();
192};
193
194class isfstream : public std::istream
195{
196 typedef std::streambuf * buf_ptr_type;
197
198private:
199 buf_ptr_type m_streambuf = nullptr;
200 std::string m_file = "";
201
202public:
203 typedef void * voidptr;
205 isfstream() : std::istream(nullptr)
206 {
207 this->init(m_streambuf);
208 }
209
211 isfstream(std::string const & file, std::ios_base::openmode mode = std::ios_base::in) : std::istream(nullptr)
212 {
213 this->init(m_streambuf);
214 open(file, mode);
215 }
216
218 buf_ptr_type open(std::string const & file, std::ios_base::openmode mode = std::ios_base::in)
219 {
220 delete m_streambuf;
221 m_streambuf = nullptr;
222 m_file = file;
223 std::streambuf * success = nullptr;
224 if (is_ram_file(file))
225 {
226 m_streambuf = new ram_filebuf();
227 success = ((ram_filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::in);
228 }
229 else
230 {
231 m_streambuf = new std::filebuf();
232 success = ((std::filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::in);
233 }
234 if (success)
235 {
236 this->clear();
237 }
238 else
239 {
240 this->setstate(std::ios_base::failbit);
241 delete m_streambuf;
242 m_streambuf = nullptr;
243 }
244 this->rdbuf(m_streambuf);
245 return m_streambuf;
246 }
247
249 bool is_open()
250 {
251 if (nullptr == m_streambuf)
252 return false;
253 if (is_ram_file(m_file))
254 {
255 return ((ram_filebuf *)m_streambuf)->is_open();
256 }
257 else
258 {
259 return ((std::filebuf *)m_streambuf)->is_open();
260 }
261 }
262
264 void close()
265 {
266 bool fail = false;
267 if (nullptr == m_streambuf)
268 {
269 fail = true;
270 }
271 else
272 {
273 if (is_ram_file(m_file))
274 {
275 fail = !((ram_filebuf *)m_streambuf)->close();
276 }
277 else
278 {
279 fail = !((std::filebuf *)m_streambuf)->close();
280 }
281 }
282 if (fail)
283 this->setstate(std::ios::failbit);
284 }
285
288 {
289 delete m_streambuf;
290 }
291
293 operator voidptr() const
294 {
295 return m_streambuf; // streambuf closes the file on destruction
296 }
297
298 isfstream & seekg(pos_type pos)
299 {
300 ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
301 try
302 {
303 if (!this->fail())
304 {
305 pos_type p = 0;
306 if (is_ram_file(m_file))
307 {
308 p = ((ram_filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::in);
309 }
310 else
311 {
312 p = ((std::filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::in);
313 }
314 if (p == pos_type(off_type(-1)))
315 {
316 err |= ios_base::failbit;
317 }
318 }
319 }
320 catch (...)
321 {
322 if (err)
323 {
324 this->setstate(err);
325 }
326 }
327 return *this;
328 }
329
330 isfstream & seekg(off_type off, ios_base::seekdir way)
331 {
332 ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
333 try
334 {
335 if (!this->fail())
336 {
337 pos_type p = 0;
338 if (is_ram_file(m_file))
339 {
340 p = ((ram_filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::in);
341 }
342 else
343 {
344 p = ((std::filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::in);
345 }
346 if (p == pos_type(off_type(-1)))
347 {
348 err |= ios_base::failbit;
349 }
350 }
351 }
352 catch (...)
353 {
354 if (err)
355 {
356 this->setstate(err);
357 }
358 }
359 return *this;
360 }
361
362 std::streampos tellg()
363 {
364 ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
365 pos_type p = pos_type(off_type(-1));
366 try
367 {
368 if (!this->fail())
369 {
370 if (is_ram_file(m_file))
371 {
372 p = ((ram_filebuf *)m_streambuf)->pubseekoff(0, std::ios_base::cur);
373 }
374 else
375 {
376 p = ((std::filebuf *)m_streambuf)->pubseekoff(0, std::ios_base::cur);
377 }
378 if (p == pos_type(off_type(-1)))
379 {
380 err |= ios_base::failbit;
381 }
382 }
383 }
384 catch (...)
385 {
386 if (err)
387 {
388 this->setstate(err);
389 }
390 }
391 return p;
392 }
393};
394
395} // namespace sdsl
396
397#endif
isfstream(std::string const &file, std::ios_base::openmode mode=std::ios_base::in)
Constructor taking a file name and open mode.
Definition sfstream.hpp:211
~isfstream()
Standard destructor.
Definition sfstream.hpp:287
isfstream & seekg(pos_type pos)
Definition sfstream.hpp:298
bool is_open()
Is the stream close?
Definition sfstream.hpp:249
std::streampos tellg()
Definition sfstream.hpp:362
buf_ptr_type open(std::string const &file, std::ios_base::openmode mode=std::ios_base::in)
Open the stream.
Definition sfstream.hpp:218
isfstream & seekg(off_type off, ios_base::seekdir way)
Definition sfstream.hpp:330
isfstream()
Standard constructor.
Definition sfstream.hpp:205
void close()
Close the stream.
Definition sfstream.hpp:264
std::streambuf * buf_ptr_type
Definition sfstream.hpp:23
osfstream & seekp(pos_type pos)
Definition sfstream.hpp:125
void close()
Close the stream.
Definition sfstream.hpp:91
bool is_open()
Is the stream close?
Definition sfstream.hpp:76
~osfstream()
Standard destructor.
Definition sfstream.hpp:114
osfstream()
Standard constructor.
Definition sfstream.hpp:32
osfstream & seekp(off_type off, ios_base::seekdir way)
Definition sfstream.hpp:158
std::streampos tellp()
buf_ptr_type open(std::string const &file, std::ios_base::openmode mode=std::ios_base::out)
Open the stream.
Definition sfstream.hpp:45
osfstream(std::string const &file, std::ios_base::openmode mode=std::ios_base::out)
Constructor taking a file name and open mode.
Definition sfstream.hpp:38
Namespace for the succinct data structure library.
bool is_ram_file(std::string const &file)
Determines if the given file is a RAM-file.
Definition ram_fs.hpp:176
ram_fs.hpp