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