GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
c-file-ptr-stream.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 
29 #include "c-file-ptr-stream.h"
30 
31 #ifndef SEEK_SET
32 #define SEEK_SET 0
33 #endif
34 
35 #ifndef SEEK_CUR
36 #define SEEK_CUR 1
37 #endif
38 
39 #ifndef SEEK_END
40 #define SEEK_END 2
41 #endif
42 
44 {
45  buf_close ();
46 }
47 
48 // FIXME: I'm sure there is room for improvement here...
49 
52 {
53 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
54  if (f)
55  return (c != traits_type::eof ()) ? gnulib::fputc (c, f) : flush ();
56  else
57  return traits_type::not_eof (c);
58 #else
59  if (f)
60  return (c != EOF) ? gnulib::fputc (c, f) : flush ();
61  else
62  return EOF;
63 #endif
64 }
65 
68 {
69  if (f)
70  {
71  int_type c = gnulib::fgetc (f);
72 
73  if (! bump
74 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
75  && c != traits_type::eof ())
76 #else
77  && c != EOF)
78 #endif
79  ungetc (c, f);
80 
81  return c;
82  }
83  else
84 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
85  return traits_type::eof ();
86 #else
87  return EOF;
88 #endif
89 }
90 
93 {
94 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
95  return (c != traits_type::eof () && f) ? ungetc (c, f)
96  : traits_type::not_eof (c);
97 #else
98  return (c != EOF && f) ? ungetc (c, f) : EOF;
99 #endif
100 }
101 
102 std::streamsize
103 c_file_ptr_buf::xsputn (const char* s, std::streamsize n)
104 {
105  if (f)
106  return gnulib::fwrite (s, 1, n, f);
107  else
108  return 0;
109 }
110 
111 std::streamsize
112 c_file_ptr_buf::xsgetn (char *s, std::streamsize n)
113 {
114  if (f)
115  return gnulib::fread (s, 1, n, f);
116  else
117  return 0;
118 }
119 
120 static inline int
121 seekdir_to_whence (std::ios::seekdir dir)
122 {
123  return ((dir == std::ios::beg) ? SEEK_SET :
124  (dir == std::ios::cur) ? SEEK_CUR :
125  (dir == std::ios::end) ? SEEK_END :
126  dir);
127 }
128 
129 std::streampos
130 c_file_ptr_buf::seekoff (std::streamoff /* offset */,
131  std::ios::seekdir /* dir */,
132  std::ios::openmode)
133 {
134  // FIXME
135 #if 0
136  if (f)
137  {
138  fseek (f, offset, seekdir_to_whence (dir));
139 
140  return ftell (f);
141  }
142  else
143  return 0;
144 #endif
145  return -1;
146 }
147 
148 std::streampos
149 c_file_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
150 {
151  // FIXME
152 #if 0
153  if (f)
154  {
155  fseek (f, offset, SEEK_SET);
156 
157  return ftell (f);
158  }
159  else
160  return 0;
161 #endif
162  return -1;
163 }
164 
165 int
167 {
168  flush ();
169 
170  return 0;
171 }
172 
173 int
175 {
176  return f ? gnulib::fflush (f) : EOF;
177 }
178 
179 int
181 {
182  int retval = -1;
183 
184  flush ();
185 
186  if (f)
187  {
188  retval = cf (f);
189  f = 0;
190  }
191 
192  return retval;
193 }
194 
195 int
196 c_file_ptr_buf::seek (off_t offset, int origin)
197 {
198  return f ? gnulib::fseeko (f, offset, origin) : -1;
199 }
200 
201 off_t
203 {
204  return f ? gnulib::ftello (f) : -1;
205 }
206 
207 int
209 {
210  return gnulib::fclose (f);
211 }
212 
213 #ifdef HAVE_ZLIB
214 
216 {
217  buf_close ();
218 }
219 
220 // FIXME: I'm sure there is room for improvement here...
221 
224 {
225 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
226  if (f)
227  return (c != traits_type::eof ()) ? gzputc (f, c) : flush ();
228  else
229  return traits_type::not_eof (c);
230 #else
231  if (f)
232  return (c != EOF) ? gzputc (f, c) : flush ();
233  else
234  return EOF;
235 #endif
236 }
237 
240 {
241  if (f)
242  {
243  int_type c = gzgetc (f);
244 
245  if (! bump
246 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
247  && c != traits_type::eof ())
248 #else
249  && c != EOF)
250 #endif
251  gzungetc (c, f);
252 
253  return c;
254  }
255  else
256 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
257  return traits_type::eof ();
258 #else
259  return EOF;
260 #endif
261 }
262 
265 {
266 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
267  return (c != traits_type::eof () && f) ? gzungetc (c, f)
268  : traits_type::not_eof (c);
269 #else
270  return (c != EOF && f) ? gzungetc (c, f) : EOF;
271 #endif
272 }
273 
274 std::streamsize
275 c_zfile_ptr_buf::xsputn (const char* s, std::streamsize n)
276 {
277  if (f)
278  return gzwrite (f, s, n);
279  else
280  return 0;
281 }
282 
283 std::streamsize
284 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n)
285 {
286  if (f)
287  return gzread (f, s, n);
288  else
289  return 0;
290 }
291 
292 std::streampos
293 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */,
294  std::ios::seekdir /* dir */,
295  std::ios::openmode)
296 {
297  // FIXME
298 #if 0
299  if (f)
300  {
301  gzseek (f, offset, seekdir_to_whence (dir));
302 
303  return gztell (f);
304  }
305  else
306  return 0;
307 #endif
308  return -1;
309 }
310 
311 std::streampos
312 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
313 {
314  // FIXME
315 #if 0
316  if (f)
317  {
318  gzseek (f, offset, SEEK_SET);
319 
320  return gztell (f);
321  }
322  else
323  return 0;
324 #endif
325  return -1;
326 }
327 
328 int
330 {
331  flush ();
332 
333  return 0;
334 }
335 
336 int
338 {
339  // FIXME: do we need something more complex here, passing
340  // something other than 0 for the second argument to gzflush and
341  // checking the return value, etc.?
342 
343  return f ? gzflush (f, 0) : EOF;
344 }
345 
346 int
348 {
349  int retval = -1;
350 
351  flush ();
352 
353  if (f)
354  {
355  retval = cf (f);
356  f = 0;
357  }
358 
359  return retval;
360 }
361 
362 #endif
std::streamsize xsputn(const char *, std::streamsize)
std::streampos seekpos(std::streampos, std::ios::openmode=std::ios::in|std::ios::out)
#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)
F77_RET_T const double const double * f
std::streamsize xsgetn(char *, std::streamsize)
#define SEEK_END
int_type pbackfail(int_type)
int_type pbackfail(int_type)
static int file_close(FILE *f)
int_type underflow_common(bool)
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)