ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

stdio_streambuf.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 #include "stlport_prefix.h"
00020 #include "stdio_streambuf.h"
00021 
00022 #ifdef _STLP_UNIX
00023 #  include <sys/types.h>
00024 #  include <sys/stat.h>
00025 #endif
00026 
00027 #include <fstream>
00028 #include <limits>
00029 
00030 _STLP_BEGIN_NAMESPACE
00031 _STLP_MOVE_TO_PRIV_NAMESPACE
00032 
00033 // Compare with streamoff definition in stl/char_traits.h!
00034 
00035 #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \
00036     (!defined(_LARGEFILE_SOURCE) && !defined(_LARGEFILE64_SOURCE))
00037 #  if !defined (_STLP_MSVC) || (_STLP_MSVC < 1400) || defined(_STLP_WCE)
00038 #    define FSEEK fseek
00039 #  else
00040 #    define FSEEK _fseeki64
00041 #  endif
00042 #  define FSETPOS  fsetpos
00043 #  define FGETPOS  fgetpos
00044 #  define FPOS_T   fpos_t
00045 #else
00046 #  define FSEEK fseeko64
00047 #  define FSETPOS  fsetpos64
00048 #  define FGETPOS  fgetpos64
00049 #  define FPOS_T   fpos64_t
00050 #endif
00051 
00052 //----------------------------------------------------------------------
00053 // Class stdio_streambuf_base
00054 
00055 stdio_streambuf_base::stdio_streambuf_base(FILE* file)
00056     : /* _STLP_STD::FILE_basic_streambuf(file, 0), */
00057     _M_file(file)
00058 {}
00059 
00060 stdio_streambuf_base::~stdio_streambuf_base() {
00061   _STLP_VENDOR_CSTD::fflush(_M_file);
00062 }
00063 
00064 _STLP_STD::streambuf* stdio_streambuf_base::setbuf(char* s, streamsize n) {
00065 #ifdef _STLP_WCE
00066   // no buffering in windows ce .NET
00067 #else
00068   size_t __n_size_t = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()), n))
00069                                                             : __STATIC_CAST(size_t, n);
00070   _STLP_VENDOR_CSTD::setvbuf(_M_file, s, (s == 0 && n == 0) ? _IONBF : _IOFBF, __n_size_t);
00071 #endif
00072   return this;
00073 }
00074 
00075 stdio_streambuf_base::pos_type
00076 stdio_streambuf_base::seekoff(off_type off, ios_base::seekdir dir,
00077                               ios_base::openmode /* mode */) {
00078   int whence;
00079   switch (dir) {
00080   case ios_base::beg:
00081     whence = SEEK_SET;
00082     break;
00083   case ios_base::cur:
00084     whence = SEEK_CUR;
00085     break;
00086   case ios_base::end:
00087     whence = SEEK_END;
00088     break;
00089   default:
00090     return pos_type(-1);
00091   }
00092 
00093   if (off <= numeric_limits<off_type>::max() && FSEEK(_M_file, off, whence) == 0) {
00094     FPOS_T pos;
00095     FGETPOS(_M_file, &pos);
00096     // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
00097     // of a primitive type
00098 #if (defined (__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2))))
00099     return pos_type((streamoff)pos.__pos);
00100 #elif defined (__ISCPP__) || defined (__MVS__) || defined (__OS400__)
00101     return pos_type(pos.__fpos_elem[ 0 ]);
00102 #elif defined (__EMX__)
00103     return pos_type((streamoff)pos._pos);
00104 #else
00105     return pos_type(pos);
00106 #endif
00107   }
00108   else
00109     return pos_type(-1);
00110 }
00111 
00112 
00113 stdio_streambuf_base::pos_type
00114 stdio_streambuf_base::seekpos(pos_type pos, ios_base::openmode /* mode */) {
00115   // added 21 june 00 mdb,rjf,wjs: glibc 2.2 changed fpos_t to be a struct instead
00116   // of a primitive type
00117 #if (defined(__GLIBC__) && ( (__GLIBC__ > 2) || ( (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2) ) ) )
00118   FPOS_T p;
00119   p.__pos = pos;
00120 #  ifdef _STLP_USE_UCLIBC
00121 #    ifdef __STDIO_MBSTATE
00122   memset( &(p.__mbstate), 0, sizeof(p.__mbstate) );
00123 #    endif
00124 #    ifdef __STDIO_WIDE
00125   p.mblen_pending = 0;
00126 #    endif
00127 #  else
00128   memset( &(p.__state), 0, sizeof(p.__state) );
00129 #  endif
00130 #elif defined (__MVS__) || defined (__OS400__)
00131   FPOS_T p;
00132   p.__fpos_elem[0] = pos;
00133 #elif defined (__EMX__)
00134   FPOS_T p;
00135   p._pos = pos;
00136   memset( &(p._mbstate), 0, sizeof(p._mbstate) );
00137 #else
00138   FPOS_T p(pos);
00139 #endif
00140 
00141   return FSETPOS(_M_file, &p) == 0 ? pos : pos_type(-1);
00142 }
00143 
00144 int stdio_streambuf_base::sync() {
00145   return _STLP_VENDOR_CSTD::fflush(_M_file) == 0 ? 0 : -1;
00146 }
00147 
00148 //----------------------------------------------------------------------
00149 // Class stdio_istreambuf
00150 
00151 stdio_istreambuf::~stdio_istreambuf() {}
00152 
00153 streamsize stdio_istreambuf::showmanyc()
00154 { return 0; }
00155 
00156 stdio_istreambuf::int_type stdio_istreambuf::underflow()
00157 {
00158 #ifdef _STLP_WCE
00159   int c = fgetc(_M_file);
00160 #else
00161   int c = getc(_M_file);
00162 #endif
00163   if (c != EOF) {
00164     _STLP_VENDOR_CSTD::ungetc(c, _M_file);
00165     return c;
00166   }
00167   else
00168     return traits_type::eof();
00169 }
00170 
00171 stdio_istreambuf::int_type stdio_istreambuf::uflow() {
00172 #ifdef _STLP_WCE
00173   int c = fgetc(_M_file);
00174 #else
00175   int c = getc(_M_file);
00176 #endif
00177   return c != EOF ? c : traits_type::eof();
00178 }
00179 
00180 stdio_istreambuf::int_type stdio_istreambuf::pbackfail(int_type c) {
00181   if (c != traits_type::eof()) {
00182     int result = _STLP_VENDOR_CSTD::ungetc(c, _M_file);
00183     return result != EOF ? result : traits_type::eof();
00184   }
00185   else{
00186     if (this->eback() < this->gptr()) {
00187       this->gbump(-1);
00188       return traits_type::not_eof(c);
00189     }
00190     else
00191       return traits_type::eof();
00192   }
00193 }
00194 
00195 //----------------------------------------------------------------------
00196 // Class stdio_ostreambuf
00197 
00198 stdio_ostreambuf::~stdio_ostreambuf() {}
00199 
00200 streamsize stdio_ostreambuf::showmanyc()
00201 { return -1; }
00202 
00203 stdio_ostreambuf::int_type stdio_ostreambuf::overflow(int_type c) {
00204   // Write the existing buffer, without writing any additional character.
00205   if (c == traits_type::eof()) {
00206     // Do we have a buffer to write?
00207     ptrdiff_t unwritten = this->pptr() - this->pbase();
00208     if (unwritten != 0) {
00209       _STLP_VENDOR_CSTD::fflush(_M_file);
00210       // Test if the write succeeded.
00211       if (this->pptr() - this->pbase() < unwritten)
00212         return traits_type::not_eof(c);
00213       else
00214         return traits_type::eof();
00215     }
00216 
00217     // We always succeed if we don't have to do anything.
00218     else
00219       return traits_type::not_eof(c);
00220   }
00221 
00222   // Write the character c, and whatever else might be in the buffer.
00223   else {
00224 #ifdef _STLP_WCE
00225     int result = fputc(c, _M_file);
00226 #else
00227     int result = putc(c, _M_file);
00228 #endif
00229     return result != EOF ? result : traits_type::eof();
00230   }
00231 }
00232 
00233 _STLP_MOVE_TO_STD_NAMESPACE
00234 _STLP_END_NAMESPACE
00235 
00236 // Local Variables:
00237 // mode:C++
00238 // End:
00239 

Generated on Sat May 26 2012 04:34:04 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.