3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #include "stlport_prefix.h"
20 #include "stdio_streambuf.h"
21 // #include "file_streambuf.h"
24 # include <sys/types.h>
25 # include <sys/stat.h>
30 #include "fstream_impl.h"
32 #if defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE)
33 # if defined (__BORLANDC__)
40 # include <sys/stat.h>
44 _STLP_MOVE_TO_PRIV_NAMESPACE
46 // Compare with streamoff definition in stl/char_traits.h!
48 #ifdef _STLP_USE_DEFAULT_FILE_OFFSET
53 # define FSETPOS fsetpos
54 # define FGETPOS fgetpos
55 # define FPOS_T fpos_t
56 #elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
57 /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */
58 # define FSEEK fseeko64
59 # define FTELL ftello64
60 # define FSTAT fstat64
62 # define FSETPOS fsetpos64
63 # define FGETPOS fgetpos64
64 # define FPOS_T fpos64_t
70 # define FSETPOS fsetpos
71 # define FGETPOS fgetpos
72 # define FPOS_T fpos_t
75 //----------------------------------------------------------------------
76 // Class stdio_streambuf_base
78 stdio_streambuf_base::stdio_streambuf_base(FILE* file)
79 : /* _STLP_STD::FILE_basic_streambuf(file, 0), */
83 stdio_streambuf_base::~stdio_streambuf_base() {
84 _STLP_VENDOR_CSTD::fflush(_M_file);
87 _STLP_STD::streambuf* stdio_streambuf_base::setbuf(char* s, streamsize n) {
89 // no buffering in windows ce .NET
91 size_t __n_size_t = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()), n))
92 : __STATIC_CAST(size_t, n);
93 _STLP_VENDOR_CSTD::setvbuf(_M_file, s, (s == 0 && n == 0) ? _IONBF : _IOFBF, __n_size_t);
98 stdio_streambuf_base::pos_type
99 stdio_streambuf_base::seekoff(off_type off, ios_base::seekdir dir,
100 ios_base::openmode /* mode */) {
116 if (off <= numeric_limits<off_type>::max() && FSEEK(_M_file, off, whence) == 0) {
118 FGETPOS(_M_file, &pos);
119 // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
120 // of a primitive type
121 #if (defined (__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2))))
122 return pos_type((streamoff)pos.__pos);
123 #elif defined (__ISCPP__) || defined (__MVS__) || defined (__OS400__)
124 return pos_type(pos.__fpos_elem[ 0 ]);
125 #elif defined (__EMX__)
126 return pos_type((streamoff)pos._pos);
128 return pos_type(pos);
136 stdio_streambuf_base::pos_type
137 stdio_streambuf_base::seekpos(pos_type pos, ios_base::openmode /* mode */) {
138 // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
139 // of a primitive type
140 #if (defined(__GLIBC__) && ( (__GLIBC__ > 2) || ( (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2) ) ) )
143 # ifdef _STLP_USE_UCLIBC
144 # ifdef __STDIO_MBSTATE
145 memset( &(p.__mbstate), 0, sizeof(p.__mbstate) );
151 memset( &(p.__state), 0, sizeof(p.__state) );
153 #elif defined (__MVS__) || defined (__OS400__)
155 p.__fpos_elem[0] = pos;
156 #elif defined(__EMX__)
159 memset( &(p._mbstate), 0, sizeof(p._mbstate) );
164 return FSETPOS(_M_file, &p) == 0 ? pos : pos_type(-1);
167 int stdio_streambuf_base::sync() {
168 return _STLP_VENDOR_CSTD::fflush(_M_file) == 0 ? 0 : -1;
171 //----------------------------------------------------------------------
172 // Class stdio_istreambuf
174 stdio_istreambuf::~stdio_istreambuf() {}
176 streamsize stdio_istreambuf::showmanyc() {
180 int fd = _FILE_fd(_M_file);
182 (fd); // prevent warning about unused variable
183 // not sure if i can mix win32 io mode with ftell but time will show
184 // cannot use WIN32_IO implementation since missing stat
185 streamsize tmp = FTELL(_M_file);
186 FSEEK(_M_file, 0, SEEK_END);
187 streamoff size= FTELL(_M_file)-tmp;
188 FSEEK(_M_file, tmp, SEEK_SET);
189 #elif defined (_STLP_USE_WIN32_IO)
190 // in this case, __file_size works with Win32 fh , not libc one
193 if(FSTAT(fd, &buf) == 0 && ( _S_IFREG & buf.st_mode ) )
194 size = ( buf.st_size > 0 ? buf.st_size : 0);
198 streamoff size = __file_size(fd);
200 // fbp : we can use ftell as this flavour always use stdio.
201 streamsize pos = FTELL(_M_file);
202 return pos >= 0 && size > pos ? size - pos : 0;
206 stdio_istreambuf::int_type stdio_istreambuf::underflow()
209 int c = fgetc(_M_file);
211 int c = getc(_M_file);
214 _STLP_VENDOR_CSTD::ungetc(c, _M_file);
218 return traits_type::eof();
221 stdio_istreambuf::int_type stdio_istreambuf::uflow() {
223 int c = fgetc(_M_file);
225 int c = getc(_M_file);
227 return c != EOF ? c : traits_type::eof();
230 stdio_istreambuf::int_type stdio_istreambuf::pbackfail(int_type c) {
231 if (c != traits_type::eof()) {
232 int result = _STLP_VENDOR_CSTD::ungetc(c, _M_file);
233 return result != EOF ? result : traits_type::eof();
236 if (this->eback() < this->gptr()) {
238 return traits_type::not_eof(c);
241 return traits_type::eof();
245 //----------------------------------------------------------------------
246 // Class stdio_ostreambuf
248 stdio_ostreambuf::~stdio_ostreambuf() {}
250 streamsize stdio_ostreambuf::showmanyc() {
254 stdio_ostreambuf::int_type stdio_ostreambuf::overflow(int_type c) {
255 // Write the existing buffer, without writing any additional character.
256 if (c == traits_type::eof()) {
257 // Do we have a buffer to write?
258 ptrdiff_t unwritten = this->pptr() - this->pbase();
259 if (unwritten != 0) {
260 _STLP_VENDOR_CSTD::fflush(_M_file);
261 // Test if the write succeeded.
262 if (this->pptr() - this->pbase() < unwritten)
263 return traits_type::not_eof(c);
265 return traits_type::eof();
268 // We always succeed if we don't have to do anything.
270 return traits_type::not_eof(c);
273 // Write the character c, and whatever else might be in the buffer.
276 int result = fputc(c, _M_file);
278 int result = putc(c, _M_file);
280 return result != EOF ? result : traits_type::eof();
284 _STLP_MOVE_TO_STD_NAMESPACE