home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libio / strstream.cc < prev    next >
C/C++ Source or Header  |  1995-06-15  |  4KB  |  169 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this library; see the file COPYING.  If not, write to the Free
  17. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /* Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #ifdef __GNUG__
  28. #pragma implementation
  29. #endif
  30. #include "iostreamP.h"
  31. #include "strstream.h"
  32. #include <string.h>
  33.  
  34. static void* default_alloc(_IO_size_t size)
  35. {
  36.     return (void*)new char[size];
  37. }
  38.  
  39. static void default_free(void* ptr)
  40. {
  41.     delete [] (char*)ptr;
  42. }
  43.  
  44. #if _IO_UNIFIED_JUMPTABLES
  45. #define SET_STR_JUMPS(STRBUF)  /* Nothing */
  46. #else
  47. /* Set to use the _IO_str_jump jumptable, for efficiency */
  48.  
  49. #define SET_STR_JUMPS(STRBUF) \
  50.   (STRBUF)->_jumps = &_IO_str_jumps,\
  51.   (STRBUF)->_vtable() = builtinbuf_vtable;
  52. #endif
  53.  
  54. istrstream::istrstream(const char *cp, int n)
  55. {
  56. #ifdef _IO_NEW_STREAMS
  57.   __my_sb.init_readonly (cp, n);
  58.   init (&__my_sb);
  59. #else
  60.   init(new strstreambuf(cp, n));
  61.   _flags &= ~ios::dont_close;
  62. #endif
  63.   SET_STR_JUMPS(_strbuf);
  64. }
  65.  
  66. ostrstream::ostrstream()
  67. {
  68. #ifdef _IO_NEW_STREAMS
  69.   init (&__my_sb);
  70. #else
  71.   init(new strstreambuf());
  72.   _flags &= ~ios::dont_close;
  73. #endif
  74.   SET_STR_JUMPS(_strbuf);
  75. }
  76.  
  77. strstream::strstream()
  78. {
  79. #ifdef _IO_NEW_STREAMS
  80.   init (&__my_sb);
  81. #else
  82.   init(new strstreambuf());
  83.   _flags &= ~ios::dont_close;
  84. #endif
  85.   SET_STR_JUMPS(_strbuf);
  86. }
  87.  
  88. strstreambase::strstreambase(char *cp, int n, int mode)
  89. #ifdef _IO_NEW_STREAMS
  90. : __my_sb (cp, n,
  91.        (mode == ios::app || mode == ios::ate) ? cp + strlen(cp) : cp)
  92. #endif
  93. {
  94. #ifdef _IO_NEW_STREAMS
  95.   init (&__my_sb);
  96. #else
  97.   char *pstart;
  98.   if (mode == ios::app || mode == ios::ate)
  99.     pstart = cp + strlen(cp);
  100.   else
  101.     pstart = cp;
  102.   init(new strstreambuf(cp, n, pstart));
  103.   _flags &= ~ios::dont_close;
  104. #endif
  105.   SET_STR_JUMPS(_strbuf);
  106. }
  107.  
  108. char *strstreambuf::str()
  109. {
  110.     freeze(1);
  111.     return base();
  112. }
  113.  
  114. _IO_ssize_t strstreambuf::pcount () { return _IO_write_ptr - _IO_write_base; }
  115.  
  116. int strstreambuf::overflow(int c /* = EOF */)
  117. {
  118.   return _IO_str_overflow (this, c);
  119. }
  120.  
  121. int strstreambuf::underflow()
  122. {
  123.   return _IO_str_underflow(this);
  124. }
  125.  
  126.  
  127. void strstreambuf::init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
  128.                 int initial_size)
  129.                 
  130. {
  131.     _s._len = 0;
  132.     if (initial_size < 16)
  133.     initial_size = 16;
  134.     _s._allocate_buffer = alloc ? alloc : default_alloc;
  135.     _s._free_buffer = free ? free : default_free;
  136.     char * buf = (char*)(*_s._allocate_buffer)(initial_size);
  137.     setb(buf, buf + initial_size, 1);
  138.     setp(buf, buf + initial_size);
  139.     setg(buf, buf, buf);
  140. }
  141.  
  142. void strstreambuf::init_static(char *ptr, int size, char *pstart)
  143. {
  144.   _IO_str_init_static (this, ptr, size, pstart);
  145. }
  146.  
  147. void strstreambuf::init_readonly (const char *ptr, int size)
  148. {
  149.   _IO_str_init_readonly (this, ptr, size);
  150. }
  151.  
  152. strstreambuf::~strstreambuf()
  153. {
  154.     if (_IO_buf_base && !(_flags & _IO_USER_BUF))
  155.         (_s._free_buffer)(_IO_buf_base);
  156.     _IO_buf_base = NULL;
  157. }
  158.  
  159. streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
  160.                     int mode /*=ios::in|ios::out*/)
  161. {
  162.   return _IO_str_seekoff (this, off, dir, mode);
  163. }
  164.  
  165. int strstreambuf::pbackfail(int c)
  166. {
  167.   return _IO_str_pbackfail (this, c);
  168. }
  169.