GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
c-file-ptr-stream.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2017 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_c_file_ptr_stream_h)
24 #define octave_c_file_ptr_stream_h 1
25 
26 #include "octave-config.h"
27 
28 #include <cstdio>
29 
30 #include <streambuf>
31 
32 class
33 c_file_ptr_buf : public std::streambuf
34 {
35 public:
36 
37  typedef std::streambuf::int_type int_type;
38 
39  typedef int (*close_fcn) (FILE *);
40 
41  FILE* stdiofile (void) { return f; }
42 
43  c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = file_close)
44  : std::streambuf (), f (f_arg), cf (cf_arg)
45  { }
46 
47  ~c_file_ptr_buf (void);
48 
49  int_type overflow (int_type);
50 
51  int_type underflow (void) { return underflow_common (false); }
52 
53  int_type uflow (void) { return underflow_common (true); }
54 
55  int_type pbackfail (int_type);
56 
57  std::streamsize xsputn (const char*, std::streamsize);
58 
59  std::streamsize xsgetn (char *, std::streamsize);
60 
61  std::streampos seekoff (std::streamoff, std::ios::seekdir,
62  std::ios::openmode = std::ios::in | std::ios::out);
63 
64  std::streampos seekpos (std::streampos,
65  std::ios::openmode = std::ios::in | std::ios::out);
66 
67  int sync (void);
68 
69  int flush (void);
70 
71  int buf_close (void);
72 
73  int file_number () const { return f ? fileno (f) : -1; }
74 
75  int seek (off_t offset, int origin);
76 
77  off_t tell (void);
78 
79  void clear (void) { if (f) clearerr (f); }
80 
81  static int file_close (FILE *f);
82 
83 protected:
84 
85  FILE *f;
86 
88 
89 private:
90 
91  int_type underflow_common (bool);
92 
93  // No copying!
94 
96 
97  c_file_ptr_buf& operator = (const c_file_ptr_buf&);
98 };
99 
100 // FIXME: the following three classes could probably share some code...
101 
102 template <typename STREAM_T, typename FILE_T, typename BUF_T>
103 class
105 {
106 public:
107 
108  c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::file_close)
109  : STREAM_T (0), buf (new BUF_T (f, cf)) { STREAM_T::init (buf); }
110 
111  ~c_file_ptr_stream (void) { delete buf; buf = 0; }
112 
113  BUF_T *rdbuf (void) { return buf; }
114 
115  void stream_close (void) { if (buf) buf->buf_close (); }
116 
117  int seek (off_t offset, int origin)
118  { return buf ? buf->seek (offset, origin) : -1; }
119 
120  off_t tell (void) { return buf ? buf->tell () : -1; }
121 
122  void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); }
123 
124 private:
125 
126  BUF_T *buf;
127 
128  // No copying!
129 
131 
132  c_file_ptr_stream& operator = (const c_file_ptr_stream&);
133 };
134 
141 
142 #if defined (HAVE_ZLIB)
143 
144 #if defined (HAVE_ZLIB_H)
145 # include <zlib.h>
146 #endif
147 
148 class
149 c_zfile_ptr_buf : public std::streambuf
150 {
151 public:
152 
153  typedef std::streambuf::int_type int_type;
154 
155  typedef int (*close_fcn) (gzFile);
156 
157  gzFile stdiofile (void) { return f; }
158 
159  c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = file_close)
160  : std::streambuf (), f (f_arg), cf (cf_arg)
161  { }
162 
163  ~c_zfile_ptr_buf (void);
164 
165  int_type overflow (int_type);
166 
167  int_type underflow (void) { return underflow_common (false); }
168 
169  int_type uflow (void) { return underflow_common (true); }
170 
171  int_type pbackfail (int_type);
172 
173  std::streamsize xsputn (const char*, std::streamsize);
174 
175  std::streamsize xsgetn (char *, std::streamsize);
176 
177  std::streampos seekoff (std::streamoff, std::ios::seekdir,
178  std::ios::openmode = std::ios::in | std::ios::out);
179 
180  std::streampos seekpos (std::streampos,
181  std::ios::openmode = std::ios::in | std::ios::out);
182 
183  int sync (void);
184 
185  int flush (void);
186 
187  int buf_close (void);
188 
189  int file_number () const { return -1; }
190 
191  int seek (off_t offset, int origin)
192  { return f ? gzseek (f, offset, origin) >= 0 : -1; }
193 
194  off_t tell (void) { return f ? gztell (f) : -1; }
195 
196  void clear (void) { if (f) gzclearerr (f); }
197 
198  static int file_close (gzFile f) { return ::gzclose (f); }
199 
200 protected:
201 
202  gzFile f;
203 
205 
206 private:
207 
208  int_type underflow_common (bool);
209 
210  // No copying!
211 
213 
214  c_zfile_ptr_buf& operator = (const c_zfile_ptr_buf&);
215 };
216 
223 
224 #endif
225 
226 #endif
static int file_close(gzFile f)
int file_number() const
c_file_ptr_stream< std::ostream, gzFile, c_zfile_ptr_buf > o_c_zfile_ptr_stream
int_type uflow(void)
c_file_ptr_stream< std::iostream, gzFile, c_zfile_ptr_buf > io_c_zfile_ptr_stream
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
c_file_ptr_stream< std::istream, gzFile, c_zfile_ptr_buf > i_c_zfile_ptr_stream
int_type uflow(void)
int_type underflow(void)
c_file_ptr_stream< std::istream, FILE *, c_file_ptr_buf > i_c_file_ptr_stream
std::streambuf::int_type int_type
STL namespace.
int seek(off_t offset, int origin)
c_file_ptr_stream(FILE_T f, typename BUF_T::close_fcn cf=BUF_T::file_close)
c_file_ptr_stream< std::iostream, FILE *, c_file_ptr_buf > io_c_file_ptr_stream
int file_number() const
static void close_fcn(FILE *f)
c_zfile_ptr_buf(gzFile f_arg, close_fcn cf_arg=file_close)
c_file_ptr_buf(FILE *f_arg, close_fcn cf_arg=file_close)
int_type underflow(void)
c_file_ptr_stream< std::ostream, FILE *, c_file_ptr_buf > o_c_file_ptr_stream
gzFile stdiofile(void)
std::streambuf::int_type int_type
FILE * stdiofile(void)
int seek(off_t offset, int origin)
static void clear(octave::dynamic_library &oct_file)
Definition: dynamic-ld.cc:230