home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / src / streambuf.cpp < prev    next >
C/C++ Source or Header  |  2002-01-18  |  6KB  |  243 lines

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