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