1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/stlport/src/stdio_streambuf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,239 @@ 1.4 +/* 1.5 + * Copyright (c) 1999 1.6 + * Silicon Graphics Computer Systems, Inc. 1.7 + * 1.8 + * Copyright (c) 1999 1.9 + * Boris Fomitchev 1.10 + * 1.11 + * This material is provided "as is", with absolutely no warranty expressed 1.12 + * or implied. Any use is at your own risk. 1.13 + * 1.14 + * Permission to use or copy this software for any purpose is hereby granted 1.15 + * without fee, provided the above notices are retained on all copies. 1.16 + * Permission to modify the code and to distribute modified code is granted, 1.17 + * provided the above notices are retained, and a notice that the code was 1.18 + * modified is included with the above copyright notice. 1.19 + * 1.20 + */ 1.21 + 1.22 +#include "stlport_prefix.h" 1.23 +#include "stdio_streambuf.h" 1.24 + 1.25 +#ifdef _STLP_UNIX 1.26 +# include <sys/types.h> 1.27 +# include <sys/stat.h> 1.28 +#endif 1.29 + 1.30 +#include <fstream> 1.31 +#include <limits> 1.32 + 1.33 +_STLP_BEGIN_NAMESPACE 1.34 +_STLP_MOVE_TO_PRIV_NAMESPACE 1.35 + 1.36 +// Compare with streamoff definition in stl/char_traits.h! 1.37 + 1.38 +#if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \ 1.39 + (!defined(_LARGEFILE_SOURCE) && !defined(_LARGEFILE64_SOURCE)) 1.40 +# if !defined (_STLP_MSVC) || (_STLP_MSVC < 1400) || defined(_STLP_WCE) 1.41 +# define FSEEK fseek 1.42 +# else 1.43 +# define FSEEK _fseeki64 1.44 +# endif 1.45 +# define FSETPOS fsetpos 1.46 +# define FGETPOS fgetpos 1.47 +# define FPOS_T fpos_t 1.48 +#else 1.49 +# define FSEEK fseeko64 1.50 +# define FSETPOS fsetpos64 1.51 +# define FGETPOS fgetpos64 1.52 +# define FPOS_T fpos64_t 1.53 +#endif 1.54 + 1.55 +//---------------------------------------------------------------------- 1.56 +// Class stdio_streambuf_base 1.57 + 1.58 +stdio_streambuf_base::stdio_streambuf_base(FILE* file) 1.59 + : /* _STLP_STD::FILE_basic_streambuf(file, 0), */ 1.60 + _M_file(file) 1.61 +{} 1.62 + 1.63 +stdio_streambuf_base::~stdio_streambuf_base() { 1.64 + _STLP_VENDOR_CSTD::fflush(_M_file); 1.65 +} 1.66 + 1.67 +_STLP_STD::streambuf* stdio_streambuf_base::setbuf(char* s, streamsize n) { 1.68 +#ifdef _STLP_WCE 1.69 + // no buffering in windows ce .NET 1.70 +#else 1.71 + size_t __n_size_t = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()), n)) 1.72 + : __STATIC_CAST(size_t, n); 1.73 + _STLP_VENDOR_CSTD::setvbuf(_M_file, s, (s == 0 && n == 0) ? _IONBF : _IOFBF, __n_size_t); 1.74 +#endif 1.75 + return this; 1.76 +} 1.77 + 1.78 +stdio_streambuf_base::pos_type 1.79 +stdio_streambuf_base::seekoff(off_type off, ios_base::seekdir dir, 1.80 + ios_base::openmode /* mode */) { 1.81 + int whence; 1.82 + switch (dir) { 1.83 + case ios_base::beg: 1.84 + whence = SEEK_SET; 1.85 + break; 1.86 + case ios_base::cur: 1.87 + whence = SEEK_CUR; 1.88 + break; 1.89 + case ios_base::end: 1.90 + whence = SEEK_END; 1.91 + break; 1.92 + default: 1.93 + return pos_type(-1); 1.94 + } 1.95 + 1.96 + if (off <= numeric_limits<off_type>::max() && FSEEK(_M_file, off, whence) == 0) { 1.97 + FPOS_T pos; 1.98 + FGETPOS(_M_file, &pos); 1.99 + // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead 1.100 + // of a primitive type 1.101 +#if (defined (__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)))) 1.102 + return pos_type((streamoff)pos.__pos); 1.103 +#elif defined (__ISCPP__) || defined (__MVS__) || defined (__OS400__) 1.104 + return pos_type(pos.__fpos_elem[ 0 ]); 1.105 +#elif defined (__EMX__) 1.106 + return pos_type((streamoff)pos._pos); 1.107 +#else 1.108 + return pos_type(pos); 1.109 +#endif 1.110 + } 1.111 + else 1.112 + return pos_type(-1); 1.113 +} 1.114 + 1.115 + 1.116 +stdio_streambuf_base::pos_type 1.117 +stdio_streambuf_base::seekpos(pos_type pos, ios_base::openmode /* mode */) { 1.118 + // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead 1.119 + // of a primitive type 1.120 +#if (defined(__GLIBC__) && ( (__GLIBC__ > 2) || ( (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2) ) ) ) 1.121 + FPOS_T p; 1.122 + p.__pos = pos; 1.123 +# ifdef _STLP_USE_UCLIBC 1.124 +# ifdef __STDIO_MBSTATE 1.125 + memset( &(p.__mbstate), 0, sizeof(p.__mbstate) ); 1.126 +# endif 1.127 +# ifdef __STDIO_WIDE 1.128 + p.mblen_pending = 0; 1.129 +# endif 1.130 +# else 1.131 + memset( &(p.__state), 0, sizeof(p.__state) ); 1.132 +# endif 1.133 +#elif defined (__MVS__) || defined (__OS400__) 1.134 + FPOS_T p; 1.135 + p.__fpos_elem[0] = pos; 1.136 +#elif defined (__EMX__) 1.137 + FPOS_T p; 1.138 + p._pos = pos; 1.139 + memset( &(p._mbstate), 0, sizeof(p._mbstate) ); 1.140 +#else 1.141 + FPOS_T p(pos); 1.142 +#endif 1.143 + 1.144 + return FSETPOS(_M_file, &p) == 0 ? pos : pos_type(-1); 1.145 +} 1.146 + 1.147 +int stdio_streambuf_base::sync() { 1.148 + return _STLP_VENDOR_CSTD::fflush(_M_file) == 0 ? 0 : -1; 1.149 +} 1.150 + 1.151 +//---------------------------------------------------------------------- 1.152 +// Class stdio_istreambuf 1.153 + 1.154 +stdio_istreambuf::~stdio_istreambuf() {} 1.155 + 1.156 +streamsize stdio_istreambuf::showmanyc() 1.157 +{ return 0; } 1.158 + 1.159 +stdio_istreambuf::int_type stdio_istreambuf::underflow() 1.160 +{ 1.161 +#ifdef _STLP_WCE 1.162 + int c = fgetc(_M_file); 1.163 +#else 1.164 + int c = getc(_M_file); 1.165 +#endif 1.166 + if (c != EOF) { 1.167 + _STLP_VENDOR_CSTD::ungetc(c, _M_file); 1.168 + return c; 1.169 + } 1.170 + else 1.171 + return traits_type::eof(); 1.172 +} 1.173 + 1.174 +stdio_istreambuf::int_type stdio_istreambuf::uflow() { 1.175 +#ifdef _STLP_WCE 1.176 + int c = fgetc(_M_file); 1.177 +#else 1.178 + int c = getc(_M_file); 1.179 +#endif 1.180 + return c != EOF ? c : traits_type::eof(); 1.181 +} 1.182 + 1.183 +stdio_istreambuf::int_type stdio_istreambuf::pbackfail(int_type c) { 1.184 + if (c != traits_type::eof()) { 1.185 + int result = _STLP_VENDOR_CSTD::ungetc(c, _M_file); 1.186 + return result != EOF ? result : traits_type::eof(); 1.187 + } 1.188 + else{ 1.189 + if (this->eback() < this->gptr()) { 1.190 + this->gbump(-1); 1.191 + return traits_type::not_eof(c); 1.192 + } 1.193 + else 1.194 + return traits_type::eof(); 1.195 + } 1.196 +} 1.197 + 1.198 +//---------------------------------------------------------------------- 1.199 +// Class stdio_ostreambuf 1.200 + 1.201 +stdio_ostreambuf::~stdio_ostreambuf() {} 1.202 + 1.203 +streamsize stdio_ostreambuf::showmanyc() 1.204 +{ return -1; } 1.205 + 1.206 +stdio_ostreambuf::int_type stdio_ostreambuf::overflow(int_type c) { 1.207 + // Write the existing buffer, without writing any additional character. 1.208 + if (c == traits_type::eof()) { 1.209 + // Do we have a buffer to write? 1.210 + ptrdiff_t unwritten = this->pptr() - this->pbase(); 1.211 + if (unwritten != 0) { 1.212 + _STLP_VENDOR_CSTD::fflush(_M_file); 1.213 + // Test if the write succeeded. 1.214 + if (this->pptr() - this->pbase() < unwritten) 1.215 + return traits_type::not_eof(c); 1.216 + else 1.217 + return traits_type::eof(); 1.218 + } 1.219 + 1.220 + // We always succeed if we don't have to do anything. 1.221 + else 1.222 + return traits_type::not_eof(c); 1.223 + } 1.224 + 1.225 + // Write the character c, and whatever else might be in the buffer. 1.226 + else { 1.227 +#ifdef _STLP_WCE 1.228 + int result = fputc(c, _M_file); 1.229 +#else 1.230 + int result = putc(c, _M_file); 1.231 +#endif 1.232 + return result != EOF ? result : traits_type::eof(); 1.233 + } 1.234 +} 1.235 + 1.236 +_STLP_MOVE_TO_STD_NAMESPACE 1.237 +_STLP_END_NAMESPACE 1.238 + 1.239 +// Local Variables: 1.240 +// mode:C++ 1.241 +// End: 1.242 +