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 <fstream> |
|
32 #include <cstdio> |
3628
|
33 |
3944
|
34 // The c_file_ptr_buf requires a std::filebuf that accepts an open |
|
35 // file descriptor. This feature, while not part of the ISO C++ |
|
36 // standard, is supported by a variety of C++ compiler runtimes, |
|
37 // albeit in slightly different ways. |
|
38 // |
|
39 // The C++ runtime libraries shipped with GCC versions < 3.0, Sun Pro, |
|
40 // Sun Workshop/Forte 5/6, Compaq C++ all support a non-standard filebuf |
|
41 // constructor that takes an open file descriptor. The almost ISO compliant |
|
42 // GNU C++ runtime shipped with GCC 3.0.x supports a different non-standard |
|
43 // filebuf constructor that takes a FILE* instead; starting from GCC 3.1, |
|
44 // the GNU C++ runtime removes all non-standard std::filebuf constructors |
|
45 // and provides an extension template class __gnu_cxx::stdio_filebuf |
|
46 // that supports the the 3.0.x behavior. |
4073
|
47 |
|
48 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) && ! (defined (__APPLE__) && defined (__MACH__)) |
3944
|
49 # include <ext/stdio_filebuf.h> |
|
50 # define OCTAVE_STD_FILEBUF __gnu_cxx::stdio_filebuf<char> |
|
51 #else |
|
52 # define OCTAVE_STD_FILEBUF std::filebuf |
|
53 #endif |
|
54 |
3628
|
55 class |
3944
|
56 c_file_ptr_buf : public OCTAVE_STD_FILEBUF |
3628
|
57 { |
|
58 public: |
|
59 |
3775
|
60 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
61 typedef int int_type; |
3944
|
62 #else |
|
63 typedef std::filebuf::int_type int_type; |
3775
|
64 #endif |
|
65 |
3716
|
66 typedef int (*close_fcn) (FILE *); |
|
67 |
3628
|
68 FILE* stdiofile (void) const { return f; } |
|
69 |
3951
|
70 c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = fclose) |
3775
|
71 : |
3841
|
72 #if defined __GNUC__ && __GNUC__ >= 3 |
3944
|
73 OCTAVE_STD_FILEBUF (f_arg, std::ios::in | std::ios::out), |
4055
|
74 #elif defined __INTEL_COMPILER |
|
75 OCTAVE_STD_FILEBUF (f_arg), |
3775
|
76 #else |
3944
|
77 OCTAVE_STD_FILEBUF (f_arg ? fileno (f_arg) : -1), |
3775
|
78 #endif |
|
79 f (f_arg), cf (cf_arg), |
|
80 fd (f_arg ? fileno (f_arg) : -1) |
|
81 { } |
3628
|
82 |
|
83 ~c_file_ptr_buf (void); |
|
84 |
3775
|
85 int_type overflow (int_type); |
3628
|
86 |
3775
|
87 int_type underflow (void); |
3628
|
88 |
3775
|
89 int_type uflow (void); |
3628
|
90 |
3775
|
91 int_type pbackfail (int_type); |
3628
|
92 |
|
93 std::streamsize xsputn (const char*, std::streamsize); |
|
94 |
|
95 std::streamsize xsgetn (char *, std::streamsize); |
|
96 |
|
97 std::streampos seekoff (std::streamoff, std::ios::seekdir, |
|
98 std::ios::openmode = std::ios::in | std::ios::out); |
|
99 |
|
100 std::streampos seekpos (std::streampos, |
|
101 std::ios::openmode = std::ios::in | std::ios::out); |
|
102 |
|
103 int sync (void); |
3644
|
104 |
3652
|
105 int flush (void); |
|
106 |
|
107 int close (void); |
|
108 |
3775
|
109 int file_number () const { return fd; } |
|
110 |
3951
|
111 static int fclose (FILE *f) { return ::fclose (f); } |
|
112 |
3644
|
113 protected: |
|
114 |
|
115 FILE *f; |
3716
|
116 |
|
117 close_fcn cf; |
3775
|
118 |
|
119 private: |
|
120 |
|
121 int fd; |
3628
|
122 }; |
|
123 |
3944
|
124 #undef OCTAVE_STD_FILEBUF |
|
125 |
3628
|
126 class |
|
127 i_c_file_ptr_stream : public std::istream |
|
128 { |
3644
|
129 public: |
|
130 |
3951
|
131 i_c_file_ptr_stream (FILE* f, |
|
132 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
3775
|
133 : std::istream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
3644
|
134 |
|
135 ~i_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
136 |
|
137 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
138 |
3652
|
139 void close (void); |
|
140 |
3628
|
141 private: |
|
142 |
3644
|
143 c_file_ptr_buf *buf; |
3628
|
144 }; |
|
145 |
|
146 class |
|
147 o_c_file_ptr_stream : public std::ostream |
|
148 { |
3644
|
149 public: |
|
150 |
3951
|
151 o_c_file_ptr_stream (FILE* f, |
|
152 c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose) |
3775
|
153 : std::ostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); } |
3644
|
154 |
|
155 ~o_c_file_ptr_stream (void) { delete buf; buf = 0; } |
|
156 |
|
157 c_file_ptr_buf *rdbuf (void) { return buf; } |
|
158 |
3652
|
159 void close (void); |
|
160 |
3628
|
161 private: |
|
162 |
3644
|
163 c_file_ptr_buf *buf; |
3628
|
164 }; |
|
165 |
|
166 #endif |
|
167 |
|
168 /* |
|
169 ;;; Local Variables: *** |
|
170 ;;; mode: C++ *** |
|
171 ;;; End: *** |
|
172 */ |
|
173 |