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 |
4061
|
23 #if defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3628
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3629
|
31 #include "c-file-ptr-stream.h" |
3628
|
32 |
|
33 #ifndef SEEK_SET |
|
34 #define SEEK_SET 0 |
|
35 #endif |
|
36 |
|
37 #ifndef SEEK_CUR |
|
38 #define SEEK_CUR 1 |
|
39 #endif |
|
40 |
|
41 #ifndef SEEK_END |
|
42 #define SEEK_END 2 |
|
43 #endif |
|
44 |
3642
|
45 c_file_ptr_buf::~c_file_ptr_buf (void) |
|
46 { |
3693
|
47 close (); |
3642
|
48 } |
3628
|
49 |
|
50 // XXX FIXME XXX -- I'm sure there is room for improvement here... |
|
51 |
3775
|
52 c_file_ptr_buf::int_type |
|
53 c_file_ptr_buf::overflow (int_type c) |
3628
|
54 { |
3775
|
55 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
56 if (f) |
|
57 return (c != traits_type::eof ()) ? fputc (c, f) : flush (); |
|
58 else |
|
59 return traits_type::not_eof (c); |
|
60 #else |
3644
|
61 if (f) |
3652
|
62 return (c != EOF) ? fputc (c, f) : flush (); |
3644
|
63 else |
|
64 return EOF; |
3775
|
65 #endif |
3628
|
66 } |
|
67 |
3775
|
68 c_file_ptr_buf::int_type |
3628
|
69 c_file_ptr_buf::underflow (void) |
|
70 { |
3644
|
71 if (f) |
|
72 return fgetc (f); |
|
73 else |
3775
|
74 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
75 return traits_type::eof (); |
|
76 #else |
3644
|
77 return EOF; |
3775
|
78 #endif |
3628
|
79 } |
|
80 |
3775
|
81 c_file_ptr_buf::int_type |
3628
|
82 c_file_ptr_buf::uflow (void) |
|
83 { |
|
84 return underflow (); |
|
85 } |
|
86 |
3775
|
87 c_file_ptr_buf::int_type |
|
88 c_file_ptr_buf::pbackfail (int_type c) |
3628
|
89 { |
3775
|
90 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
91 return (c != traits_type::eof () && f) ? ungetc (c, f) : |
|
92 traits_type::not_eof (c); |
|
93 #else |
3644
|
94 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3775
|
95 #endif |
3628
|
96 } |
|
97 |
|
98 std::streamsize |
|
99 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
100 { |
3644
|
101 if (f) |
|
102 return fwrite (s, 1, n, f); |
|
103 else |
|
104 return 0; |
3628
|
105 } |
|
106 |
|
107 std::streamsize |
|
108 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
109 { |
3644
|
110 if (f) |
|
111 return fread (s, 1, n, f); |
|
112 else |
|
113 return 0; |
3628
|
114 } |
|
115 |
|
116 static inline int |
|
117 seekdir_to_whence (std::ios::seekdir dir) |
|
118 { |
|
119 return ((dir == std::ios::beg) ? SEEK_SET : |
|
120 (dir == std::ios::cur) ? SEEK_CUR : |
|
121 (dir == std::ios::end) ? SEEK_END : |
|
122 dir); |
|
123 } |
|
124 |
|
125 std::streampos |
|
126 c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
127 std::ios::openmode) |
|
128 { |
|
129 // XXX FIXME XXX -- is this the right thing to do? |
|
130 |
3644
|
131 if (f) |
|
132 { |
|
133 fseek (f, offset, seekdir_to_whence (dir)); |
3628
|
134 |
3644
|
135 return ftell (f); |
|
136 } |
|
137 else |
|
138 return 0; |
3628
|
139 } |
|
140 |
|
141 std::streampos |
|
142 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) |
|
143 { |
|
144 // XXX FIXME XXX -- is this the right thing to do? |
|
145 |
3644
|
146 if (f) |
|
147 { |
|
148 fseek (f, offset, SEEK_SET); |
3628
|
149 |
3644
|
150 return ftell (f); |
|
151 } |
|
152 else |
|
153 return 0; |
3628
|
154 } |
|
155 |
|
156 int |
|
157 c_file_ptr_buf::sync (void) |
|
158 { |
3652
|
159 flush (); |
|
160 |
3628
|
161 return 0; |
|
162 } |
|
163 |
3652
|
164 int |
|
165 c_file_ptr_buf::flush (void) |
|
166 { |
|
167 return f ? fflush (f) : EOF; |
|
168 } |
|
169 |
|
170 int |
|
171 c_file_ptr_buf::close (void) |
|
172 { |
3693
|
173 int retval = -1; |
|
174 |
3716
|
175 flush (); |
|
176 |
3652
|
177 if (f) |
3693
|
178 { |
3716
|
179 retval = cf (f); |
3693
|
180 f = 0; |
|
181 } |
|
182 |
|
183 return retval; |
3652
|
184 } |
|
185 |
|
186 void |
|
187 i_c_file_ptr_stream::close (void) |
|
188 { |
|
189 if (buf) |
|
190 buf->close (); |
|
191 } |
|
192 |
|
193 void |
|
194 o_c_file_ptr_stream::close (void) |
|
195 { |
|
196 if (buf) |
|
197 buf->close (); |
|
198 } |
|
199 |
3628
|
200 /* |
|
201 ;;; Local Variables: *** |
|
202 ;;; mode: C++ *** |
|
203 ;;; End: *** |
|
204 */ |
|
205 |