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