GNU Octave  9.1.0
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-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_c_file_ptr_stream_h)
27 #define octave_c_file_ptr_stream_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdio>
32 #include <istream>
33 
34 #if defined (HAVE_ZLIB_H)
35 # include <zlib.h>
36 #endif
37 
39 
40 class
41 c_file_ptr_buf : public std::streambuf
42 {
43 public:
44 
45  typedef std::streambuf::int_type int_type;
46 
47  typedef int (*close_fcn) (FILE *);
48 
49  FILE * stdiofile () { return m_f; }
50 
51  c_file_ptr_buf () = delete;
52 
53  c_file_ptr_buf (FILE *f, close_fcn cf = file_close)
54  : std::streambuf (), m_f (f), m_cf (cf)
55  { }
56 
57  OCTAVE_DISABLE_COPY_MOVE (c_file_ptr_buf)
58 
59  ~c_file_ptr_buf ();
60 
61  int_type overflow (int_type);
62 
63  int_type underflow () { return underflow_common (false); }
64 
65  int_type uflow () { return underflow_common (true); }
66 
67  int_type pbackfail (int_type);
68 
69  std::streamsize xsputn (const char *, std::streamsize);
70 
71  std::streamsize xsgetn (char *, std::streamsize);
72 
73  std::streampos seekoff (std::streamoff, std::ios::seekdir,
74  std::ios::openmode = std::ios::in | std::ios::out);
75 
76  std::streampos seekpos (std::streampos,
77  std::ios::openmode = std::ios::in | std::ios::out);
78 
79  int sync ();
80 
81  int flush ();
82 
83  int buf_close ();
84 
85  int file_number () const { return m_f ? fileno (m_f) : -1; }
86 
87  int seek (off_t offset, int origin);
88 
89  off_t tell ();
90 
91  void clear () { if (m_f) clearerr (m_f); }
92 
93  static int file_close (FILE *m_f);
94 
95 protected:
96 
97  FILE *m_f;
98 
99  close_fcn m_cf;
100 
101 private:
102 
103  int_type underflow_common (bool);
104 };
105 
106 // FIXME: the following three classes could probably share some code...
107 
108 template <typename STREAM_T, typename FILE_T, typename BUF_T>
109 class
111 {
112 public:
113 
114  c_file_ptr_stream () = delete;
115 
116  c_file_ptr_stream (FILE_T m_f,
117  typename BUF_T::close_fcn m_cf = BUF_T::file_close)
118  : STREAM_T (nullptr), m_buf (new BUF_T (m_f, m_cf))
119  { STREAM_T::init (m_buf); }
120 
121  OCTAVE_DISABLE_COPY_MOVE (c_file_ptr_stream)
122 
123  ~c_file_ptr_stream () { delete m_buf; m_buf = nullptr; }
124 
125  BUF_T * rdbuf () { return m_buf; }
126 
127  void stream_close () { if (m_buf) m_buf->buf_close (); }
128 
129  int seek (off_t offset, int origin)
130  { return m_buf ? m_buf->seek (offset, origin) : -1; }
131 
132  off_t tell () { return m_buf ? m_buf->tell () : -1; }
133 
134  void clear () { if (m_buf) m_buf->clear (); STREAM_T::clear (); }
135 
136 private:
137 
138  BUF_T *m_buf;
139 };
140 
147 
148 
149 #if defined (HAVE_ZLIB)
150 
151 class
152 c_zfile_ptr_buf : public std::streambuf
153 {
154 public:
155 
156  typedef std::streambuf::int_type int_type;
157 
158  typedef int (*close_fcn) (gzFile);
159 
160  gzFile stdiofile () { return m_f; }
161 
162  c_zfile_ptr_buf () = delete;
163 
164  c_zfile_ptr_buf (gzFile f, close_fcn cf = file_close)
165  : std::streambuf (), m_f (f), m_cf (cf)
166  { }
167 
168  OCTAVE_DISABLE_COPY_MOVE (c_zfile_ptr_buf)
169 
170  ~c_zfile_ptr_buf ();
171 
172  int_type overflow (int_type);
173 
174  int_type underflow () { return underflow_common (false); }
175 
176  int_type uflow () { 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 ();
191 
192  int flush ();
193 
194  int buf_close ();
195 
196  int file_number () const { return -1; }
197 
198  int seek (off_t offset, int origin)
199  { return m_f ? gzseek (m_f, offset, origin) >= 0 : -1; }
200 
201  off_t tell () { return m_f ? gztell (m_f) : -1; }
202 
203  void clear () { if (m_f) gzclearerr (m_f); }
204 
205  static int file_close (gzFile m_f) { return ::gzclose (m_f); }
206 
207 protected:
208 
209  gzFile m_f;
210 
211  close_fcn m_cf;
212 
213 private:
214 
215  int_type underflow_common (bool);
216 };
217 
224 
225 #endif
226 
227 OCTAVE_END_NAMESPACE(octave)
228 
229 #endif
c_file_ptr_stream< std::ostream, gzFile, c_zfile_ptr_buf > o_c_zfile_ptr_stream
c_file_ptr_stream< std::iostream, gzFile, c_zfile_ptr_buf > io_c_zfile_ptr_stream
c_file_ptr_stream< std::istream, FILE *, c_file_ptr_buf > i_c_file_ptr_stream
c_file_ptr_stream< std::iostream, FILE *, c_file_ptr_buf > io_c_file_ptr_stream
c_file_ptr_stream< std::ostream, FILE *, c_file_ptr_buf > o_c_file_ptr_stream
c_file_ptr_stream< std::istream, gzFile, c_zfile_ptr_buf > i_c_zfile_ptr_stream
c_file_ptr_buf()=delete
c_file_ptr_buf(FILE *f, close_fcn cf=file_close)
int_type underflow()
int file_number() const
std::streambuf::int_type int_type
int seek(off_t offset, int origin)
c_file_ptr_stream()=delete
c_file_ptr_stream(FILE_T m_f, typename BUF_T::close_fcn m_cf=BUF_T::file_close)
int file_number() const
static int file_close(gzFile m_f)
c_zfile_ptr_buf(gzFile f, close_fcn cf=file_close)
int seek(off_t offset, int origin)
c_zfile_ptr_buf()=delete
std::streambuf::int_type int_type
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f