3628
|
1 /* |
|
2 |
|
3 Copyright (C) 2000 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3628
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_c_file_ptr_stream_h) |
|
25 #define octave_c_file_ptr_stream_h 1 |
|
26 |
|
27 #include <iostream> |
3775
|
28 #include <cstdio> |
3628
|
29 |
|
30 class |
4310
|
31 c_file_ptr_buf : public std::streambuf |
3628
|
32 { |
|
33 public: |
|
34 |
3775
|
35 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
36 typedef int int_type; |
3944
|
37 #else |
4310
|
38 typedef std::streambuf::int_type int_type; |
3775
|
39 #endif |
|
40 |
3716
|
41 typedef int (*close_fcn) (FILE *); |
|
42 |
4797
|
43 FILE* stdiofile (void) { return f; } |
3628
|
44 |
3951
|
45 c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = fclose) |
4310
|
46 : std::streambuf (), f (f_arg), cf (cf_arg) |
3775
|
47 { } |
3628
|
48 |
|
49 ~c_file_ptr_buf (void); |
|
50 |
3775
|
51 int_type overflow (int_type); |
3628
|
52 |
4310
|
53 int_type underflow (void) { return underflow_common (false); } |
3628
|
54 |
4310
|
55 int_type uflow (void) { return underflow_common (true); } |
3628
|
56 |
3775
|
57 int_type pbackfail (int_type); |
3628
|
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); |
3644
|
70 |
3652
|
71 int flush (void); |
|
72 |
|
73 int close (void); |
|
74 |
4310
|
75 int file_number () const { return f ? fileno (f) : -1; } |
3775
|
76 |
4797
|
77 int seek (long offset, int origin) |
|
78 { return f ? fseek (f, offset, origin) : -1; } |
|
79 |
|
80 long tell (void) { return f ? ftell (f) : -1; } |
|
81 |
4888
|
82 void clear (void) { if (f) clearerr (f); } |
|
83 |
3951
|
84 static int fclose (FILE *f) { return ::fclose (f); } |
|
85 |
3644
|
86 protected: |
|
87 |
|
88 FILE *f; |
3716
|
89 |
|
90 close_fcn cf; |
3775
|
91 |
|
92 private: |
|
93 |
4310
|
94 int_type underflow_common (bool); |
3628
|
95 }; |
|
96 |
5325
|
97 // XXX FIXME XXX -- the following three classes could probably share |
|
98 // some code... |
|
99 |
|
100 template <typename STREAM_T, typename FILE_T, typename BUF_T> |
3628
|
101 class |
5325
|
102 c_file_ptr_stream : public STREAM_T |
3628
|
103 { |
3644
|
104 public: |
|
105 |
5325
|
106 c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::fclose) |
|
107 : STREAM_T (0), buf (new BUF_T (f, cf)) { init (buf); } |
3644
|
108 |
5325
|
109 ~c_file_ptr_stream (void) { delete buf; buf = 0; } |
3644
|
110 |
5325
|
111 BUF_T *rdbuf (void) { return buf; } |
3644
|
112 |
4326
|
113 void close (void) { if (buf) buf->close (); } |
3652
|
114 |
4797
|
115 int seek (long offset, int origin) |
|
116 { return buf ? buf->seek (offset, origin) : -1; } |
|
117 |
|
118 long tell (void) { return buf ? buf->tell () : -1; } |
|
119 |
5325
|
120 void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); } |
4888
|
121 |
3628
|
122 private: |
|
123 |
5325
|
124 BUF_T *buf; |
3628
|
125 }; |
|
126 |
5325
|
127 typedef c_file_ptr_stream<std::istream, FILE *, c_file_ptr_buf> i_c_file_ptr_stream; |
|
128 typedef c_file_ptr_stream<std::ostream, FILE *, c_file_ptr_buf> o_c_file_ptr_stream; |
|
129 typedef c_file_ptr_stream<std::iostream, FILE *, c_file_ptr_buf> io_c_file_ptr_stream; |
|
130 |
|
131 #ifdef HAVE_ZLIB |
|
132 |
|
133 #ifdef HAVE_ZLIB_H |
|
134 #include <zlib.h> |
|
135 #endif |
|
136 |
3628
|
137 class |
5325
|
138 c_zfile_ptr_buf : public std::streambuf |
3628
|
139 { |
3644
|
140 public: |
|
141 |
5325
|
142 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
143 typedef int int_type; |
|
144 #else |
|
145 typedef std::streambuf::int_type int_type; |
|
146 #endif |
|
147 |
|
148 typedef int (*close_fcn) (gzFile); |
|
149 |
|
150 gzFile stdiofile (void) { return f; } |
|
151 |
|
152 c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = fclose) |
|
153 : std::streambuf (), f (f_arg), cf (cf_arg) |
|
154 { } |
|
155 |
|
156 ~c_zfile_ptr_buf (void); |
|
157 |
|
158 int_type overflow (int_type); |
|
159 |
|
160 int_type underflow (void) { return underflow_common (false); } |
3644
|
161 |
5325
|
162 int_type uflow (void) { return underflow_common (true); } |
|
163 |
|
164 int_type pbackfail (int_type); |
|
165 |
|
166 std::streamsize xsputn (const char*, std::streamsize); |
|
167 |
|
168 std::streamsize xsgetn (char *, std::streamsize); |
3644
|
169 |
5325
|
170 std::streampos seekoff (std::streamoff, std::ios::seekdir, |
|
171 std::ios::openmode = std::ios::in | std::ios::out); |
|
172 |
|
173 std::streampos seekpos (std::streampos, |
|
174 std::ios::openmode = std::ios::in | std::ios::out); |
3644
|
175 |
5325
|
176 int sync (void); |
|
177 |
|
178 int flush (void); |
|
179 |
|
180 int close (void); |
|
181 |
|
182 int file_number () const { return -1; } |
4326
|
183 |
4797
|
184 int seek (long offset, int origin) |
5325
|
185 { return f ? gzseek (f, offset, origin) : -1; } |
|
186 |
|
187 long tell (void) { return f ? gztell (f) : -1; } |
|
188 |
|
189 void clear (void) { if (f) gzclearerr (f); } |
4797
|
190 |
5325
|
191 static int fclose (gzFile f) { return ::gzclose (f); } |
|
192 |
|
193 protected: |
4797
|
194 |
5325
|
195 gzFile f; |
|
196 |
|
197 close_fcn cf; |
4888
|
198 |
4326
|
199 private: |
|
200 |
5325
|
201 int_type underflow_common (bool); |
4326
|
202 }; |
|
203 |
5325
|
204 typedef c_file_ptr_stream<std::istream, gzFile, c_zfile_ptr_buf> i_c_zfile_ptr_stream; |
|
205 typedef c_file_ptr_stream<std::ostream, gzFile, c_zfile_ptr_buf> o_c_zfile_ptr_stream; |
|
206 typedef c_file_ptr_stream<std::iostream, gzFile, c_zfile_ptr_buf> io_c_zfile_ptr_stream; |
4326
|
207 |
5325
|
208 #endif |
3628
|
209 |
|
210 #endif |
|
211 |
|
212 /* |
|
213 ;;; Local Variables: *** |
|
214 ;;; mode: C++ *** |
|
215 ;;; End: *** |
|
216 */ |