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 |
|
97 class |
|
98 i_c_file_ptr_stream : public std::istream |
|
99 { |
3644
|
100 public: |
|
101 |
3951
|
102 i_c_file_ptr_stream (FILE* f, |
|
103 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
3775
|
104 : std::istream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
3644
|
105 |
|
106 ~i_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
107 |
|
108 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
109 |
4326
|
110 void close (void) { if (buf) buf->close (); } |
3652
|
111 |
4797
|
112 int seek (long offset, int origin) |
|
113 { return buf ? buf->seek (offset, origin) : -1; } |
|
114 |
|
115 long tell (void) { return buf ? buf->tell () : -1; } |
|
116 |
4888
|
117 void clear (void) { if (buf) buf->clear (); std::istream::clear (); } |
|
118 |
3628
|
119 private: |
|
120 |
3644
|
121 c_file_ptr_buf *buf; |
3628
|
122 }; |
|
123 |
|
124 class |
|
125 o_c_file_ptr_stream : public std::ostream |
|
126 { |
3644
|
127 public: |
|
128 |
3951
|
129 o_c_file_ptr_stream (FILE* f, |
|
130 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
3775
|
131 : std::ostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
3644
|
132 |
|
133 ~o_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
134 |
|
135 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
136 |
4326
|
137 void close (void) { if (buf) buf->close (); } |
|
138 |
4797
|
139 int seek (long offset, int origin) |
|
140 { return buf ? buf->seek (offset, origin) : -1; } |
|
141 |
|
142 long tell (void) { return buf ? buf->tell () : -1; } |
|
143 |
4888
|
144 void clear (void) { if (buf) buf->clear (); std::ostream::clear (); } |
|
145 |
4326
|
146 private: |
|
147 |
|
148 c_file_ptr_buf *buf; |
|
149 }; |
|
150 |
|
151 class |
|
152 io_c_file_ptr_stream : public std::iostream |
|
153 { |
|
154 public: |
|
155 |
|
156 io_c_file_ptr_stream (FILE* f, |
|
157 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
|
158 : std::iostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
|
159 |
|
160 ~io_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
161 |
|
162 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
163 |
|
164 void close (void) { if (buf) buf->close (); } |
3652
|
165 |
4797
|
166 int seek (long offset, int origin) |
|
167 { return buf ? buf->seek (offset, origin) : -1; } |
|
168 |
|
169 long tell (void) { return buf ? buf->tell () : -1; } |
|
170 |
4888
|
171 void clear (void) { if (buf) buf->clear (); std::iostream::clear (); } |
|
172 |
3628
|
173 private: |
|
174 |
3644
|
175 c_file_ptr_buf *buf; |
3628
|
176 }; |
|
177 |
|
178 #endif |
|
179 |
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |