os/ossrv/genericopenlibs/cppstdlib/stl/src/stdio_streambuf.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/src/stdio_streambuf.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,296 @@
     1.4 +/*
     1.5 + * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.6 + *
     1.7 + * Copyright (c) 1999
     1.8 + * Silicon Graphics Computer Systems, Inc.
     1.9 + *
    1.10 + * Copyright (c) 1999
    1.11 + * Boris Fomitchev
    1.12 + *
    1.13 + * This material is provided "as is", with absolutely no warranty expressed
    1.14 + * or implied. Any use is at your own risk.
    1.15 + *
    1.16 + * Permission to use or copy this software for any purpose is hereby granted
    1.17 + * without fee, provided the above notices are retained on all copies.
    1.18 + * Permission to modify the code and to distribute modified code is granted,
    1.19 + * provided the above notices are retained, and a notice that the code was
    1.20 + * modified is included with the above copyright notice.
    1.21 + *
    1.22 + */
    1.23 +
    1.24 +#include "stlport_prefix.h"
    1.25 +#include "stdio_streambuf.h"
    1.26 +// #include "file_streambuf.h"
    1.27 +
    1.28 +#ifdef _STLP_UNIX
    1.29 +#  include <sys/types.h>
    1.30 +#  include <sys/stat.h>
    1.31 +#endif /* __unix */
    1.32 +
    1.33 +#include <fstream>
    1.34 +#include <limits>
    1.35 +#include "fstream_impl.h"
    1.36 +
    1.37 +#if defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE)
    1.38 +#  if defined (__BORLANDC__)
    1.39 +// #  include <cio.h>
    1.40 +#    include <cfcntl.h>
    1.41 +#  else
    1.42 +#    include <io.h>
    1.43 +#    include <fcntl.h>
    1.44 +#  endif
    1.45 +#  include <sys/stat.h>
    1.46 +#endif
    1.47 +
    1.48 +_STLP_BEGIN_NAMESPACE
    1.49 +_STLP_MOVE_TO_PRIV_NAMESPACE
    1.50 +
    1.51 +// Compare with streamoff definition in stl/char_traits.h!
    1.52 +
    1.53 +#ifdef _STLP_USE_DEFAULT_FILE_OFFSET
    1.54 +#  define FSEEK fseek
    1.55 +#  define FTELL ftell
    1.56 +#  define FSTAT fstat
    1.57 +#  define STAT  stat
    1.58 +#  define FSETPOS  fsetpos
    1.59 +#  define FGETPOS  fgetpos
    1.60 +#  define FPOS_T   fpos_t
    1.61 +#elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
    1.62 +     /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */
    1.63 +#  define FSEEK fseeko64
    1.64 +#  define FTELL ftello64
    1.65 +#  define FSTAT fstat64
    1.66 +#  define STAT  stat64
    1.67 +#  define FSETPOS  fsetpos64
    1.68 +#  define FGETPOS  fgetpos64
    1.69 +#  define FPOS_T   fpos64_t
    1.70 +#else
    1.71 +#  define FSEEK fseek
    1.72 +#  define FTELL ftell
    1.73 +#  define FSTAT fstat
    1.74 +#  define STAT  stat
    1.75 +#  define FSETPOS  fsetpos
    1.76 +#  define FGETPOS  fgetpos
    1.77 +#  define FPOS_T   fpos_t
    1.78 +#endif
    1.79 +
    1.80 +//----------------------------------------------------------------------
    1.81 +// Class stdio_streambuf_base
    1.82 +
    1.83 +stdio_streambuf_base::stdio_streambuf_base(FILE* file)
    1.84 +    : /* _STLP_STD::FILE_basic_streambuf(file, 0), */
    1.85 +    _M_file(file)
    1.86 +{}
    1.87 +
    1.88 +stdio_streambuf_base::~stdio_streambuf_base() {
    1.89 +  _STLP_VENDOR_CSTD::fflush(_M_file);
    1.90 +}
    1.91 +
    1.92 +_STLP_STD::streambuf* stdio_streambuf_base::setbuf(char* s, streamsize n) {
    1.93 +#ifdef _STLP_WCE
    1.94 +  // no buffering in windows ce .NET
    1.95 +#else
    1.96 +  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.97 +                                                            : __STATIC_CAST(size_t, n);
    1.98 +  _STLP_VENDOR_CSTD::setvbuf(_M_file, s, (s == 0 && n == 0) ? _IONBF : _IOFBF, __n_size_t);
    1.99 +#endif
   1.100 +  return this;
   1.101 +}
   1.102 +
   1.103 +stdio_streambuf_base::pos_type
   1.104 +stdio_streambuf_base::seekoff(off_type off, ios_base::seekdir dir,
   1.105 +                              ios_base::openmode /* mode */) {
   1.106 +  int whence;
   1.107 +  switch (dir) {
   1.108 +  case ios_base::beg:
   1.109 +    whence = SEEK_SET;
   1.110 +    break;
   1.111 +  case ios_base::cur:
   1.112 +    whence = SEEK_CUR;
   1.113 +    break;
   1.114 +  case ios_base::end:
   1.115 +    whence = SEEK_END;
   1.116 +    break;
   1.117 +  default:
   1.118 +    return pos_type(-1);
   1.119 +  }
   1.120 +
   1.121 +  if (off <= numeric_limits<off_type>::max() && FSEEK(_M_file, off, whence) == 0) {
   1.122 +    FPOS_T pos;
   1.123 +    FGETPOS(_M_file, &pos);
   1.124 +    // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
   1.125 +    // of a primitive type
   1.126 +#if (defined (__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2))))
   1.127 +    return pos_type((streamoff)pos.__pos);
   1.128 +#elif defined (__ISCPP__) || defined (__MVS__) || defined (__OS400__)
   1.129 +    return pos_type(pos.__fpos_elem[ 0 ]);
   1.130 +#elif defined (__EMX__)
   1.131 +    return pos_type((streamoff)pos._pos);
   1.132 +#else
   1.133 +    return pos_type(pos);
   1.134 +#endif
   1.135 +  }
   1.136 +  else
   1.137 +    return pos_type(-1);
   1.138 +}
   1.139 +
   1.140 +
   1.141 +stdio_streambuf_base::pos_type
   1.142 +stdio_streambuf_base::seekpos(pos_type pos, ios_base::openmode /* mode */) {
   1.143 +  // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
   1.144 +  // of a primitive type
   1.145 +#if (defined(__GLIBC__) && ( (__GLIBC__ > 2) || ( (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2) ) ) )
   1.146 +  FPOS_T p;
   1.147 +  p.__pos = pos;
   1.148 +#  ifdef _STLP_USE_UCLIBC
   1.149 +#    ifdef __STDIO_MBSTATE
   1.150 +  memset( &(p.__mbstate), 0, sizeof(p.__mbstate) );
   1.151 +#    endif
   1.152 +#    ifdef __STDIO_WIDE
   1.153 +  p.mblen_pending = 0;
   1.154 +#    endif
   1.155 +#  else
   1.156 +  memset( &(p.__state), 0, sizeof(p.__state) );
   1.157 +#  endif
   1.158 +#elif defined (__MVS__) || defined (__OS400__)
   1.159 +  FPOS_T p;
   1.160 +  p.__fpos_elem[0] = pos;
   1.161 +#elif defined(__EMX__)
   1.162 +  FPOS_T p;
   1.163 +  p._pos = pos;
   1.164 +  memset( &(p._mbstate), 0, sizeof(p._mbstate) );
   1.165 +#else
   1.166 +  FPOS_T p(pos);
   1.167 +#endif
   1.168 +
   1.169 +  return FSETPOS(_M_file, &p) == 0 ? pos : pos_type(-1);
   1.170 +}
   1.171 +
   1.172 +int stdio_streambuf_base::sync() {
   1.173 +  return _STLP_VENDOR_CSTD::fflush(_M_file) == 0 ? 0 : -1;
   1.174 +}
   1.175 +
   1.176 +//----------------------------------------------------------------------
   1.177 +// Class stdio_istreambuf
   1.178 +
   1.179 +stdio_istreambuf::~stdio_istreambuf() {}
   1.180 +
   1.181 +streamsize stdio_istreambuf::showmanyc() {
   1.182 +  if (feof(_M_file))
   1.183 +    return -1;
   1.184 +  else {
   1.185 +#ifdef __SYMBIAN32__
   1.186 +	  int fd = fileno(_M_file);
   1.187 +#else
   1.188 +    int fd = _FILE_fd(_M_file);
   1.189 +#endif
   1.190 +#ifdef _STLP_WCE
   1.191 +   (fd); // prevent warning about unused variable
   1.192 +// not sure if i can mix win32 io mode with ftell but time will show
   1.193 +// cannot use WIN32_IO implementation since missing stat
   1.194 +    streamsize tmp = FTELL(_M_file);
   1.195 +    FSEEK(_M_file, 0, SEEK_END);
   1.196 +    streamoff size= FTELL(_M_file)-tmp;
   1.197 +    FSEEK(_M_file, tmp, SEEK_SET);
   1.198 +#elif defined (_STLP_USE_WIN32_IO)
   1.199 +    // in this case, __file_size works with Win32 fh , not libc one
   1.200 +    streamoff size;
   1.201 +    struct stat buf;
   1.202 +    if(FSTAT(fd, &buf) == 0 && ( _S_IFREG & buf.st_mode ) )
   1.203 +      size = ( buf.st_size > 0  ? buf.st_size : 0);
   1.204 +    else
   1.205 +      size = 0;
   1.206 +#else
   1.207 +    streamoff size = __file_size(fd);
   1.208 +#endif
   1.209 +    // fbp : we can use ftell as this flavour always use stdio.
   1.210 +    streamsize pos = FTELL(_M_file);
   1.211 +    return pos >= 0 && size > pos ? size - pos : 0;
   1.212 +  }
   1.213 +}
   1.214 +
   1.215 +stdio_istreambuf::int_type stdio_istreambuf::underflow()
   1.216 +{
   1.217 +#ifdef _STLP_WCE
   1.218 +  int c = fgetc(_M_file);
   1.219 +#else
   1.220 +  int c = getc(_M_file);
   1.221 +#endif
   1.222 +  if (c != EOF) {
   1.223 +    _STLP_VENDOR_CSTD::ungetc(c, _M_file);
   1.224 +    return c;
   1.225 +  }
   1.226 +  else
   1.227 +    return traits_type::eof();
   1.228 +}
   1.229 +
   1.230 +stdio_istreambuf::int_type stdio_istreambuf::uflow() {
   1.231 +#ifdef _STLP_WCE
   1.232 +  int c = fgetc(_M_file);
   1.233 +#else
   1.234 +  int c = getc(_M_file);
   1.235 +#endif
   1.236 +  return c != EOF ? c : traits_type::eof();
   1.237 +}
   1.238 +
   1.239 +stdio_istreambuf::int_type stdio_istreambuf::pbackfail(int_type c) {
   1.240 +  if (c != traits_type::eof()) {
   1.241 +    int result = _STLP_VENDOR_CSTD::ungetc(c, _M_file);
   1.242 +    return result != EOF ? result : traits_type::eof();
   1.243 +  }
   1.244 +  else{
   1.245 +    if (this->eback() < this->gptr()) {
   1.246 +      this->gbump(-1);
   1.247 +      return traits_type::not_eof(c);
   1.248 +    }
   1.249 +    else
   1.250 +      return traits_type::eof();
   1.251 +  }
   1.252 +}
   1.253 +
   1.254 +//----------------------------------------------------------------------
   1.255 +// Class stdio_ostreambuf
   1.256 +
   1.257 +stdio_ostreambuf::~stdio_ostreambuf() {}
   1.258 +
   1.259 +streamsize stdio_ostreambuf::showmanyc() {
   1.260 +  return -1;
   1.261 +}
   1.262 +
   1.263 +stdio_ostreambuf::int_type stdio_ostreambuf::overflow(int_type c) {
   1.264 +  // Write the existing buffer, without writing any additional character.
   1.265 +  if (c == traits_type::eof()) {
   1.266 +    // Do we have a buffer to write?
   1.267 +    ptrdiff_t unwritten = this->pptr() - this->pbase();
   1.268 +    if (unwritten != 0) {
   1.269 +      _STLP_VENDOR_CSTD::fflush(_M_file);
   1.270 +      // Test if the write succeeded.
   1.271 +      if (this->pptr() - this->pbase() < unwritten)
   1.272 +        return traits_type::not_eof(c);
   1.273 +      else
   1.274 +        return traits_type::eof();
   1.275 +    }
   1.276 +
   1.277 +    // We always succeed if we don't have to do anything.
   1.278 +    else
   1.279 +      return traits_type::not_eof(c);
   1.280 +  }
   1.281 +
   1.282 +  // Write the character c, and whatever else might be in the buffer.
   1.283 +  else {
   1.284 +#ifdef _STLP_WCE
   1.285 +    int result = fputc(c, _M_file);
   1.286 +#else
   1.287 +    int result = putc(c, _M_file);
   1.288 +#endif
   1.289 +    return result != EOF ? result : traits_type::eof();
   1.290 +  }
   1.291 +}
   1.292 +
   1.293 +_STLP_MOVE_TO_STD_NAMESPACE
   1.294 +_STLP_END_NAMESPACE
   1.295 +
   1.296 +// Local Variables:
   1.297 +// mode:C++
   1.298 +// End:
   1.299 +