GNU Octave  3.8.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.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2013 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 #if !defined (octave_c_file_ptr_stream_h)
24 #define octave_c_file_ptr_stream_h 1
25 
26 #include <cstdio>
27 
28 #include <iosfwd>
29 
30 class
31 c_file_ptr_buf : public std::streambuf
32 {
33 public:
34 
35 #if !defined (CXX_ISO_COMPLIANT_LIBRARY)
36  typedef int int_type;
37 #else
38  typedef std::streambuf::int_type int_type;
39 #endif
40 
41  typedef int (*close_fcn) (FILE *);
42 
43  FILE* stdiofile (void) { return f; }
44 
45  c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = file_close)
46  : std::streambuf (), f (f_arg), cf (cf_arg)
47  { }
48 
49  ~c_file_ptr_buf (void);
50 
51  int_type overflow (int_type);
52 
53  int_type underflow (void) { return underflow_common (false); }
54 
55  int_type uflow (void) { return underflow_common (true); }
56 
57  int_type pbackfail (int_type);
58 
59  std::streamsize xsputn (const char*, std::streamsize);
60 
61  std::streamsize xsgetn (char *, std::streamsize);
62 
63  std::streampos seekoff (std::streamoff, std::ios::seekdir,
64  std::ios::openmode = std::ios::in | std::ios::out);
65 
66  std::streampos seekpos (std::streampos,
67  std::ios::openmode = std::ios::in | std::ios::out);
68 
69  int sync (void);
70 
71  int flush (void);
72 
73  int buf_close (void);
74 
75  int file_number () const { return f ? fileno (f) : -1; }
76 
77  int seek (off_t offset, int origin);
78 
79  off_t tell (void);
80 
81  void clear (void) { if (f) clearerr (f); }
82 
83  static int file_close (FILE *f);
84 
85 protected:
86 
87  FILE *f;
88 
90 
91 private:
92 
93  int_type underflow_common (bool);
94 
95  // No copying!
96 
98 
99  c_file_ptr_buf& operator = (const c_file_ptr_buf&);
100 };
101 
102 // FIXME: the following three classes could probably share some code...
103 
104 template <typename STREAM_T, typename FILE_T, typename BUF_T>
105 class
106 c_file_ptr_stream : public STREAM_T
107 {
108 public:
109 
110  c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::file_close)
111  : STREAM_T (0), buf (new BUF_T (f, cf)) { STREAM_T::init (buf); }
112 
113  ~c_file_ptr_stream (void) { delete buf; buf = 0; }
114 
115  BUF_T *rdbuf (void) { return buf; }
116 
117  void stream_close (void) { if (buf) buf->buf_close (); }
118 
119  int seek (off_t offset, int origin)
120  { return buf ? buf->seek (offset, origin) : -1; }
121 
122  off_t tell (void) { return buf ? buf->tell () : -1; }
123 
124  void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); }
125 
126 private:
127 
128  BUF_T *buf;
129 
130  // No copying!
131 
133 
134  c_file_ptr_stream& operator = (const c_file_ptr_stream&);
135 };
136 
143 
144 #ifdef HAVE_ZLIB
145 
146 #ifdef HAVE_ZLIB_H
147 #include <zlib.h>
148 #endif
149 
150 class
151 c_zfile_ptr_buf : public std::streambuf
152 {
153 public:
154 
155 #if !defined (CXX_ISO_COMPLIANT_LIBRARY)
156  typedef int int_type;
157 #else
158  typedef std::streambuf::int_type int_type;
159 #endif
160 
161  typedef int (*close_fcn) (gzFile);
162 
163  gzFile stdiofile (void) { return f; }
164 
165  c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = file_close)
166  : std::streambuf (), f (f_arg), cf (cf_arg)
167  { }
168 
169  ~c_zfile_ptr_buf (void);
170 
171  int_type overflow (int_type);
172 
173  int_type underflow (void) { return underflow_common (false); }
174 
175  int_type uflow (void) { return underflow_common (true); }
176 
177  int_type pbackfail (int_type);
178 
179  std::streamsize xsputn (const char*, std::streamsize);
180 
181  std::streamsize xsgetn (char *, std::streamsize);
182 
183  std::streampos seekoff (std::streamoff, std::ios::seekdir,
184  std::ios::openmode = std::ios::in | std::ios::out);
185 
186  std::streampos seekpos (std::streampos,
187  std::ios::openmode = std::ios::in | std::ios::out);
188 
189  int sync (void);
190 
191  int flush (void);
192 
193  int buf_close (void);
194 
195  int file_number () const { return -1; }
196 
197  int seek (off_t offset, int origin)
198  { return f ? gzseek (f, offset, origin) >= 0 : -1; }
199 
200  off_t tell (void) { return f ? gztell (f) : -1; }
201 
202  void clear (void) { if (f) gzclearerr (f); }
203 
204  static int file_close (gzFile f) { return ::gzclose (f); }
205 
206 protected:
207 
208  gzFile f;
209 
211 
212 private:
213 
214  int_type underflow_common (bool);
215 
216  // No copying!
217 
219 
220  c_zfile_ptr_buf& operator = (const c_zfile_ptr_buf&);
221 };
222 
229 
230 #endif
231 
232 #endif