os/ossrv/stdcpp/src/streambuf.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     3  *
     4  * Copyright (c) 1999
     5  * Silicon Graphics Computer Systems, Inc.
     6  *
     7  * Copyright (c) 1999 
     8  * Boris Fomitchev
     9  *
    10  * This material is provided "as is", with absolutely no warranty expressed
    11  * or implied. Any use is at your own risk.
    12  *
    13  * Permission to use or copy this software for any purpose is hereby granted 
    14  * without fee, provided the above notices are retained on all copies.
    15  * Permission to modify the code and to distribute modified code is granted,
    16  * provided the above notices are retained, and a notice that the code was
    17  * modified is included with the above copyright notice.
    18  *
    19  */ 
    20 # include "stlport_prefix.h"
    21 
    22 
    23 #include <stl/_streambuf.h>
    24 #include <stl/_algobase.h>
    25 
    26 // Implementation of non-inline member functions of class
    27 // basic_streambuf<char, char_traits<char> >
    28 
    29 # if defined (__hpux)
    30 #  define FILE_CAST(x) (__REINTERPRET_CAST(FILE*, x))
    31 # else
    32 #  define FILE_CAST(x) x
    33 # endif
    34 
    35 _STLP_BEGIN_NAMESPACE
    36 
    37 #if !defined(_STLP_WINCE)
    38 
    39 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::~basic_streambuf() {}
    40 
    41 // This constructor is an extension.  It is for streambuf subclasses that
    42 // are synchronized with C stdio files.
    43 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >
    44   ::basic_streambuf(FILE* __get, FILE* __put)
    45     : _M_get(__get ? __get : FILE_CAST(&_M_default_get)),
    46       _M_put(__put ? __put : FILE_CAST(&_M_default_put)),
    47       _M_locale()
    48 {
    49   _M_lock._M_initialize();
    50 
    51   if (_M_get == FILE_CAST(&_M_default_get))
    52     _FILE_I_set(_M_get, 0, 0, 0);
    53   if (_M_put == FILE_CAST(&_M_default_put))
    54     _FILE_O_set(_M_put, 0, 0, 0);      
    55 }
    56 
    57 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >
    58         ::basic_streambuf _STLP_PSPEC2(char, char_traits<char>) ()
    59     : _M_get(__REINTERPRET_CAST(FILE*,&_M_default_get)),
    60       _M_put(__REINTERPRET_CAST(FILE*,&_M_default_put)), _M_locale()
    61   {
    62     // _M_lock._M_initialize();
    63     _FILE_I_set(_M_get, 0, 0, 0);
    64     _FILE_O_set(_M_put, 0, 0, 0);
    65   }
    66 
    67 // virtual functions
    68 
    69 _STLP_EXP_DECLSPEC void basic_streambuf<char, char_traits<char> >::imbue(const locale&)
    70 {}
    71 
    72 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >*
    73 basic_streambuf<char, char_traits<char> >::setbuf(char*, streamsize)
    74 {
    75   return this;
    76 }
    77 
    78 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::pos_type
    79 basic_streambuf<char, char_traits<char> >
    80   ::seekoff(off_type, ios_base::seekdir, ios_base::openmode)
    81 {
    82   return pos_type(-1);
    83 }
    84 
    85 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::pos_type
    86 basic_streambuf<char, char_traits<char> >
    87   ::seekpos(pos_type, ios_base::openmode)
    88 {
    89   return pos_type(-1);
    90 }
    91 
    92 _STLP_EXP_DECLSPEC int basic_streambuf<char, char_traits<char> >::sync()
    93 {
    94   return 0;
    95 }
    96 
    97 _STLP_EXP_DECLSPEC streamsize basic_streambuf<char, char_traits<char> >::showmanyc()
    98 {
    99   return 0;
   100 }
   101 
   102 _STLP_EXP_DECLSPEC streamsize basic_streambuf<char, char_traits<char> >
   103   ::xsgetn(char* s, streamsize n)
   104 {
   105   streamsize result = 0;
   106   const int_type eof = traits_type::eof();
   107 
   108   while (result < n) {
   109     if (_FILE_I_avail(_M_get) > 0) {
   110       size_t chunk = (min) (__STATIC_CAST(size_t,_FILE_I_avail(_M_get)),
   111                          __STATIC_CAST(size_t,n - result));
   112       traits_type::copy(s, _FILE_I_next(_M_get), chunk);
   113       result += chunk;
   114       s += chunk;
   115       _FILE_I_bump(_M_get, chunk);
   116     }
   117     else {
   118       int_type c = sbumpc();
   119       if (c != eof) {
   120         *s = c;
   121         ++result;
   122 	++s;
   123       }
   124       else
   125         break; 
   126     }
   127   }
   128   
   129   return result;
   130 }
   131 
   132 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::int_type
   133 basic_streambuf<char, char_traits<char> >::underflow()
   134 {
   135   return traits_type::eof();
   136 }
   137 
   138 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::int_type
   139 basic_streambuf<char, char_traits<char> >::uflow()
   140 {
   141   const int_type eof = traits_type::eof();
   142   return this->underflow() == eof 
   143     ? eof
   144     : traits_type::to_int_type(_FILE_I_postincr(_M_get));
   145 }
   146 
   147 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::int_type
   148 basic_streambuf<char, char_traits<char> >::pbackfail(int_type /* __c */)
   149 {
   150   return traits_type::eof();
   151 }
   152 
   153 
   154 _STLP_EXP_DECLSPEC streamsize basic_streambuf<char, char_traits<char> >
   155   ::xsputn(const char* s, streamsize n)
   156 {
   157   streamsize result = 0;
   158   const int_type eof = traits_type::eof();
   159 
   160   while (result < n) {
   161     if (_FILE_O_avail(_M_put) > 0) {
   162       size_t chunk = (min) (__STATIC_CAST(size_t,_FILE_O_avail(_M_put)),
   163                          __STATIC_CAST(size_t,n - result));
   164       traits_type::copy(_FILE_O_next(_M_put), s, chunk);
   165       result += chunk;
   166       s += chunk;
   167       _FILE_O_bump(_M_put, (int)chunk);
   168     }
   169 
   170     else if (this->overflow(traits_type::to_int_type(*s)) != eof) {
   171       ++result;
   172       ++s;
   173     }
   174     else
   175       break;
   176   }
   177   return result;
   178 }
   179 
   180 _STLP_EXP_DECLSPEC streamsize basic_streambuf<char, char_traits<char> >
   181   ::_M_xsputnc(char c, streamsize n)
   182 {
   183   streamsize result = 0;
   184   const int_type eof = traits_type::eof();
   185 
   186   while (result < n) {
   187     if (_FILE_O_avail(_M_put) > 0) {
   188       size_t chunk = (min) (__STATIC_CAST(size_t,_FILE_O_avail(_M_put)),
   189                          __STATIC_CAST(size_t,n - result));
   190       traits_type::assign(_FILE_O_next(_M_put), chunk, c);
   191       result += chunk;
   192       _FILE_O_bump(_M_put, (int)chunk);
   193     }
   194 
   195     else if (this->overflow(traits_type::to_int_type(c)) != eof)
   196       ++result;
   197     else
   198       break;
   199   }
   200   return result;
   201 }
   202 
   203 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::int_type
   204 basic_streambuf<char, char_traits<char> >::overflow(int_type/*  c */)
   205 {
   206   return traits_type::eof();
   207 }
   208 
   209 _STLP_EXP_DECLSPEC basic_streambuf<char, char_traits<char> >::int_type
   210 basic_streambuf<char, char_traits<char> >::_M_snextc_aux()
   211 {
   212   int_type eof = traits_type::eof();
   213   if (_FILE_I_avail(_M_get) == 0)
   214     return this->uflow() == eof ? eof : this->sgetc();
   215   else {
   216     _FILE_I_set(_M_get,
   217               _FILE_I_begin(_M_get), _FILE_I_end(_M_get), _FILE_I_end(_M_get));
   218     return this->underflow();
   219   }
   220 }
   221 
   222 
   223 _STLP_EXP_DECLSPEC locale basic_streambuf<char, char_traits<char> >::pubimbue(const locale& loc)
   224 {
   225   this->imbue(loc);
   226   locale tmp = _M_locale;
   227   _M_locale = loc;
   228   return tmp;
   229 }
   230 
   231 #else
   232 
   233 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
   234 template class basic_streambuf<char, char_traits<char> >;
   235 #endif
   236 
   237 #endif /* _STLP_WINCE */
   238 
   239 //----------------------------------------------------------------------
   240 // Force instantiation of basic_streambuf
   241 
   242 // not basic_streambuf<char>, because it's specialized.
   243 
   244 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
   245 #if !defined (_STLP_NO_WCHAR_T)
   246 template class basic_streambuf<wchar_t, char_traits<wchar_t> >;
   247 #endif /* INSTANTIATE_WIDE_STREAMS */
   248 #endif
   249 
   250 _STLP_END_NAMESPACE
   251 
   252 // Local Variables:
   253 // mode:C++
   254 // End: