Merge from vendor branch NTPD:
[dragonfly.git] / contrib / libio / strstream.cc
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 istrstream::istrstream(const char *cp, int n)
45 {
46   __my_sb.init_readonly (cp, n);
47 }
48
49 strstreambase::strstreambase(char *cp, int n, int mode)
50 : __my_sb (cp, n,
51            (mode == ios::app || mode == ios::ate) ? cp + strlen(cp) : cp)
52 {
53   init (&__my_sb);
54 }
55
56 char *strstreambuf::str()
57 {
58     freeze(1);
59     return base();
60 }
61
62 _IO_ssize_t strstreambuf::pcount () { return _IO_write_ptr - _IO_write_base; }
63
64 int strstreambuf::overflow(int c /* = EOF */)
65 {
66   return _IO_str_overflow (this, c);
67 }
68
69 int strstreambuf::underflow()
70 {
71   return _IO_str_underflow(this);
72 }
73
74
75 void strstreambuf::init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
76                                 int initial_size)
77                                 
78 {
79     _s._allocate_buffer = alloc ? alloc : default_alloc;
80     _s._free_buffer = free ? free : default_free;
81     if (initial_size > 0)
82       {
83         char * buf = (char*)(*_s._allocate_buffer)(initial_size);
84         setb(buf, buf + initial_size, 1);
85         setp(buf, buf + initial_size);
86         setg(buf, buf, buf);
87       }
88 }
89
90 void strstreambuf::init_static(char *ptr, int size, char *pstart)
91 {
92   _IO_str_init_static (this, ptr, size, pstart);
93 }
94
95 void strstreambuf::init_readonly (const char *ptr, int size)
96 {
97   _IO_str_init_readonly (this, ptr, size);
98 }
99
100 strstreambuf::~strstreambuf()
101 {
102     if (_IO_buf_base && !(_flags & _IO_USER_BUF))
103         (_s._free_buffer)(_IO_buf_base);
104     _IO_buf_base = NULL;
105 }
106
107 streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
108                                         int mode /*=ios::in|ios::out*/)
109 {
110   return _IO_str_seekoff (this, off, dir, mode);
111 }
112
113 int strstreambuf::pbackfail(int c)
114 {
115   return _IO_str_pbackfail (this, c);
116 }