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