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 { |
|
47 if (f) |
|
48 fflush (f); |
|
49 } |
3628
|
50 |
|
51 // XXX FIXME XXX -- I'm sure there is room for improvement here... |
|
52 |
|
53 int |
|
54 c_file_ptr_buf::overflow (int c) |
|
55 { |
3644
|
56 if (f) |
|
57 return (c != EOF) ? fputc (c, f) : fflush (f); |
|
58 else |
|
59 return EOF; |
3628
|
60 } |
|
61 |
|
62 int |
|
63 c_file_ptr_buf::underflow (void) |
|
64 { |
3644
|
65 if (f) |
|
66 return fgetc (f); |
|
67 else |
|
68 return EOF; |
3628
|
69 } |
|
70 |
|
71 int |
|
72 c_file_ptr_buf::uflow (void) |
|
73 { |
|
74 return underflow (); |
|
75 } |
|
76 |
|
77 int |
|
78 c_file_ptr_buf::pbackfail (int c) |
|
79 { |
3644
|
80 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3628
|
81 } |
|
82 |
|
83 std::streamsize |
|
84 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
85 { |
3644
|
86 if (f) |
|
87 return fwrite (s, 1, n, f); |
|
88 else |
|
89 return 0; |
3628
|
90 } |
|
91 |
|
92 std::streamsize |
|
93 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
94 { |
3644
|
95 if (f) |
|
96 return fread (s, 1, n, f); |
|
97 else |
|
98 return 0; |
3628
|
99 } |
|
100 |
|
101 static inline int |
|
102 seekdir_to_whence (std::ios::seekdir dir) |
|
103 { |
|
104 return ((dir == std::ios::beg) ? SEEK_SET : |
|
105 (dir == std::ios::cur) ? SEEK_CUR : |
|
106 (dir == std::ios::end) ? SEEK_END : |
|
107 dir); |
|
108 } |
|
109 |
|
110 std::streampos |
|
111 c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
112 std::ios::openmode) |
|
113 { |
|
114 // XXX FIXME XXX -- is this the right thing to do? |
|
115 |
3644
|
116 if (f) |
|
117 { |
|
118 fseek (f, offset, seekdir_to_whence (dir)); |
3628
|
119 |
3644
|
120 return ftell (f); |
|
121 } |
|
122 else |
|
123 return 0; |
3628
|
124 } |
|
125 |
|
126 std::streampos |
|
127 c_file_ptr_buf::seekpos (std::streampos offset, 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, SEEK_SET); |
3628
|
134 |
3644
|
135 return ftell (f); |
|
136 } |
|
137 else |
|
138 return 0; |
3628
|
139 } |
|
140 |
|
141 int |
|
142 c_file_ptr_buf::sync (void) |
|
143 { |
|
144 return 0; |
|
145 } |
|
146 |
|
147 /* |
|
148 ;;; Local Variables: *** |
|
149 ;;; mode: C++ *** |
|
150 ;;; End: *** |
|
151 */ |
|
152 |