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