GNU Octave  4.4.1
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-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 (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "filepos-wrappers.h"
30 
31 #include "c-file-ptr-stream.h"
32 
33 #if ! defined (SEEK_SET)
34 # define SEEK_SET 0
35 #endif
36 
37 #if ! defined (SEEK_CUR)
38 # define SEEK_CUR 1
39 #endif
40 
41 #if ! defined (SEEK_END)
42 # define SEEK_END 2
43 #endif
44 
46 {
47  buf_close ();
48 }
49 
50 // FIXME: I'm sure there is room for improvement here...
51 
54 {
55  if (f)
56  return (c != traits_type::eof ()) ? std::fputc (c, f) : flush ();
57  else
58  return traits_type::not_eof (c);
59 }
60 
63 {
64  if (f)
65  {
66  int_type c = std::fgetc (f);
67 
68  if (! bump && c != traits_type::eof ())
69  ungetc (c, f);
70 
71  return c;
72  }
73  else
74  return traits_type::eof ();
75 }
76 
79 {
80  return ((c != traits_type::eof () && f)
81  ? ungetc (c, f) : traits_type::not_eof (c));
82 }
83 
84 std::streamsize
85 c_file_ptr_buf::xsputn (const char *s, std::streamsize n)
86 {
87  if (f)
88  return std::fwrite (s, 1, n, f);
89  else
90  return 0;
91 }
92 
93 std::streamsize
94 c_file_ptr_buf::xsgetn (char *s, std::streamsize n)
95 {
96  if (f)
97  return std::fread (s, 1, n, f);
98  else
99  return 0;
100 }
101 
102 static inline int
103 seekdir_to_whence (std::ios::seekdir dir)
104 {
105  return (dir == std::ios::beg
106  ? SEEK_SET : (dir == std::ios::cur
107  ? SEEK_CUR : (dir == std::ios::end
108  ? SEEK_END : dir)));
109 }
110 
111 std::streampos
112 c_file_ptr_buf::seekoff (std::streamoff offset,
113  std::ios::seekdir dir,
114  std::ios::openmode)
115 {
116  if (f)
117  {
118  octave_fseeko_wrapper (f, offset, seekdir_to_whence (dir));
119 
120  return octave_ftello_wrapper (f);
121  }
122  else
123  return 0;
124 }
125 
126 std::streampos
127 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode)
128 {
129  if (f)
130  {
131  octave_fseeko_wrapper (f, offset, SEEK_SET);
132 
133  return octave_ftello_wrapper (f);
134  }
135  else
136  return 0;
137 }
138 
139 int
141 {
142  flush ();
143 
144  return 0;
145 }
146 
147 int
149 {
150  return f ? std::fflush (f) : traits_type::eof ();
151 }
152 
153 int
155 {
156  int retval = -1;
157 
158  flush ();
159 
160  if (f)
161  {
162  retval = cf (f);
163  f = nullptr;
164  }
165 
166  return retval;
167 }
168 
169 int
170 c_file_ptr_buf::seek (off_t offset, int origin)
171 {
172  return f ? octave_fseeko_wrapper (f, offset, origin) : -1;
173 }
174 
175 off_t
177 {
178  return f ? octave_ftello_wrapper (f) : -1;
179 }
180 
181 int
183 {
184  return std::fclose (f);
185 }
186 
187 #if defined (HAVE_ZLIB)
188 
190 {
191  buf_close ();
192 }
193 
194 // FIXME: I'm sure there is room for improvement here...
195 
198 {
199  if (f)
200  return (c != traits_type::eof ()) ? gzputc (f, c) : flush ();
201  else
202  return traits_type::not_eof (c);
203 }
204 
207 {
208  if (f)
209  {
210  int_type c = gzgetc (f);
211 
212  if (! bump && c != traits_type::eof ())
213  gzungetc (c, f);
214 
215  return c;
216  }
217  else
218  return traits_type::eof ();
219 }
220 
223 {
224  return ((c != traits_type::eof () && f)
225  ? gzungetc (c, f) : traits_type::not_eof (c));
226 }
227 
228 std::streamsize
229 c_zfile_ptr_buf::xsputn (const char *s, std::streamsize n)
230 {
231  if (f)
232  return gzwrite (f, s, n);
233  else
234  return 0;
235 }
236 
237 std::streamsize
238 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n)
239 {
240  if (f)
241  return gzread (f, s, n);
242  else
243  return 0;
244 }
245 
246 std::streampos
247 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */,
248  std::ios::seekdir /* dir */,
249  std::ios::openmode)
250 {
251  // FIXME
252 #if 0
253  if (f)
254  {
255  gzseek (f, offset, seekdir_to_whence (dir));
256 
257  return gztell (f);
258  }
259  else
260  return 0;
261 #endif
262  return -1;
263 }
264 
265 std::streampos
266 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
267 {
268  // FIXME
269 #if 0
270  if (f)
271  {
272  gzseek (f, offset, SEEK_SET);
273 
274  return gztell (f);
275  }
276  else
277  return 0;
278 #endif
279  return -1;
280 }
281 
282 int
284 {
285  flush ();
286 
287  return 0;
288 }
289 
290 int
292 {
293  // FIXME: do we need something more complex here, passing
294  // something other than 0 for the second argument to gzflush and
295  // checking the return value, etc.?
296 
297  return f ? gzflush (f, 0) : traits_type::eof ();
298 }
299 
300 int
302 {
303  int retval = -1;
304 
305  flush ();
306 
307  if (f)
308  {
309  retval = cf (f);
310  f = nullptr;
311  }
312 
313  return retval;
314 }
315 
316 #endif
std::streamsize xsputn(const char *, std::streamsize)
off_t octave_ftello_wrapper(FILE *fp)
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
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
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)
std::streambuf::int_type int_type
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
s
Definition: file-io.cc:2729
#define SEEK_CUR
int_type underflow_common(bool)
std::streampos seekoff(std::streamoff, std::ios::seekdir, std::ios::openmode=std::ios::in|std::ios::out)
std::streamsize xsputn(const char *, std::streamsize)
std::streamsize xsgetn(char *, std::streamsize)
static int seekdir_to_whence(std::ios::seekdir dir)
OCTAVE_EXPORT octave_value_list or both For fclose
Definition: file-io.cc:676
std::streamsize xsgetn(char *, std::streamsize)
#define SEEK_END
octave_value retval
Definition: data.cc:6246
int_type pbackfail(int_type)
int_type pbackfail(int_type)
static int file_close(FILE *f)
int_type underflow_common(bool)
std::streambuf::int_type int_type
std::streampos seekoff(std::streamoff, std::ios::seekdir, std::ios::openmode=std::ios::in|std::ios::out)
int seek(off_t offset, int origin)
#define SEEK_SET
int_type overflow(int_type)
int_type overflow(int_type)
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)