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 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3628
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <iostream> |
3775
|
31 #include <cstdio> |
3628
|
32 |
|
33 class |
4310
|
34 c_file_ptr_buf : public std::streambuf |
3628
|
35 { |
|
36 public: |
|
37 |
3775
|
38 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
39 typedef int int_type; |
3944
|
40 #else |
4310
|
41 typedef std::streambuf::int_type int_type; |
3775
|
42 #endif |
|
43 |
3716
|
44 typedef int (*close_fcn) (FILE *); |
|
45 |
4797
|
46 FILE* stdiofile (void) { return f; } |
3628
|
47 |
3951
|
48 c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = fclose) |
4310
|
49 : std::streambuf (), f (f_arg), cf (cf_arg) |
3775
|
50 { } |
3628
|
51 |
|
52 ~c_file_ptr_buf (void); |
|
53 |
3775
|
54 int_type overflow (int_type); |
3628
|
55 |
4310
|
56 int_type underflow (void) { return underflow_common (false); } |
3628
|
57 |
4310
|
58 int_type uflow (void) { return underflow_common (true); } |
3628
|
59 |
3775
|
60 int_type pbackfail (int_type); |
3628
|
61 |
|
62 std::streamsize xsputn (const char*, std::streamsize); |
|
63 |
|
64 std::streamsize xsgetn (char *, std::streamsize); |
|
65 |
|
66 std::streampos seekoff (std::streamoff, std::ios::seekdir, |
|
67 std::ios::openmode = std::ios::in | std::ios::out); |
|
68 |
|
69 std::streampos seekpos (std::streampos, |
|
70 std::ios::openmode = std::ios::in | std::ios::out); |
|
71 |
|
72 int sync (void); |
3644
|
73 |
3652
|
74 int flush (void); |
|
75 |
|
76 int close (void); |
|
77 |
4310
|
78 int file_number () const { return f ? fileno (f) : -1; } |
3775
|
79 |
4797
|
80 int seek (long offset, int origin) |
|
81 { return f ? fseek (f, offset, origin) : -1; } |
|
82 |
|
83 long tell (void) { return f ? ftell (f) : -1; } |
|
84 |
3951
|
85 static int fclose (FILE *f) { return ::fclose (f); } |
|
86 |
3644
|
87 protected: |
|
88 |
|
89 FILE *f; |
3716
|
90 |
|
91 close_fcn cf; |
3775
|
92 |
|
93 private: |
|
94 |
4310
|
95 int_type underflow_common (bool); |
3628
|
96 }; |
|
97 |
|
98 class |
|
99 i_c_file_ptr_stream : public std::istream |
|
100 { |
3644
|
101 public: |
|
102 |
3951
|
103 i_c_file_ptr_stream (FILE* f, |
|
104 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
3775
|
105 : std::istream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
3644
|
106 |
|
107 ~i_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
108 |
|
109 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
110 |
4326
|
111 void close (void) { if (buf) buf->close (); } |
3652
|
112 |
4797
|
113 int seek (long offset, int origin) |
|
114 { return buf ? buf->seek (offset, origin) : -1; } |
|
115 |
|
116 long tell (void) { return buf ? buf->tell () : -1; } |
|
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 |
4326
|
143 private: |
|
144 |
|
145 c_file_ptr_buf *buf; |
|
146 }; |
|
147 |
|
148 class |
|
149 io_c_file_ptr_stream : public std::iostream |
|
150 { |
|
151 public: |
|
152 |
|
153 io_c_file_ptr_stream (FILE* f, |
|
154 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
|
155 : std::iostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
|
156 |
|
157 ~io_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
158 |
|
159 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
160 |
|
161 void close (void) { if (buf) buf->close (); } |
3652
|
162 |
4797
|
163 int seek (long offset, int origin) |
|
164 { return buf ? buf->seek (offset, origin) : -1; } |
|
165 |
|
166 long tell (void) { return buf ? buf->tell () : -1; } |
|
167 |
3628
|
168 private: |
|
169 |
3644
|
170 c_file_ptr_buf *buf; |
3628
|
171 }; |
|
172 |
|
173 #endif |
|
174 |
|
175 /* |
|
176 ;;; Local Variables: *** |
|
177 ;;; mode: C++ *** |
|
178 ;;; End: *** |
|
179 */ |
|
180 |