2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * Neither the name of Nokia Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _STLP_INTERNAL_STREAMBUF
32 #include <stl/_streambuf.h>
35 #include <istream> // Includes <ostream>, <ios>, <iosfwd>
37 #ifndef _STLP_STRING_H
38 #include <stl/_string.h>
43 #ifndef _STLP_USE_NAMESPACES
44 # define strstream _STLP_strstream
45 # define ostrstream _STLP_ostrstream
46 # define istrstream _STLP_istrstream
47 # define strstreambuf _STLP_strstreambuf
50 //----------------------------------------------------------------------
51 // Class strstreambuf, a streambuf class that manages an array of char.
52 // Note that this class is not a template.
54 class strstreambuf : public basic_streambuf<char, char_traits<char> >
56 class _STLP_CLASS_DECLSPEC strstreambuf : public basic_streambuf<char, char_traits<char> >
60 typedef char_traits<char> _Traits;
61 typedef basic_streambuf<char, char_traits<char> > _Base;
62 typedef void* (*__alloc_fn)(size_t);
63 typedef void (*__free_fn)(void*);
64 public: // Constructor, destructor
66 explicit strstreambuf(streamsize _Initial_capacity = 0);
68 _STLP_DECLSPEC strstreambuf(__alloc_fn, __free_fn);
70 _STLP_DECLSPEC strstreambuf(char* __get, streamsize __n, char* __put = 0);
71 _STLP_DECLSPEC strstreambuf(signed char* __get, streamsize __n, signed char* __put = 0);
72 _STLP_DECLSPEC strstreambuf(unsigned char* __get, streamsize __n, unsigned char* __put=0);
74 _STLP_DECLSPEC strstreambuf(const char* __get, streamsize __n);
75 _STLP_DECLSPEC strstreambuf(const signed char* __get, streamsize __n);
76 _STLP_DECLSPEC strstreambuf(const unsigned char* __get, streamsize __n);
78 virtual ~strstreambuf();
80 public: // strstreambuf operations.
81 _STLP_DECLSPEC void freeze(bool = true);
82 _STLP_DECLSPEC char* str();
83 _STLP_DECLSPEC int pcount() const;
85 protected: // Overridden virtual member functions.
86 virtual int_type overflow(int_type __c = _Traits::eof());
87 virtual int_type pbackfail(int_type __c = _Traits::eof());
88 virtual int_type underflow();
89 virtual _Base* setbuf(char* __buf, streamsize __n);
90 virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
91 ios_base::openmode __mode
92 = ios_base::in | ios_base::out);
93 virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
94 = ios_base::in | ios_base::out);
96 private: // Helper functions.
97 // Dynamic allocation, possibly using _M_alloc_fun and _M_free_fun.
98 char* _M_alloc(size_t);
99 inline void _M_free(char*);
101 // Helper function used in constructors.
102 void _M_setup(char* __get, char* __put, streamsize __n);
103 private: // Data members.
104 __alloc_fn _M_alloc_fun;
105 __free_fn _M_free_fun;
108 bool _M_constant : 1;
110 char* _pfrozenendsave;
111 char* _pgetfrozenendsave;
115 inline strstreambuf::~strstreambuf()
118 if (_M_dynamic && !_M_frozen)
121 _M_free_fun(eback());
126 if (_M_dynamic && !_M_frozen)
131 inline void strstreambuf::_M_free(char* p)
141 //----------------------------------------------------------------------
142 // Class istrstream, an istream that manages a strstreambuf.
145 NONSHARABLE_CLASS (istrstream) : public basic_istream<char, char_traits<char> >
147 class _STLP_CLASS_DECLSPEC istrstream : public basic_istream<char, char_traits<char> >
151 _STLP_DECLSPEC explicit istrstream(char*);
152 _STLP_DECLSPEC explicit istrstream(const char*);
153 _STLP_DECLSPEC istrstream(char* , streamsize);
154 _STLP_DECLSPEC istrstream(const char*, streamsize);
155 virtual ~istrstream();
157 _STLP_DECLSPEC strstreambuf* rdbuf() const;
158 _STLP_DECLSPEC char* str();
164 //----------------------------------------------------------------------
167 NONSHARABLE_CLASS (ostrstream) : public basic_ostream<char, char_traits<char> >
169 class _STLP_CLASS_DECLSPEC ostrstream : public basic_ostream<char, char_traits<char> >
173 _STLP_DECLSPEC ostrstream();
174 _STLP_DECLSPEC ostrstream(char*, int, ios_base::openmode = ios_base::out);
175 virtual ~ostrstream();
177 _STLP_DECLSPEC strstreambuf* rdbuf() const;
178 _STLP_DECLSPEC void freeze(bool = true);
179 _STLP_DECLSPEC char* str();
180 _STLP_DECLSPEC int pcount() const;
186 //----------------------------------------------------------------------
189 NONSHARABLE_CLASS (strstream) : public basic_iostream<char, char_traits<char> >
191 class _STLP_CLASS_DECLSPEC strstream : public basic_iostream<char, char_traits<char> >
195 typedef char char_type;
196 typedef char_traits<char>::int_type int_type;
197 typedef char_traits<char>::pos_type pos_type;
198 typedef char_traits<char>::off_type off_type;
200 _STLP_DECLSPEC strstream();
201 _STLP_DECLSPEC strstream(char*, int, ios_base::openmode = ios_base::in | ios_base::out);
202 virtual ~strstream();
204 _STLP_DECLSPEC strstreambuf* rdbuf() const;
205 _STLP_DECLSPEC void freeze(bool = true);
206 _STLP_DECLSPEC int pcount() const;
207 _STLP_DECLSPEC char* str();