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 |
4192
|
23 #if defined (__GNUG__) && 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 |
4310
|
69 c_file_ptr_buf::underflow_common (bool bump) |
3628
|
70 { |
3644
|
71 if (f) |
4310
|
72 { |
|
73 int_type c = fgetc (f); |
|
74 |
|
75 if (! bump |
|
76 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
77 && c != traits_type::eof ()) |
|
78 #else |
|
79 && c != EOF) |
|
80 #endif |
|
81 ungetc (c, f); |
|
82 |
|
83 return c; |
|
84 } |
3644
|
85 else |
3775
|
86 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
87 return traits_type::eof (); |
|
88 #else |
3644
|
89 return EOF; |
3775
|
90 #endif |
3628
|
91 } |
|
92 |
3775
|
93 c_file_ptr_buf::int_type |
|
94 c_file_ptr_buf::pbackfail (int_type c) |
3628
|
95 { |
3775
|
96 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
97 return (c != traits_type::eof () && f) ? ungetc (c, f) : |
|
98 traits_type::not_eof (c); |
|
99 #else |
3644
|
100 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3775
|
101 #endif |
3628
|
102 } |
|
103 |
|
104 std::streamsize |
|
105 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
106 { |
3644
|
107 if (f) |
|
108 return fwrite (s, 1, n, f); |
|
109 else |
|
110 return 0; |
3628
|
111 } |
|
112 |
|
113 std::streamsize |
|
114 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
115 { |
3644
|
116 if (f) |
|
117 return fread (s, 1, n, f); |
|
118 else |
|
119 return 0; |
3628
|
120 } |
|
121 |
|
122 static inline int |
|
123 seekdir_to_whence (std::ios::seekdir dir) |
|
124 { |
|
125 return ((dir == std::ios::beg) ? SEEK_SET : |
|
126 (dir == std::ios::cur) ? SEEK_CUR : |
|
127 (dir == std::ios::end) ? SEEK_END : |
|
128 dir); |
|
129 } |
|
130 |
|
131 std::streampos |
|
132 c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
133 std::ios::openmode) |
|
134 { |
|
135 // XXX FIXME XXX -- is this the right thing to do? |
|
136 |
3644
|
137 if (f) |
|
138 { |
|
139 fseek (f, offset, seekdir_to_whence (dir)); |
3628
|
140 |
3644
|
141 return ftell (f); |
|
142 } |
|
143 else |
|
144 return 0; |
3628
|
145 } |
|
146 |
|
147 std::streampos |
|
148 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) |
|
149 { |
|
150 // XXX FIXME XXX -- is this the right thing to do? |
|
151 |
3644
|
152 if (f) |
|
153 { |
|
154 fseek (f, offset, SEEK_SET); |
3628
|
155 |
3644
|
156 return ftell (f); |
|
157 } |
|
158 else |
|
159 return 0; |
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 |