OSDN Git Service

2002-02-15 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / sstream.tcc
1 // String based streams -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 27.7  String-based streams
33 //
34
35 #ifndef _CPP_BITS_SSTREAM_TCC
36 #define _CPP_BITS_SSTREAM_TCC   1
37
38 #include <sstream>
39
40 namespace std
41 {
42   template <class _CharT, class _Traits, class _Alloc>
43     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type 
44     basic_stringbuf<_CharT, _Traits, _Alloc>::
45     pbackfail(int_type __c)
46     {
47       int_type __ret = traits_type::eof();
48       bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
49       bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur; 
50       
51       // Try to put back __c into input sequence in one of three ways.
52       // Order these tests done in is unspecified by the standard.
53       if (__testpos)
54         {
55           if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])
56               && !__testeof)
57             {
58               --_M_in_cur;
59               __ret = __c;
60             }
61           else if (!__testeof)
62             {
63               --_M_in_cur;
64               *_M_in_cur = traits_type::to_char_type(__c);
65               __ret = __c;
66             }
67           else if (__testeof)
68             {
69               --_M_in_cur;
70               __ret = traits_type::not_eof(__c);
71             }
72         }
73       return __ret;
74     }
75   
76   template <class _CharT, class _Traits, class _Alloc>
77     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type 
78     basic_stringbuf<_CharT, _Traits, _Alloc>::
79     overflow(int_type __c)
80     {
81       int_type __ret = traits_type::eof();
82       bool __testeof = traits_type::eq_int_type(__c, __ret);
83       bool __testwrite = _M_out_cur < _M_buf + _M_buf_size;
84       bool __testout = _M_mode & ios_base::out;
85
86       // Try to append __c into output sequence in one of two ways.
87       // Order these tests done in is unspecified by the standard.
88       if (__testout)
89         {
90           if (!__testeof)
91             {
92               __size_type __len = max(_M_buf_size, _M_buf_size_opt);
93               __len *= 2;
94
95               if (__testwrite)
96                 __ret = this->sputc(__c);
97               else if (__len <= _M_string.max_size())
98                 {
99                   // Force-allocate, re-sync.
100                   _M_string = this->str();
101                   _M_string.reserve(__len);
102                   _M_buf_size = static_cast<int_type>(__len);
103                   _M_really_sync(_M_in_cur - _M_in_beg, 
104                                  _M_out_cur - _M_out_beg);
105                   *_M_out_cur = traits_type::to_char_type(__c);
106                   _M_out_cur_move(1);
107                   __ret = __c;
108                 }
109             }
110           else
111             __ret = traits_type::not_eof(__c);
112         }
113       return __ret;
114     }
115
116   template <class _CharT, class _Traits, class _Alloc>
117     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
118     basic_stringbuf<_CharT, _Traits, _Alloc>::
119     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
120     {
121       pos_type __ret =  pos_type(off_type(-1)); 
122       bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
123       bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
124       bool __testboth = __testin && __testout && __way != ios_base::cur;
125       
126       if (_M_buf_size && ((__testin != __testout) || __testboth))
127         {
128           char_type* __beg = _M_buf;
129           char_type* __curi = NULL;
130           char_type* __curo = NULL;
131           char_type* __endi = NULL;
132           char_type* __endo = NULL;
133
134           if (__testin)
135             {
136               __curi = this->gptr();
137               __endi = this->egptr();
138             }
139           if (__testout)
140             {
141               __curo = this->pptr();
142               __endo = this->epptr();
143             }
144
145           off_type __newoffi = 0;
146           off_type __newoffo = 0;
147           if (__way == ios_base::cur)
148             {
149               __newoffi = __curi - __beg;
150               __newoffo = __curo - __beg;
151             }
152           else if (__way == ios_base::end)
153             {
154               __newoffi = __endi - __beg;
155               __newoffo = __endo - __beg;
156             }
157
158           if (__testin
159               && __newoffi + __off >= 0 && __endi - __beg >= __newoffi + __off)
160             {
161               _M_in_cur = __beg + __newoffi + __off;
162               __ret = pos_type(__newoffi);
163             }
164           if (__testout
165               && __newoffo + __off >= 0 && __endo - __beg >= __newoffo + __off)
166             {
167               _M_out_cur_move(__newoffo + __off - (_M_out_cur - __beg));
168               __ret = pos_type(__newoffo);
169             }
170         }
171       return __ret;
172     }
173
174   template <class _CharT, class _Traits, class _Alloc>
175     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
176     basic_stringbuf<_CharT, _Traits, _Alloc>::
177     seekpos(pos_type __sp, ios_base::openmode __mode)
178     {
179       pos_type __ret =  pos_type(off_type(-1)); 
180       off_type __pos = __sp._M_position();
181       char_type* __beg = NULL;
182       char_type* __end = NULL;
183       bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
184       bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
185       
186       if (__testin)
187         {
188           __beg = this->eback();
189           __end = this->egptr();
190         }
191       if (__testout)
192         {
193           __beg = this->pbase();
194           __end = _M_buf + _M_buf_size;
195         }
196  
197       if (0 <= __pos && __pos <= __end - __beg)
198         {
199           // Need to set both of these if applicable
200           if (__testin)
201             _M_in_cur = _M_in_beg + __pos;
202           if (__testout)
203             _M_out_cur_move((__pos) - (_M_out_cur - __beg));
204           __ret = pos_type(off_type(__pos));
205         }
206       
207       return __ret;
208     }
209
210   // Inhibit implicit instantiations for required instantiations,
211   // which are defined via explicit instantiations elsewhere.  
212   // NB:  This syntax is a GNU extension.
213   extern template class basic_stringbuf<char>;
214   extern template class basic_stringbuf<wchar_t>;
215   extern template class basic_istringstream<char>;
216   extern template class basic_istringstream<wchar_t>;
217   extern template class basic_ostringstream<char>;
218   extern template class basic_ostringstream<wchar_t>;
219   extern template class basic_stringstream<char>;
220   extern template class basic_stringstream<wchar_t>;
221 } // namespace std
222
223 #endif