SDSL  3.0.0
Succinct Data Structure Library
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 <sstream>
13 #include <string>
14 
15 #include <sdsl/ram_filebuf.hpp>
16 #include <sdsl/ram_fs.hpp>
17 
18 namespace sdsl
19 {
20 
21 class osfstream : public std::ostream
22 {
23  public:
24  typedef std::streambuf * buf_ptr_type;
25 
26  private:
27  buf_ptr_type m_streambuf = nullptr;
28  std::string m_file = "";
29 
30  public:
31  typedef void * voidptr;
34  : std::ostream(nullptr)
35  {
36  this->init(m_streambuf);
37  }
38 
40  osfstream(const std::string & file, std::ios_base::openmode mode = std::ios_base::out)
41  : std::ostream(nullptr)
42  {
43  this->init(m_streambuf);
44  open(file, mode);
45  }
46 
48  buf_ptr_type open(const std::string & file, std::ios_base::openmode mode = std::ios_base::out)
49  {
50  delete m_streambuf;
51  m_streambuf = nullptr;
52  m_file = file;
53  std::streambuf * success = nullptr;
54  if (is_ram_file(file))
55  {
56  m_streambuf = new ram_filebuf();
57  success = ((ram_filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::out);
58  }
59  else
60  {
61  m_streambuf = new std::filebuf();
62  success = ((std::filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::out);
63  }
64  if (success) { this->clear(); }
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) return false;
79  if (is_ram_file(m_file)) { return ((ram_filebuf *)m_streambuf)->is_open(); }
80  else
81  {
82  return ((std::filebuf *)m_streambuf)->is_open();
83  }
84  }
85 
87  void close()
88  {
89  bool fail = false;
90  if (nullptr == m_streambuf) { fail = true; }
91  else
92  {
93  if (is_ram_file(m_file)) { fail = !((ram_filebuf *)m_streambuf)->close(); }
94  else
95  {
96  fail = !((std::filebuf *)m_streambuf)->close();
97  }
98  }
99  if (fail) this->setstate(std::ios::failbit);
100  }
101 
104  {
105  delete m_streambuf; // streambuf closes the file on destruction
106  }
107 
109  operator voidptr() const { return m_streambuf; }
110 
111  osfstream & seekp(pos_type pos)
112  {
113  ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
114  try
115  {
116  if (!this->fail())
117  {
118  pos_type p = 0;
119  if (is_ram_file(m_file)) { p = ((ram_filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::out); }
120  else
121  {
122  p = ((std::filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::out);
123  }
124  if (p == pos_type(off_type(-1)))
125  {
126  err |= ios_base::failbit;
127  this->setstate(err);
128  }
129  }
130  }
131  catch (...)
132  {
133  if (err) { this->setstate(err); }
134  }
135  return *this;
136  }
137 
138  osfstream & seekp(off_type off, ios_base::seekdir way)
139  {
140  ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
141  try
142  {
143  if (!this->fail())
144  {
145  pos_type p = 0;
146  if (is_ram_file(m_file)) { p = ((ram_filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::out); }
147  else
148  {
149  p = ((std::filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::out);
150  }
151  if (p == pos_type(off_type(-1)))
152  {
153  err |= ios_base::failbit;
154  this->setstate(err);
155  }
156  }
157  }
158  catch (...)
159  {
160  if (err) { this->setstate(err); }
161  }
162  return *this;
163  }
164 
165  std::streampos tellp();
166 };
167 
168 class isfstream : public std::istream
169 {
170  typedef std::streambuf * buf_ptr_type;
171 
172  private:
173  buf_ptr_type m_streambuf = nullptr;
174  std::string m_file = "";
175 
176  public:
177  typedef void * voidptr;
180  : std::istream(nullptr)
181  {
182  this->init(m_streambuf);
183  }
184 
186  isfstream(const std::string & file, std::ios_base::openmode mode = std::ios_base::in)
187  : std::istream(nullptr)
188  {
189  this->init(m_streambuf);
190  open(file, mode);
191  }
192 
194  buf_ptr_type open(const std::string & file, std::ios_base::openmode mode = std::ios_base::in)
195  {
196  delete m_streambuf;
197  m_streambuf = nullptr;
198  m_file = file;
199  std::streambuf * success = nullptr;
200  if (is_ram_file(file))
201  {
202  m_streambuf = new ram_filebuf();
203  success = ((ram_filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::in);
204  }
205  else
206  {
207  m_streambuf = new std::filebuf();
208  success = ((std::filebuf *)m_streambuf)->open(m_file, mode | std::ios_base::in);
209  }
210  if (success) { this->clear(); }
211  else
212  {
213  this->setstate(std::ios_base::failbit);
214  delete m_streambuf;
215  m_streambuf = nullptr;
216  }
217  this->rdbuf(m_streambuf);
218  return m_streambuf;
219  }
220 
222  bool is_open()
223  {
224  if (nullptr == m_streambuf) return false;
225  if (is_ram_file(m_file)) { return ((ram_filebuf *)m_streambuf)->is_open(); }
226  else
227  {
228  return ((std::filebuf *)m_streambuf)->is_open();
229  }
230  }
231 
233  void close()
234  {
235  bool fail = false;
236  if (nullptr == m_streambuf) { fail = true; }
237  else
238  {
239  if (is_ram_file(m_file)) { fail = !((ram_filebuf *)m_streambuf)->close(); }
240  else
241  {
242  fail = !((std::filebuf *)m_streambuf)->close();
243  }
244  }
245  if (fail) this->setstate(std::ios::failbit);
246  }
247 
249  ~isfstream() { delete m_streambuf; }
250 
252  operator voidptr() const
253  {
254  return m_streambuf; // streambuf closes the file on destruction
255  }
256 
257  isfstream & seekg(pos_type pos)
258  {
259  ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
260  try
261  {
262  if (!this->fail())
263  {
264  pos_type p = 0;
265  if (is_ram_file(m_file)) { p = ((ram_filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::in); }
266  else
267  {
268  p = ((std::filebuf *)m_streambuf)->pubseekpos(pos, std::ios_base::in);
269  }
270  if (p == pos_type(off_type(-1))) { err |= ios_base::failbit; }
271  }
272  }
273  catch (...)
274  {
275  if (err) { this->setstate(err); }
276  }
277  return *this;
278  }
279 
280  isfstream & seekg(off_type off, ios_base::seekdir way)
281  {
282  ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
283  try
284  {
285  if (!this->fail())
286  {
287  pos_type p = 0;
288  if (is_ram_file(m_file)) { p = ((ram_filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::in); }
289  else
290  {
291  p = ((std::filebuf *)m_streambuf)->pubseekoff(off, way, std::ios_base::in);
292  }
293  if (p == pos_type(off_type(-1))) { err |= ios_base::failbit; }
294  }
295  }
296  catch (...)
297  {
298  if (err) { this->setstate(err); }
299  }
300  return *this;
301  }
302 
303  std::streampos tellg()
304  {
305  ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
306  pos_type p = pos_type(off_type(-1));
307  try
308  {
309  if (!this->fail())
310  {
311  if (is_ram_file(m_file)) { p = ((ram_filebuf *)m_streambuf)->pubseekoff(0, std::ios_base::cur); }
312  else
313  {
314  p = ((std::filebuf *)m_streambuf)->pubseekoff(0, std::ios_base::cur);
315  }
316  if (p == pos_type(off_type(-1))) { err |= ios_base::failbit; }
317  }
318  }
319  catch (...)
320  {
321  if (err) { this->setstate(err); }
322  }
323  return p;
324  }
325 };
326 
327 } // namespace sdsl
328 
329 #endif
~isfstream()
Standard destructor.
Definition: sfstream.hpp:249
bool is_open()
Is the stream close?
Definition: sfstream.hpp:222
std::streampos tellg()
Definition: sfstream.hpp:303
buf_ptr_type open(const std::string &file, std::ios_base::openmode mode=std::ios_base::in)
Open the stream.
Definition: sfstream.hpp:194
isfstream & seekg(off_type off, ios_base::seekdir way)
Definition: sfstream.hpp:280
isfstream()
Standard constructor.
Definition: sfstream.hpp:179
void close()
Close the stream.
Definition: sfstream.hpp:233
isfstream(const std::string &file, std::ios_base::openmode mode=std::ios_base::in)
Constructor taking a file name and open mode.
Definition: sfstream.hpp:186
isfstream & seekg(pos_type pos)
Definition: sfstream.hpp:257
std::streambuf * buf_ptr_type
Definition: sfstream.hpp:24
void close()
Close the stream.
Definition: sfstream.hpp:87
osfstream & seekp(off_type off, ios_base::seekdir way)
Definition: sfstream.hpp:138
bool is_open()
Is the stream close?
Definition: sfstream.hpp:76
buf_ptr_type open(const std::string &file, std::ios_base::openmode mode=std::ios_base::out)
Open the stream.
Definition: sfstream.hpp:48
osfstream(const std::string &file, std::ios_base::openmode mode=std::ios_base::out)
Constructor taking a file name and open mode.
Definition: sfstream.hpp:40
~osfstream()
Standard destructor.
Definition: sfstream.hpp:103
osfstream()
Standard constructor.
Definition: sfstream.hpp:33
std::streampos tellp()
osfstream & seekp(pos_type pos)
Definition: sfstream.hpp:111
void * voidptr
Definition: sfstream.hpp:31
Namespace for the succinct data structure library.
bool is_ram_file(const std::string &file)
Determines if the given file is a RAM-file.
Definition: ram_fs.hpp:158
ram_fs.hpp