os/ossrv/genericopenlibs/cppstdlib/stl/src/strstream.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 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
sl@0
    21
// Implementation of the classes in header <strstream>.
sl@0
    22
// WARNING: The classes defined in <strstream> are DEPRECATED.  This
sl@0
    23
// header is defined in section D.7.1 of the C++ standard, and it
sl@0
    24
// MAY BE REMOVED in a future standard revision.  You should use the
sl@0
    25
// header <sstream> instead.
sl@0
    26
sl@0
    27
#include "stlport_prefix.h"
sl@0
    28
sl@0
    29
#include <strstream>
sl@0
    30
#include <algorithm>
sl@0
    31
#include <limits>
sl@0
    32
sl@0
    33
_STLP_BEGIN_NAMESPACE
sl@0
    34
sl@0
    35
// strstreambuf constructor, destructor.
sl@0
    36
_STLP_DECLSPEC strstreambuf::strstreambuf(streamsize initial_capacity)
sl@0
    37
   : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    38
     _M_dynamic(true), _M_frozen(false), _M_constant(false) {
sl@0
    39
  size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
sl@0
    40
                                                                                 (max)(initial_capacity, streamsize(16))))
sl@0
    41
                                                   : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
sl@0
    42
sl@0
    43
  char* buf = _M_alloc(n);
sl@0
    44
  if (buf) {
sl@0
    45
    setp(buf, buf + n);
sl@0
    46
    setg(buf, buf, buf);
sl@0
    47
  }
sl@0
    48
}
sl@0
    49
sl@0
    50
_STLP_DECLSPEC strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
sl@0
    51
  : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
sl@0
    52
    _M_dynamic(true), _M_frozen(false), _M_constant(false) {
sl@0
    53
  size_t n = 16;
sl@0
    54
sl@0
    55
  char* buf = _M_alloc(n);
sl@0
    56
  if (buf) {
sl@0
    57
    setp(buf, buf + n);
sl@0
    58
    setg(buf, buf, buf);
sl@0
    59
  }
sl@0
    60
}
sl@0
    61
sl@0
    62
_STLP_DECLSPEC strstreambuf::strstreambuf(char* get, streamsize n, char* put)
sl@0
    63
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    64
    _M_dynamic(false), _M_frozen(false), _M_constant(false) {
sl@0
    65
  _M_setup(get, put, n);
sl@0
    66
}
sl@0
    67
sl@0
    68
_STLP_DECLSPEC strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
sl@0
    69
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    70
    _M_dynamic(false), _M_frozen(false), _M_constant(false) {
sl@0
    71
  _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
sl@0
    72
}
sl@0
    73
sl@0
    74
_STLP_DECLSPEC strstreambuf::strstreambuf(unsigned char* get, streamsize n,
sl@0
    75
                           unsigned char* put)
sl@0
    76
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    77
    _M_dynamic(false), _M_frozen(false), _M_constant(false) {
sl@0
    78
  _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
sl@0
    79
}
sl@0
    80
sl@0
    81
_STLP_DECLSPEC strstreambuf::strstreambuf(const char* get, streamsize n)
sl@0
    82
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    83
    _M_dynamic(false), _M_frozen(false), _M_constant(true) {
sl@0
    84
  _M_setup(__CONST_CAST(char*,get), 0, n);
sl@0
    85
}
sl@0
    86
sl@0
    87
_STLP_DECLSPEC strstreambuf::strstreambuf(const signed char* get, streamsize n)
sl@0
    88
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    89
    _M_dynamic(false), _M_frozen(false), _M_constant(true) {
sl@0
    90
  _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
sl@0
    91
}
sl@0
    92
sl@0
    93
_STLP_DECLSPEC strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
sl@0
    94
  : _M_alloc_fun(0), _M_free_fun(0),
sl@0
    95
    _M_dynamic(false), _M_frozen(false), _M_constant(true) {
sl@0
    96
  _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
sl@0
    97
}
sl@0
    98
sl@0
    99
_STLP_DECLSPEC strstreambuf::~strstreambuf() {
sl@0
   100
  if (_M_dynamic && !_M_frozen)
sl@0
   101
    _M_free(eback());
sl@0
   102
}
sl@0
   103
sl@0
   104
_STLP_DECLSPEC void strstreambuf::freeze(bool frozenflag) {
sl@0
   105
  if (_M_dynamic)
sl@0
   106
    _M_frozen = frozenflag;
sl@0
   107
}
sl@0
   108
