GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
c-file-ptr-stream.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 <iostream>
31 #include <streambuf>
32 
33 class
34 c_file_ptr_buf : public std::streambuf
35 {
36 public:
37 
38  typedef std::streambuf::int_type int_type;
39 
40  typedef int (*close_fcn) (FILE *);
41 
42  FILE *stdiofile (void) { return f; }
43 
44  c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = file_close)
45  : std::streambuf (), f (f_arg), cf (cf_arg)
46  { }
47 
48  // No copying!
49 
50  c_file_ptr_buf (const c_file_ptr_buf&) = delete;
51 
52  c_file_ptr_buf& operator = (const c_file_ptr_buf&) = delete;
53 
54  ~c_file_ptr_buf (void);
55 
56  int_type overflow (int_type);
57 
58  int_type underflow (void) { return underflow_common (false); }
59 
60  int_type uflow (void) { return underflow_common (true); }
61 
62  int_type pbackfail (int_type);
63 
64  std::streamsize xsputn (const char*, std::streamsize);
65 
66  std::streamsize xsgetn (char *, std::streamsize);
67 
68  std::streampos seekoff (std::streamoff, std::ios::seekdir,
69  std::ios::openmode = std::ios::in | std::ios::out);
70 
71  std::streampos seekpos (std::streampos,
72  std::ios::openmode = std::ios::in | std::ios::out);
73 
74  int sync (void);
75 
76  int flush (void);
77 
78  int buf_close (void);
79 
80  int file_number () const { return f ? fileno (f) : -1; }
81 
82  int seek (off_t offset, int origin);
83 
84  off_t tell (void);
85 
86  void clear (void) { if (f) clearerr (f); }
87 
88  static int file_close (FILE *f);
89 
90 protected:
91 
92  FILE *f;
93 
95 
96 private:
97 
98  int_type underflow_common (bool);
99 };
100 
101 // FIXME: the following three classes could probably share some code...
102 
103 template <typename STREAM_T, typename FILE_T, typename BUF_T>
104 class
106 {
107 public:
108 
109  c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::file_close)
110  : STREAM_T (nullptr), buf (new BUF_T (f, cf)) { STREAM_T::init (buf); }
111 
112  // No copying!
113 
114  c_file_ptr_stream (const c_file_ptr_stream&) = delete;
115 
116  c_file_ptr_stream& operator = (const c_file_ptr_stream&) = delete;
117 
118  ~c_file_ptr_stream (void) { delete buf; buf = nullptr; }
119 
120  BUF_T * rdbuf (void) { return buf; }
121 
122  void stream_close (void) { if (buf) buf->buf_close (); }
123 
124  int seek (off_t offset, int origin)
125  { return buf ? buf->seek (offset, origin) : -1; }
126 
127  off_t tell (void) { return buf ? buf->tell () : -1; }
128 
129  void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); }
130 
131 private:
132 
133  BUF_T *buf;
134 };
135 
142 
143 #if defined (HAVE_ZLIB)
144 
145 #if defined (HAVE_ZLIB_H)
146 # include <zlib.h>
147 #endif
148 
149 class
150 c_zfile_ptr_buf : public std::streambuf
151 {
152 public:
153 
154  typedef std::streambuf::int_type int_type;
155 
156  typedef int (*close_fcn) (gzFile);
157 
158  gzFile stdiofile (void) { return f; }
159 
160  c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = file_close)
161  : std::streambuf (), f (f_arg), cf (cf_arg)
162  { }
163 
164  // No copying!
165 
166  c_zfile_ptr_buf (const c_zfile_ptr_buf&) = delete;
167 
168  c_zfile_ptr_buf& operator = (const c_zfile_ptr_buf&) = delete;
169 
170  ~c_zfile_ptr_buf (void);
171 
172  int_type overflow (int_type);
173 
174  int_type underflow (void) { return underflow_common (false); }
175 
176  int_type uflow (void) { return underflow_common (true); }
177 
178  int_type pbackfail (int_type);
179 
180  std::streamsize xsputn (const char*, std::streamsize);
181 
182  std::streamsize xsgetn (char *, std::streamsize);
183 
184  std::streampos seekoff (std::streamoff, std::ios::seekdir,
185  std::ios::openmode = std::ios::in | std::ios::out);
186 
187  std::streampos seekpos (std::streampos,
188  std::ios::openmode = std::ios::in | std::ios::out);
189 
190  int sync (void);
191 
192  int flush (void);
193 
194  int buf_close (void);
195 
196  int file_number () const { return -1; }
197 
198  int seek (off_t offset, int origin)
199  { return f ? gzseek (f, offset, origin) >= 0 : -1; }
200 
201  off_t tell (void) { return f ? gztell (f) : -1; }
202 
203  void clear (void) { if (f) gzclearerr (f); }
204 
205  static int file_close (gzFile f) { return ::gzclose (f); }
206 
207 protected:
208 
209  gzFile f;
210 
212 
213 private:
214 
215  int_type underflow_common (bool);
216 };
217 
224 
225 #endif
226 
227 #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
int file_number() const
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &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
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)