NEST  2.6.0,not_revisioned_source_dir@0
fdstream.h
Go to the documentation of this file.
1 /*
2  * fdstream.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef FDSTREAM_H
24 #define FDSTREAM_H
25 
31 #include "config.h"
32 #ifndef HAVE_ISTREAM
33 #include <iostream>
34 #include <fstream>
35 
36 typedef std::ifstream ifdstream;
37 typedef std::ofstream ofdstream;
38 typedef std::iostream fdstream;
39 #else
40 
41 #include <unistd.h>
42 #include <streambuf>
43 #include <istream>
44 #include <ostream>
45 #include <string>
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 
51 #include <iostream>
52 #include <fstream>
53 #include <cassert>
54 
55 #include <cstdio> // for the parallel FILE* workaround.
56 
57 // The Compaq C++ compiler V6.5-014 surpresses all non standard
58 // names if compilation is performed with -std strict_ansi.
59 // However, fdopen() is POSIX and guaranteed to be available in
60 // <stdio.h>.
61 // The flag __PURE_CNAME is specific for the Compaq compiler.
62 // We need to add a workaround flag HAVE_FDOPEN_IGNORED here.
63 // 24.4.03, Diesmann
64 //
65 // Compaq C++ V6.5-040 required fdopen without :: here. In fdstream.cc,
66 // ::fdopen is fine then.
67 // 28.6.04, Plesser
68 #ifdef __PURE_CNAME
69  extern "C" std::FILE* fdopen(int , const char *);
70 #endif
71 
72 
73 class fdbuf: public std::streambuf
74 {
75  static std::streamsize const s_bufsiz=1024;
76 
77 public:
78 
79  fdbuf():
80  m_fd(-1), m_isopen(false)
81  {
82  setp(m_outbuf, m_outbuf + s_bufsiz);
83  }
84 
85  fdbuf(int fd):
86  m_fd(fd), m_isopen(true)
87  {
88  setp(m_outbuf, m_outbuf + s_bufsiz);
89  }
90 
92  {
93  //sync();
94  close();
95  }
96 
97 
98  bool is_open() const
99  {
100  return m_isopen;
101  }
102 
103  fdbuf* open(const char*, std::ios_base::openmode);
104 
105  fdbuf* close();
106 
110  int fd()
111  {
112  return m_fd;
113  }
114 
115 protected:
116 
117  int_type underflow()
118  {
119  if (gptr() == egptr())
120  {
121  int size = ::read(m_fd, m_inbuf, s_bufsiz);
122  if (size < 1)
123  return traits_type::eof();
124  setg(m_inbuf, m_inbuf, m_inbuf + size);
125  }
126  return traits_type::to_int_type(*gptr());
127  }
128 
129  int_type overflow(int_type c)
130  {
131  if (sync() == -1)
132  {
133  // std::cerr<<"sync failed!"<<std::endl;
134  return traits_type::eof();}
135 
136  if (!traits_type::eq_int_type(c, traits_type::eof()))
137  {
138  *pptr() = traits_type::to_char_type(c);
139  pbump(1);
140  return c;
141  }
142  return traits_type::not_eof(c);
143  }
144 
145  int sync()
146  {
147  std::streamsize size = pptr() - pbase();
148  if (size > 0 && ::write(m_fd, m_outbuf, size) != size)
149  return -1;
150  setp(m_outbuf, m_outbuf + s_bufsiz);
151  return 0;
152  }
153 
154 private:
155 
156  int m_fd;
157  bool m_isopen;
160 
161 };
162 
163 
164 
165 
166 class ofdstream: public std::ostream
167 {
168 public:
169 
180  std::ostream(0), sb()
181  {
182  std::ostream::rdbuf(&sb);
183  init(&sb);
184  }
185 
186  explicit ofdstream(const char* s, std::ios_base::openmode mode = std::ios_base::out):
187  std::ostream(0), sb()
188  {
189  std::ostream::rdbuf(&sb);
190  init(&sb);
191  assert(good());
192  open(s, mode);
193  }
194 
195  explicit ofdstream(int fd):
196  std::ostream(0), sb(fd)
197  {
198  std::ostream::rdbuf(&sb);
199  init(&sb);
200  }
201 
202  fdbuf* rdbuf() const
203  {
204  return const_cast<fdbuf*>(&sb); // return type is non-const, member is const, by C++ specs!
205  }
206 
207  bool is_open()
208  {
209  return rdbuf()->is_open();
210  }
211 
212  void open(const char* s, std::ios_base::openmode mode = std::ios_base::out)
213  {
214  if (rdbuf()->open(s, mode|std::ios_base::out) == NULL)
215  {
216  setstate(failbit);
217  }
218  }
219 
220  void close();
221 
222 private:
223 
225 };
226 
227 class ifdstream: public std::istream
228 {
229 public:
230 
232  std::istream(0), sb()
233  {
234  std::istream::rdbuf(&sb);
235  init(&sb);
236  }
237 
238  explicit ifdstream(const char* s, std::ios_base::openmode mode = std::ios_base::in):
239  std::istream(0), sb()
240  {
241  std::istream::rdbuf(&sb);
242  init(&sb);
243  open(s, mode);
244  }
245 
246  explicit ifdstream(int fd):
247  std::istream(0), sb(fd)
248  {
249  std::istream::rdbuf(&sb);
250  init(&sb);
251  }
252 
253 
254  fdbuf* rdbuf() const
255  {
256  return const_cast<fdbuf*>(&sb); // return type is non-const, member is const, by C++ specs!
257  }
258 
259  bool is_open()
260  {
261  return rdbuf()->is_open();
262  }
263 
264  void open(const char* s, std::ios_base::openmode mode = std::ios_base::in)
265  {
266  if (rdbuf()->open(s, mode|std::ios_base::in) == NULL)
267  {
268  setstate(failbit);
269  }
270  }
271 
272  void close();
273 
274 private:
275 
277 };
278 
279 class fdstream: public std::iostream
280 {
281 public:
282 
284  std::iostream(0), sb() // See Constructor comment above.
285  {
286  std::iostream::rdbuf(&sb);
287  init(&sb); // See Constructor comment above.
288  }
289 
290  explicit fdstream(const char* s, std::ios_base::openmode mode):
291  std::iostream(0), sb() // See Constructor comment above.
292  {
293  std::iostream::rdbuf(&sb);
294  init(&sb); // See Constructor comment above.
295  open(s, mode);
296  }
297 
298  explicit fdstream(int fd): // See Constructor comment above.
299  std::iostream(0), sb(fd)
300  {
301  std::iostream::rdbuf(&sb);
302  init(&sb); // See Constructor comment above.
303  }
304 
305 
306  fdbuf* rdbuf() const
307  {
308  return const_cast<fdbuf*>(&sb); // return type is non-const, member is const, by C++ specs!
309  }
310 
311  bool is_open()
312  {
313  return rdbuf()->is_open();
314  }
315 
316  void open(const char* s, std::ios_base::openmode mode)
317  {
318  if (rdbuf()->open(s, mode) == NULL)
319  {
320  setstate(failbit);
321  }
322  }
323 
324  void close();
325 
326 private:
327 
329 };
330 #endif
331 
332 #endif
ofdstream(int fd)
Definition: fdstream.h:195
void open(const char *s, std::ios_base::openmode mode=std::ios_base::out)
Definition: fdstream.h:212
Definition: fdstream.h:279
fdbuf * close()
Definition: fdstream.cc:87
static std::streamsize const s_bufsiz
Definition: fdstream.h:75
Definition: fdstream.h:73
bool is_open()
Definition: fdstream.h:311
fdbuf * rdbuf() const
Definition: fdstream.h:254
fdbuf sb
Definition: fdstream.h:328
bool is_open() const
Definition: fdstream.h:98
char m_inbuf[s_bufsiz]
Definition: fdstream.h:158
bool m_isopen
Definition: fdstream.h:157
int fd()
Return the underlying file descriptor.
Definition: fdstream.h:110
Definition: fdstream.h:166
assert(pNet!=0)
fdbuf sb
Definition: fdstream.h:276
const Name std("std")
Miscellaneous parameters.
Definition: nest_names.h:265
Definition: fdstream.h:227
int_type underflow()
Definition: fdstream.h:117
~fdbuf()
Definition: fdstream.h:91
fdbuf()
Definition: fdstream.h:79
void close()
Definition: fdstream.cc:130
C std::FILE * fdopen(int, const char *)
void close()
Definition: fdstream.cc:113
fdstream()
Definition: fdstream.h:283
bool is_open()
Definition: fdstream.h:259
ofdstream()
Definition: fdstream.h:179
std::iostream fdstream
Definition: fdstream.h:38
ifdstream(const char *s, std::ios_base::openmode mode=std::ios_base::in)
Definition: fdstream.h:238
int m_fd
Definition: fdstream.h:156
std::ifstream ifdstream
Definition: fdstream.h:36
fdbuf sb
Definition: fdstream.h:224
std::ofstream ofdstream
Definition: fdstream.h:37
void open(const char *s, std::ios_base::openmode mode=std::ios_base::in)
Definition: fdstream.h:264
void open(const char *s, std::ios_base::openmode mode)
Definition: fdstream.h:316
fdbuf * rdbuf() const
Definition: fdstream.h:202
fdbuf * open(const char *, std::ios_base::openmode)
Definition: fdstream.cc:30
ifdstream(int fd)
Definition: fdstream.h:246
fdbuf(int fd)
Definition: fdstream.h:85
fdbuf * rdbuf() const
Definition: fdstream.h:306
int sync()
Definition: fdstream.h:145
int_type overflow(int_type c)
Definition: fdstream.h:129
bool is_open()
Definition: fdstream.h:207
ifdstream()
Definition: fdstream.h:231
const Name c("c")
Specific to Izhikevich 2003.
Definition: nest_names.h:62
fdstream(const char *s, std::ios_base::openmode mode)
Definition: fdstream.h:290
char m_outbuf[s_bufsiz]
Definition: fdstream.h:159
void close()
Definition: fdstream.cc:122
fdstream(int fd)
Definition: fdstream.h:298
ofdstream(const char *s, std::ios_base::openmode mode=std::ios_base::out)
Definition: fdstream.h:186