GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
c-file-ptr-stream.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2000-2023 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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <iomanip>
31 
32 #include "filepos-wrappers.h"
33 
34 #include "c-file-ptr-stream.h"
35 
37 
38 #if ! defined (SEEK_SET)
39 # define SEEK_SET 0
40 #endif
41 
42 #if ! defined (SEEK_CUR)
43 # define SEEK_CUR 1
44 #endif
45 
46 #if ! defined (SEEK_END)
47 # define SEEK_END 2
48 #endif
49 
51 {
52  buf_close ();
53 }
54 
55 // FIXME: I'm sure there is room for improvement here...
56 
59 {
60  if (m_f)
61  return (c != traits_type::eof ()) ? std::fputc (c, m_f) : flush ();
62  else
63  return traits_type::not_eof (c);
64 }
65 
68 {
69  if (m_f)
70  {
71  int_type c = std::fgetc (m_f);
72 
73  if (! bump && c != traits_type::eof ())
74  ungetc (c, m_f);
75 
76  return c;
77  }
78  else
79  return traits_type::eof ();
80 }
81 
84 {
85  return ((c != traits_type::eof () && m_f)
86  ? ungetc (c, m_f) : traits_type::not_eof (c));
87 }
88 
89 std::streamsize
90 c_file_ptr_buf::xsputn (const char *s, std::streamsize n)
91 {
92  if (m_f)
93  return std::fwrite (s, 1, n, m_f);
94  else
95  return 0;
96 }
97 
98 std::streamsize
99 c_file_ptr_buf::xsgetn (char *s, std::streamsize n)
100 {
101  if (m_f)
102  return std::fread (s, 1, n, m_f);
103  else
104  return 0;
105 }
106 
107 static inline int
108 seekdir_to_whence (std::ios::seekdir dir)
109 {
110  return (dir == std::ios::beg
111  ? SEEK_SET : (dir == std::ios::cur
112  ? SEEK_CUR : (dir == std::ios::end
113  ? SEEK_END : dir)));
114 }
115 
116 std::streampos
117 c_file_ptr_buf::seekoff (std::streamoff offset,
118  std::ios::seekdir dir,
119  std::ios::openmode)
120 {
121  if (m_f)
122  {
124 
125  return octave_ftello_wrapper (m_f);
126  }
127  else
128  return 0;
129 }
130 
131 std::streampos
132 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode)
133 {
134  if (m_f)
135  {
137 
138  return octave_ftello_wrapper (m_f);
139  }
140  else
141  return 0;
142 }
143 
144 int
146 {
147  flush ();
148 
149  return 0;
150 }
151 
152 int
154 {
155  return m_f ? std::fflush (m_f) : traits_type::eof ();
156 }
157 
158 int
160 {
161  int retval = -1;
162 
163  flush ();
164 
165  if (m_f)
166  {
167  retval = m_cf (m_f);
168  m_f = nullptr;
169  }
170 
171  return retval;
172 }
173 
174 int
175 c_file_ptr_buf::seek (off_t offset, int origin)
176 {
177  return m_f ? octave_fseeko_wrapper (m_f, offset, origin) : -1;
178 }
179 
180 off_t
182 {
183  return m_f ? octave_ftello_wrapper (m_f) : -1;
184 }
185 
186 int
188 {
189  return std::fclose (m_f);
190 }
191 
192 #if defined (HAVE_ZLIB)
193 
195 {
196  buf_close ();
197 }
198 
199 // FIXME: I'm sure there is room for improvement here...
200 
203 {
204  if (m_f)
205  return (c != traits_type::eof ()) ? gzputc (m_f, c) : flush ();
206  else
207  return traits_type::not_eof (c);
208 }
209 
212 {
213  if (m_f)
214  {
215  int_type c = gzgetc (m_f);
216 
217  if (! bump && c != traits_type::eof ())
218  gzungetc (c, m_f);
219 
220  return c;
221  }
222  else
223  return traits_type::eof ();
224 }
225 
228 {
229  return ((c != traits_type::eof () && m_f)
230  ? gzungetc (c, m_f) : traits_type::not_eof (c));
231 }
232 
233 std::streamsize
234 c_zfile_ptr_buf::xsputn (const char *s, std::streamsize n)
235 {
236  if (m_f)
237  return gzwrite (m_f, s, n);
238  else
239  return 0;
240 }
241 
242 std::streamsize
243 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n)
244 {
245  if (m_f)
246  return gzread (m_f, s, n);
247  else
248  return 0;
249 }
250 
251 std::streampos
252 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */,
253  std::ios::seekdir /* dir */,
254  std::ios::openmode)
255 {
256  // FIXME
257 #if 0
258  if (m_f)
259  {
260  gzseek (m_f, offset, seekdir_to_whence (dir));
261 
262  return gztell (m_f);
263  }
264  else
265  return 0;
266 #endif
267  return -1;
268 }
269 
270 std::streampos
271 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
272 {
273  // FIXME
274 #if 0
275  if (m_f)
276  {
277  gzseek (m_f, offset, SEEK_SET);
278 
279  return gztell (m_f);
280  }
281  else
282  return 0;
283 #endif
284  return -1;
285 }
286 
287 int
289 {
290  flush ();
291 
292  return 0;
293 }
294 
295 int
297 {
298  // FIXME: do we need something more complex here, passing
299  // something other than 0 for the second argument to gzflush and
300  // checking the return value, etc.?
301 
302  return m_f ? gzflush (m_f, 0) : traits_type::eof ();
303 }
304 
305 int
307 {
308  int retval = -1;
309 
310  flush ();
311 
312  if (m_f)
313  {
314  retval = m_cf (m_f);
315  m_f = nullptr;
316  }
317 
318  return retval;
319 }
320 
321 #endif
322 
OCTAVE_END_NAMESPACE(octave)
#define SEEK_SET
#define SEEK_CUR
static int seekdir_to_whence(std::ios::seekdir dir)
#define SEEK_END
std::streampos seekoff(std::streamoff, std::ios::seekdir, std::ios::openmode=std::ios::in|std::ios::out)
std::streamsize xsgetn(char *, std::streamsize)
int_type underflow_common(bool)
int_type overflow(int_type)
std::streamsize xsputn(const char *, std::streamsize)
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)
static int file_close(FILE *m_f)
int seek(off_t offset, int origin)
std::streambuf::int_type int_type
int_type pbackfail(int_type)
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)
std::streamsize xsgetn(char *, std::streamsize)
std::streampos seekoff(std::streamoff, std::ios::seekdir, std::ios::openmode=std::ios::in|std::ios::out)
int_type underflow_common(bool)
int_type pbackfail(int_type)
int_type overflow(int_type)
std::streamsize xsputn(const char *, std::streamsize)
std::streambuf::int_type int_type
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
off_t octave_ftello_wrapper(FILE *fp)
octave_idx_type n
Definition: mx-inlines.cc:753