sl@0
   109
_STLP_DECLSPEC char* strstreambuf::str() {
sl@0
   110
  freeze(true);
sl@0
   111
  return eback();
sl@0
   112
}
sl@0
   113
sl@0
   114
_STLP_DECLSPEC int strstreambuf::pcount() const {
sl@0
   115
  return int(pptr() ? pptr() - pbase() : 0);
sl@0
   116
}
sl@0
   117
sl@0
   118
_STLP_DECLSPEC strstreambuf::int_type strstreambuf::overflow(int_type c) {
sl@0
   119
  if (c == traits_type::eof())
sl@0
   120
    return traits_type::not_eof(c);
sl@0
   121
sl@0
   122
  // Try to expand the buffer.
sl@0
   123
  if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
sl@0
   124
    ptrdiff_t old_size = epptr() - pbase();
sl@0
   125
    ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
sl@0
   126
sl@0
   127
    char* buf = _M_alloc(new_size);
sl@0
   128
    if (buf) {
sl@0
   129
      memcpy(buf, pbase(), old_size);
sl@0
   130
sl@0
   131
      char* old_buffer = pbase();
sl@0
   132
      bool reposition_get = false;
sl@0
   133
      ptrdiff_t old_get_offset;
sl@0
   134
      if (gptr() != 0) {
sl@0
   135
        reposition_get = true;
sl@0
   136
        old_get_offset = gptr() - eback();
sl@0
   137
      }
sl@0
   138
sl@0
   139
      setp(buf, buf + new_size);
sl@0
   140
      pbump((int)old_size);
sl@0
   141
sl@0
   142
      if (reposition_get)
sl@0
   143
        setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
sl@0
   144
sl@0
   145
      _M_free(old_buffer);
sl@0
   146
    }
sl@0
   147
  }
sl@0
   148
sl@0
   149
  if (pptr() != epptr()) {
sl@0
   150
    *pptr() = traits_type::to_char_type(c);
sl@0
   151
    pbump(1);
sl@0
   152
    return c;
sl@0
   153
  }
sl@0
   154
  else
sl@0
   155
    return traits_type::eof();
sl@0
   156
}
sl@0
   157
sl@0
   158
_STLP_DECLSPEC strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
sl@0
   159
  if (gptr() != eback()) {
sl@0
   160
    if (c == traits_type::eof()) {
sl@0
   161
      gbump(-1);
sl@0
   162
      return traits_type::not_eof(c);
sl@0
   163
    }
sl@0
   164
    else if (c == gptr()[-1]) {
sl@0
   165
      gbump(-1);
sl@0
   166
      return c;
sl@0
   167
    }
sl@0
   168
    else if (!_M_constant) {
sl@0
   169
      gbump(-1);
sl@0
   170
      *gptr() = traits_type::to_char_type(c);
sl@0
   171
      return c;
sl@0
   172
    }
sl@0
   173
  }
sl@0
   174
sl@0
   175
  return traits_type::eof();
sl@0
   176
}
sl@0
   177
sl@0
   178
_STLP_DECLSPEC strstreambuf::int_type strstreambuf::underflow() {
sl@0
   179
  if (gptr() == egptr() && pptr() && pptr() > egptr())
sl@0
   180
    setg(eback(), gptr(), pptr());
sl@0
   181
sl@0
   182
  if (gptr() != egptr())
sl@0
   183
    return (unsigned char) *gptr();
sl@0
   184
  else
sl@0
   185
    return _Traits::eof();
sl@0
   186
}
sl@0
   187
sl@0
   188
_STLP_DECLSPEC basic_streambuf<char, char_traits<char> >*
sl@0
   189
strstreambuf::setbuf(char*, streamsize) {
sl@0
   190
  return this;
sl@0
   191
}
sl@0
   192
sl@0
   193
_STLP_DECLSPEC strstreambuf::pos_type
sl@0
   194
