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"
23 # include <sys/types.h>
24 # include <sys/stat.h>
31 _STLP_MOVE_TO_PRIV_NAMESPACE
33 // Compare with streamoff definition in stl/char_traits.h!
35 #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \
36 (!defined(_LARGEFILE_SOURCE) && !defined(_LARGEFILE64_SOURCE))
37 # if !defined (_STLP_MSVC) || (_STLP_MSVC < 1400) || defined(_STLP_WCE)
40 # define FSEEK _fseeki64
42 # define FSETPOS fsetpos
43 # define FGETPOS fgetpos
44 # define FPOS_T fpos_t
46 # define FSEEK fseeko64
47 # define FSETPOS fsetpos64
48 # define FGETPOS fgetpos64
49 # define FPOS_T fpos64_t
52 //----------------------------------------------------------------------
53 // Class stdio_streambuf_base
55 stdio_streambuf_base::stdio_streambuf_base(FILE* file
)
56 : /* _STLP_STD::FILE_basic_streambuf(file, 0), */
60 stdio_streambuf_base::~stdio_streambuf_base() {
61 _STLP_VENDOR_CSTD::fflush(_M_file
);
64 _STLP_STD::streambuf
* stdio_streambuf_base::setbuf(char* s
, streamsize n
) {
66 // no buffering in windows ce .NET
68 size_t __n_size_t
= (sizeof(streamsize
) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min
)(__STATIC_CAST(streamsize
, (numeric_limits
<size_t>::max
)()), n
))
69 : __STATIC_CAST(size_t, n
);
70 _STLP_VENDOR_CSTD::setvbuf(_M_file
, s
, (s
== 0 && n
== 0) ? _IONBF
: _IOFBF
, __n_size_t
);
75 stdio_streambuf_base::pos_type
76 stdio_streambuf_base::seekoff(off_type off
, ios_base::seekdir dir
,
77 ios_base::openmode
/* mode */) {
93 if (off
<= numeric_limits
<off_type
>::max() && FSEEK(_M_file
, off
, whence
) == 0) {
95 FGETPOS(_M_file
, &pos
);
96 // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
97 // of a primitive type
98 #if (defined (__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2))))
99 return pos_type((streamoff
)pos
.__pos
);
100 #elif defined (__ISCPP__) || defined (__MVS__) || defined (__OS400__)
101 return pos_type(pos
.__fpos_elem
[ 0 ]);
102 #elif defined (__EMX__)
103 return pos_type((streamoff
)pos
._pos
);
105 return pos_type(pos
);
113 stdio_streambuf_base::pos_type
114 stdio_streambuf_base::seekpos(pos_type pos
, ios_base::openmode
/* mode */) {
115 // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
116 // of a primitive type
117 #if (defined(__GLIBC__) && ( (__GLIBC__ > 2) || ( (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2) ) ) )
120 # ifdef _STLP_USE_UCLIBC
121 # ifdef __STDIO_MBSTATE
122 memset( &(p
.__mbstate
), 0, sizeof(p
.__mbstate
) );
128 memset( &(p
.__state
), 0, sizeof(p
.__state
) );
130 #elif defined (__MVS__) || defined (__OS400__)
132 p
.__fpos_elem
[0] = pos
;
133 #elif defined (__EMX__)
136 memset( &(p
._mbstate
), 0, sizeof(p
._mbstate
) );
141 return FSETPOS(_M_file
, &p
) == 0 ? pos
: pos_type(-1);
144 int stdio_streambuf_base::sync() {
145 return _STLP_VENDOR_CSTD::fflush(_M_file
) == 0 ? 0 : -1;
148 //----------------------------------------------------------------------
149 // Class stdio_istreambuf
151 stdio_istreambuf::~stdio_istreambuf() {}
153 streamsize
stdio_istreambuf::showmanyc()
156 stdio_istreambuf::int_type
stdio_istreambuf::underflow()
159 int c
= fgetc(_M_file
);
161 int c
= getc(_M_file
);
164 _STLP_VENDOR_CSTD::ungetc(c
, _M_file
);
168 return traits_type::eof();
171 stdio_istreambuf::int_type
stdio_istreambuf::uflow() {
173 int c
= fgetc(_M_file
);
175 int c
= getc(_M_file
);
177 return c
!= EOF
? c
: traits_type::eof();
180 stdio_istreambuf::int_type
stdio_istreambuf::pbackfail(int_type c
) {
181 if (c
!= traits_type::eof()) {
182 int result
= _STLP_VENDOR_CSTD::ungetc(c
, _M_file
);
183 return result
!= EOF
? result
: traits_type::eof();
186 if (this->eback() < this->gptr()) {
188 return traits_type::not_eof(c
);
191 return traits_type::eof();
195 //----------------------------------------------------------------------
196 // Class stdio_ostreambuf
198 stdio_ostreambuf::~stdio_ostreambuf() {}
200 streamsize
stdio_ostreambuf::showmanyc()
203 stdio_ostreambuf::int_type
stdio_ostreambuf::overflow(int_type c
) {
204 // Write the existing buffer, without writing any additional character.
205 if (c
== traits_type::eof()) {
206 // Do we have a buffer to write?
207 ptrdiff_t unwritten
= this->pptr() - this->pbase();
208 if (unwritten
!= 0) {
209 _STLP_VENDOR_CSTD::fflush(_M_file
);
210 // Test if the write succeeded.
211 if (this->pptr() - this->pbase() < unwritten
)
212 return traits_type::not_eof(c
);
214 return traits_type::eof();
217 // We always succeed if we don't have to do anything.
219 return traits_type::not_eof(c
);
222 // Write the character c, and whatever else might be in the buffer.
225 int result
= fputc(c
, _M_file
);
227 int result
= putc(c
, _M_file
);
229 return result
!= EOF
? result
: traits_type::eof();
233 _STLP_MOVE_TO_STD_NAMESPACE