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 (__GNUG__) |
|
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 |
|
52 int |
|
53 c_file_ptr_buf::overflow (int c) |
|
54 { |
3644
|
55 if (f) |
3652
|
56 return (c != EOF) ? fputc (c, f) : flush (); |
3644
|
57 else |
|
58 return EOF; |
3628
|
59 } |
|
60 |
|
61 int |
|
62 c_file_ptr_buf::underflow (void) |
|
63 { |
3644
|
64 if (f) |
|
65 return fgetc (f); |
|
66 else |
|
67 return EOF; |
3628
|
68 } |
|
69 |
|
70 int |
|
71 c_file_ptr_buf::uflow (void) |
|
72 { |
|
73 return underflow (); |
|
74 } |
|
75 |
|
76 int |
|
77 c_file_ptr_buf::pbackfail (int c) |
|
78 { |
3644
|
79 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3628
|
80 } |
|
81 |
|
82 std::streamsize |
|
83 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
84 { |
3644
|
85 if (f) |
|
86 return fwrite (s, 1, n, f); |
|
87 else |
|
88 return 0; |
3628
|
89 } |
|
90 |
|
91 std::streamsize |
|
92 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
93 { |
3644
|
94 if (f) |
|
95 return fread (s, 1, n, f); |
|
96 else |
|
97 return 0; |
3628
|
98 } |
|
99 |
|
100 static inline int |
|
101 seekdir_to_whence (std::ios::seekdir dir) |
|
102 { |
|
103 return ((dir == std::ios::beg) ? SEEK_SET : |
|
104 (dir == std::ios::cur) ? SEEK_CUR : |
|
105 (dir == std::ios::end) ? SEEK_END : |
|
106 dir); |
|
107 } |
|
108 |
|
109 std::streampos |
|
110 c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
111 std::ios::openmode) |
|
112 { |
|
113 // XXX FIXME XXX -- is this the right thing to do? |
|
114 |
3644
|
115 if (f) |
|
116 { |
|
117 fseek (f, offset, seekdir_to_whence (dir)); |
3628
|
118 |
3644
|
119 return ftell (f); |
|
120 } |
|
121 else |
|
122 return 0; |
3628
|
123 } |
|
124 |
|
125 std::streampos |
|
126 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) |
|
127 { |
|
128 // XXX FIXME XXX -- is this the right thing to do? |
|
129 |
3644
|
130 if (f) |
|
131 { |
|
132 fseek (f, offset, SEEK_SET); |
3628
|
133 |
3644
|
134 return ftell (f); |
|
135 } |
|
136 else |
|
137 return 0; |
3628
|
138 } |
|
139 |
|
140 int |
|
141 c_file_ptr_buf::sync (void) |
|
142 { |
3652
|
143 flush (); |
|
144 |
3628
|
145 return 0; |
|
146 } |
|
147 |
3652
|
148 int |
|
149 c_file_ptr_buf::flush (void) |
|
150 { |
|
151 return f ? fflush (f) : EOF; |
|
152 } |
|
153 |
|
154 int |
|
155 c_file_ptr_buf::close (void) |
|
156 { |
3693
|
157 int retval = -1; |
|
158 |
3716
|
159 flush (); |
|
160 |
3652
|
161 if (f) |
3693
|
162 { |
3716
|
163 retval = cf (f); |
3693
|
164 f = 0; |
|
165 } |
|
166 |
|
167 return retval; |
3652
|
168 } |
|
169 |
|
170 void |
|
171 i_c_file_ptr_stream::close (void) |
|
172 { |
|
173 if (buf) |
|
174 buf->close (); |
|
175 } |
|
176 |
|
177 void |
|
178 o_c_file_ptr_stream::close (void) |
|
179 { |
|
180 if (buf) |
|
181 buf->close (); |
|
182 } |
|
183 |
3628
|
184 /* |
|
185 ;;; Local Variables: *** |
|
186 ;;; mode: C++ *** |
|
187 ;;; End: *** |
|
188 */ |
|
189 |