GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
c-file-ptr-stream.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2000-2025 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
40class c_file_ptr_buf : public std::streambuf
41{
42public:
43
44 typedef std::streambuf::int_type int_type;
45
46 typedef int (*close_fcn) (FILE *);
47
48 FILE * stdiofile () { return m_f; }
49
50 c_file_ptr_buf () = delete;
51
52 c_file_ptr_buf (FILE *f, close_fcn cf = file_close)
53 : std::streambuf (), m_f (f), m_cf (cf)
54 { }
55
56 OCTAVE_DISABLE_COPY_MOVE (c_file_ptr_buf)
57
59
60 int_type overflow (int_type);
61
62 int_type underflow () { return underflow_common (false); }
63
64 int_type uflow () { return underflow_common (true); }
65
66 int_type pbackfail (int_type);
67
68 std::streamsize xsputn (const char *, std::streamsize);
69
70 std::streamsize xsgetn (char *, std::streamsize);
71
72 std::streampos seekoff (std::streamoff, std::ios::seekdir,
73 std::ios::openmode = std::ios::in | std::ios::out);
74
75 std::streampos seekpos (std::streampos,
76 std::ios::openmode = std::ios::in | std::ios::out);
77
78 int sync ();
79
80 int flush ();
81
82 int buf_close ();
83
84 int file_number () const { return m_f ? fileno (m_f) : -1; }
85
86 int seek (off_t offset, int origin);
87
88 off_t tell ();
89
90 void clear () { if (m_f) clearerr (m_f); }
91
92 static int file_close (FILE *m_f);
93
94protected:
95
96 FILE *m_f;
97
98 close_fcn m_cf;
99
100private:
101
102 int_type underflow_common (bool);
103};
104
105// FIXME: the following three classes could probably share some code...
106
107template <typename STREAM_T, typename FILE_T, typename BUF_T>
109{
110public:
111
112 c_file_ptr_stream () = delete;
113
114 c_file_ptr_stream (FILE_T m_f,
115 typename BUF_T::close_fcn m_cf = BUF_T::file_close)
116 : STREAM_T (nullptr), m_buf (new BUF_T (m_f, m_cf))
117 { STREAM_T::init (m_buf); }
118
119 OCTAVE_DISABLE_COPY_MOVE (c_file_ptr_stream)
120
121 ~c_file_ptr_stream () { delete m_buf; m_buf = nullptr; }
122
123 BUF_T * rdbuf () { return m_buf; }
124
125 void stream_close () { if (m_buf) m_buf->buf_close (); }
126
127 int seek (off_t offset, int origin)
128 { return m_buf ? m_buf->seek (offset, origin) : -1; }
129
130 off_t tell () { return m_buf ? m_buf->tell () : -1; }
131
132 void clear () { if (m_buf) m_buf->clear (); STREAM_T::clear (); }
133
134private:
135
136 BUF_T *m_buf;
137};
138
145
146
147#if defined (HAVE_ZLIB)
148
149class c_zfile_ptr_buf : public std::streambuf
150{
151public:
152
153 typedef std::streambuf::int_type int_type;
154
155 typedef int (*close_fcn) (gzFile);
156
157 gzFile stdiofile () { return m_f; }
158
159 c_zfile_ptr_buf () = delete;
160
162 : std::streambuf (), m_f (f), m_cf (cf)
163 { }
164
165 OCTAVE_DISABLE_COPY_MOVE (c_zfile_ptr_buf)
166
168
170
171 int_type underflow () { return underflow_common (false); }
172
173 int_type uflow () { return underflow_common (true); }
174
176
177 std::streamsize xsputn (const char *, std::streamsize);
178
179 std::streamsize xsgetn (char *, std::streamsize);
180
181 std::streampos seekoff (std::streamoff, std::ios::seekdir,
182 std::ios::openmode = std::ios::in | std::ios::out);
183
184 std::streampos seekpos (std::streampos,
185 std::ios::openmode = std::ios::in | std::ios::out);
186
187 int sync ();
188
189 int flush ();
190
191 int buf_close ();
192
193 int file_number () const { return -1; }
194
195 int seek (off_t offset, int origin)
196 { return m_f ? gzseek (m_f, offset, origin) >= 0 : -1; }
197
198 off_t tell () { return m_f ? gztell (m_f) : -1; }
199
200 void clear () { if (m_f) gzclearerr (m_f); }
201
202 static int file_close (gzFile m_f) { return ::gzclose (m_f); }
203
204protected:
205
206 gzFile m_f;
207
209
210private:
211
212 int_type underflow_common (bool);
213};
214
221
222#endif
223
224OCTAVE_END_NAMESPACE(octave)
225
226#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 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)
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)
int file_number() const
std::streamsize xsgetn(char *, std::streamsize)
static int file_close(gzFile m_f)
c_zfile_ptr_buf(gzFile f, close_fcn cf=file_close)
std::streampos seekoff(std::streamoff, std::ios::seekdir, std::ios::openmode=std::ios::in|std::ios::out)
int_type pbackfail(int_type)
int(* close_fcn)(gzFile)
int_type overflow(int_type)
int seek(off_t offset, int origin)
std::streamsize xsputn(const char *, std::streamsize)
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