strstreambuf::seekoff(off_type off,
sl@0
   195
                      ios_base::seekdir dir, ios_base::openmode mode) {
sl@0
   196
  bool do_get = false;
sl@0
   197
  bool do_put = false;
sl@0
   198
sl@0
   199
  if ((mode & (ios_base::in | ios_base::out)) ==
sl@0
   200
          (ios_base::in | ios_base::out) &&
sl@0
   201
      (dir == ios_base::beg || dir == ios_base::end))
sl@0
   202
    do_get = do_put = true;
sl@0
   203
  else if (mode & ios_base::in)
sl@0
   204
    do_get = true;
sl@0
   205
  else if (mode & ios_base::out)
sl@0
   206
    do_put = true;
sl@0
   207
sl@0
   208
  // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
sl@0
   209
  // area is undefined if there is no get area.
sl@0
   210
  if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
sl@0
   211
    return pos_type(off_type(-1));
sl@0
   212
sl@0
   213
  char* seeklow  = eback();
sl@0
   214
  char* seekhigh = epptr() ? epptr() : egptr();
sl@0
   215
sl@0
   216
  off_type newoff;
sl@0
   217
  switch(dir) {
sl@0
   218
  case ios_base::beg:
sl@0
   219
    newoff = 0;
sl@0
   220
    break;
sl@0
   221
  case ios_base::end:
sl@0
   222
    newoff = seekhigh - seeklow;
sl@0
   223
    break;
sl@0
   224
  case ios_base::cur:
sl@0
   225
    newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
sl@0
   226
    break;
sl@0
   227
  default:
sl@0
   228
    return pos_type(off_type(-1));
sl@0
   229
  }
sl@0
   230
sl@0
   231
  off += newoff;
sl@0
   232
  if (off < 0 || off > seekhigh - seeklow)
sl@0
   233
    return pos_type(off_type(-1));
sl@0
   234
sl@0
   235
  if (do_put) {
sl@0
   236
    if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
sl@0
   237
      setp(seeklow, epptr());
sl@0
   238
      pbump((int)off);
sl@0
   239
    }
sl@0
   240
    else {
sl@0
   241
      setp(pbase(), epptr());
sl@0
   242
      pbump((int)(off - (pbase() - seeklow)));
sl@0
   243
    }
sl@0
   244
  }
sl@0
   245
  if (do_get) {
sl@0
   246
    if (off <= egptr() - seeklow)
sl@0
   247
      setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
sl@0
   248
    else if (off <= pptr() - seeklow)
sl@0
   249
      setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
sl@0
   250
    else
sl@0
   251
      setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
sl@0
   252
  }
sl@0
   253
sl@0
   254
  return pos_type(newoff);
sl@0
   255
}
sl@0
   256
sl@0
   257
_STLP_DECLSPEC strstreambuf::pos_type
sl@0
   258
strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
sl@0
   259
  return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
sl@0
   260
}
sl@0
   261
sl@0
   262
sl@0
   263
char* strstreambuf::_M_alloc(size_t n) {
sl@0
   264
  if (_M_alloc_fun)
sl@0
   265
    return __STATIC_CAST(char*,_M_alloc_fun(n));
sl@0
   266
  else
sl@0
   267
    return new char[n];
sl@0
   268
}
sl@0
   269
sl@0
   270
void strstreambuf::_M_free(char* p) {
sl@0
   271
  if (p)
sl@0
   272
    if (_M_free_fun)
sl@0
   273
      _M_free_fun(p);
sl@0
   274
    else
sl@0
   275
      delete[] p;
sl@0
   276
}
sl@0
   277
sl@0
   278
void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
sl@0
   279
  if (get) {
sl@0
   280
    size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
sl@0
   281
sl@0
   282
    if (put) {
sl@0
   283
      setg(get, get, get + N);
sl@0
   284
      setp(put, put + N);
sl@0
   285
    }
sl@0
   286
    else {
sl@0
   287
      setg(get, get, get + N);
sl@0
   288
    }
sl@0
   289
  }
sl@0
   290
}
sl@0
   291
sl@0
   292
//----------------------------------------------------------------------
sl@0
   293
// Class istrstream
sl@0
   294
sl@0
   295
_STLP_DECLSPEC istrstream::istrstream(char* s)
sl@0
   296
  : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
sl@0
   297
  this->init(&_M_buf);
sl@0
   298
}
sl@0
   299
sl@0
   300
_STLP_DECLSPEC istrstream::istrstream(const char* s)
sl@0
   301
  : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
sl@0
   302
  this->init(&_M_buf);
sl@0
   303
}
sl@0
   304
sl@0
   305
_STLP_DECLSPEC istrstream::istrstream(char* s, streamsize n)
sl@0
   306
  : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
