GNU Octave  6.2.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-2021 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 
36 #if ! defined (SEEK_SET)
37 # define SEEK_SET 0
38 #endif
39 
40 #if ! defined (SEEK_CUR)
41 # define SEEK_CUR 1
42 #endif
43 
44 #if ! defined (SEEK_END)
45 # define SEEK_END 2
46 #endif
47 
49 {
50  buf_close ();
51 }
52 
53 // FIXME: I'm sure there is room for improvement here...
54 
57 {
58  if (f)
59  return (c != traits_type::eof ()) ? std::fputc (c, f) : flush ();
60  else
61  return traits_type::not_eof (c);
62 }
63 
66 {
67  if (f)
68  {
69  int_type c = std::fgetc (f);
70 
71  if (! bump && c != traits_type::eof ())
72  ungetc (c, f);
73 
74  return c;
75  }
76  else
77  return traits_type::eof ();
78 }
79 
82 {
83  return ((c != traits_type::eof () && f)
84  ? ungetc (c, f) : traits_type::not_eof (c));
85 }
86 
87 std::streamsize
88 c_file_ptr_buf::xsputn (const char *s, std::streamsize n)
89 {
90  if (f)
91  return std::fwrite (s, 1, n, f);
92  else
93  return 0;
94 }
95 
96 std::streamsize
97 c_file_ptr_buf::xsgetn (char *s, std::streamsize n)
98 {
99  if (f)
100  return std::fread (s, 1, n, f);
101  else
102  return 0;
103 }
104 
105 static inline int
106 seekdir_to_whence (std::ios::seekdir dir)
107 {
108  return (dir == std::ios::beg
109  ? SEEK_SET : (dir == std::ios::cur
110  ? SEEK_CUR : (dir == std::ios::end
111  ? SEEK_END : dir)));
112 }
113 
114 std::streampos
115 c_file_ptr_buf::seekoff (std::streamoff offset,
116  std::ios::seekdir dir,
117  std::ios::openmode)
118 {
119  if (f)
120  {
121  octave_fseeko_wrapper (f, offset, seekdir_to_whence (dir));
122 
123  return octave_ftello_wrapper (f);
124  }
125  else
126  return 0;
127 }
128 
129 std::streampos
130 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode)
131 {
132  if (f)
133  {
134  octave_fseeko_wrapper (f, offset, SEEK_SET);
135 
136  return octave_ftello_wrapper (f);
137  }
138  else
139  return 0;
140 }
141 
142 int
144 {
145  flush ();
146 
147  return 0;
148 }
149 
150 int
152 {
153  return f ? std::fflush (f) : traits_type::eof ();
154 }
155 
156 int
158 {
159  int retval = -1;
160 
161  flush ();
162 
163  if (f)
164  {
165  retval = cf (f);
166  f = nullptr;
167  }
168 
169  return retval;
170 }
171 
172 int
173 c_file_ptr_buf::seek (off_t offset, int origin)
174 {
175  return f ? octave_fseeko_wrapper (f, offset, origin) : -1;
176 }
177 
178 off_t
180 {
181  return f ? octave_ftello_wrapper (f) : -1;
182 }
183 
184 int
186 {
187  return std::fclose (f);
188 }
189 
190 #if defined (HAVE_ZLIB)
191 
193 {
194  buf_close ();
195 }
196 
197 // FIXME: I'm sure there is room for improvement here...
198 
201 {
202  if (f)
203  return (c != traits_type::eof ()) ? gzputc (f, c) : flush ();
204  else
205  return traits_type::not_eof (c);
206 }
207 
210 {
211  if (f)
212  {
213  int_type c = gzgetc (f);
214 
215  if (! bump && c != traits_type::eof ())
216  gzungetc (c, f);
217 
218  return c;
219  }
220  else
221  return traits_type::eof ();
222 }
223 
226 {
227  return ((c != traits_type::eof () && f)
228  ? gzungetc (c, f) : traits_type::not_eof (c));
229 }
230 
231 std::streamsize
232 c_zfile_ptr_buf::xsputn (const char *s, std::streamsize n)
233 {
234  if (f)
235  return gzwrite (f, s, n);
236  else
237  return 0;
238 }
239 
240 std::streamsize
241 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n)
242 {
243  if (f)
244  return gzread (f, s, n);
245  else
246  return 0;
247 }
248 
249 std::streampos
250 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */,
251  std::ios::seekdir /* dir */,
252  std::ios::openmode)
253 {
254  // FIXME
255 #if 0
256  if (f)
257  {
258  gzseek (f, offset, seekdir_to_whence (dir));
259 
260  return gztell (f);
261  }
262  else
263  return 0;
264 #endif
265  return -1;
266 }
267 
268 std::streampos
269 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
270 {
271  // FIXME
272 #if 0
273  if (f)
274  {
275  gzseek (f, offset, SEEK_SET);
276 
277  return gztell (f);
278  }
279  else
280  return 0;
281 #endif
282  return -1;
283 }
284 
285 int
287 {
288  flush ();
289 
290  return 0;
291 }
292 
293 int
295 {
296  // FIXME: do we need something more complex here, passing
297  // something other than 0 for the second argument to gzflush and
298  // checking the return value, etc.?
299 
300  return f ? gzflush (f, 0) : traits_type::eof ();
301 }
302 
303 int
305 {
306  int retval = -1;
307 
308  flush ();
309 
310  if (f)
311  {
312  retval = cf (f);
313  f = nullptr;
314  }
315 
316  return retval;
317 }
318 
319 #endif
#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 *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
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
off_t octave_ftello_wrapper(FILE *fp)
F77_RET_T const F77_DBLE const F77_DBLE * f
octave_idx_type n
Definition: mx-inlines.cc:753
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811