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