sl@0
   307
  this->init(&_M_buf);
sl@0
   308
}
sl@0
   309
sl@0
   310
_STLP_DECLSPEC istrstream::istrstream(const char* s, streamsize n)
sl@0
   311
  : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
sl@0
   312
  this->init(&_M_buf);
sl@0
   313
}
sl@0
   314
sl@0
   315
_STLP_DECLSPEC istrstream::~istrstream() {}
sl@0
   316
sl@0
   317
_STLP_DECLSPEC strstreambuf* istrstream::rdbuf() const {
sl@0
   318
  return __CONST_CAST(strstreambuf*,&_M_buf);
sl@0
   319
}
sl@0
   320
sl@0
   321
_STLP_DECLSPEC char* istrstream::str() { return _M_buf.str(); }
sl@0
   322
sl@0
   323
//----------------------------------------------------------------------
sl@0
   324
// Class ostrstream
sl@0
   325
sl@0
   326
_STLP_DECLSPEC ostrstream::ostrstream()
sl@0
   327
  : basic_ostream<char, char_traits<char> >(0), _M_buf() {
sl@0
   328
  basic_ios<char, char_traits<char> >::init(&_M_buf);
sl@0
   329
}
sl@0
   330
sl@0
   331
_STLP_DECLSPEC ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
sl@0
   332
  : basic_ostream<char, char_traits<char> >(0),
sl@0
   333
    _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
sl@0
   334
  basic_ios<char, char_traits<char> >::init(&_M_buf);
sl@0
   335
}
sl@0
   336
sl@0
   337
_STLP_DECLSPEC ostrstream::~ostrstream() {}
sl@0
   338
sl@0
   339
_STLP_DECLSPEC strstreambuf* ostrstream::rdbuf() const {
sl@0
   340
  return __CONST_CAST(strstreambuf*,&_M_buf);
sl@0
   341
}
sl@0
   342
sl@0
   343
_STLP_DECLSPEC void ostrstream::freeze(bool freezeflag) {
sl@0
   344
  _M_buf.freeze(freezeflag);
sl@0
   345
}
sl@0
   346
sl@0
   347
_STLP_DECLSPEC char* ostrstream::str() {
sl@0
   348
  return _M_buf.str();
sl@0
   349
}
sl@0
   350
sl@0
   351
_STLP_DECLSPEC int ostrstream::pcount() const {
sl@0
   352
  return _M_buf.pcount();
sl@0
   353
}
sl@0
   354
sl@0
   355
sl@0
   356
//----------------------------------------------------------------------
sl@0
   357
// Class strstream
sl@0
   358
sl@0
   359
_STLP_DECLSPEC strstream::strstream()
sl@0
   360
  : basic_iostream<char, char_traits<char> >(0), _M_buf() {
sl@0
   361
  basic_ios<char, char_traits<char> >::init(&_M_buf);
sl@0
   362
}
sl@0
   363
sl@0
   364
_STLP_DECLSPEC strstream::strstream(char* s, int n, ios_base::openmode mode)
sl@0
   365
  : basic_iostream<char, char_traits<char> >(0),
sl@0
   366
    _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
sl@0
   367
  basic_ios<char, char_traits<char> >::init(&_M_buf);
sl@0
   368
}
sl@0
   369
sl@0
   370
_STLP_DECLSPEC strstream::~strstream() {}
sl@0
   371
sl@0
   372
_STLP_DECLSPEC strstreambuf* strstream::rdbuf() const {
sl@0
   373
  return __CONST_CAST(strstreambuf*,&_M_buf);
sl@0
   374
}
sl@0
   375
sl@0
   376
_STLP_DECLSPEC void strstream::freeze(bool freezeflag) {
sl@0
   377
  _M_buf.freeze(freezeflag);
sl@0
   378
}
sl@0
   379
sl@0
   380
_STLP_DECLSPEC int strstream::pcount() const {
sl@0
   381
  return _M_buf.pcount();
sl@0
   382
}
sl@0
   383
sl@0
   384
_STLP_DECLSPEC char* strstream::str() {
sl@0
   385
  return _M_buf.str();
sl@0
   386
}
sl@0
   387
sl@0
   388
_STLP_END_NAMESPACE
sl@0
   389
sl@0
   390
// Local Variables:
sl@0
   391
// mode:C++
sl@0
   392
// End: