blob: a10ce1bf5636270375d6ff032ccc98ba1d8c0478 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- streambuf ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STEAMBUF
12#define _LIBCPP_STEAMBUF
13
14/*
15 streambuf synopsis
16
17namespace std
18{
19
20template <class charT, class traits = char_traits<charT> >
21class basic_streambuf
22{
23public:
24 // types:
25 typedef charT char_type;
26 typedef traits traits_type;
27 typedef typename traits_type::int_type int_type;
28 typedef typename traits_type::pos_type pos_type;
29 typedef typename traits_type::off_type off_type;
30
31 virtual ~basic_streambuf();
32
33 // 27.6.2.2.1 locales:
34 locale pubimbue(const locale& loc);
35 locale getloc() const;
36
37 // 27.6.2.2.2 buffer and positioning:
38 basic_streambuf* pubsetbuf(char_type* s, streamsize n);
39 pos_type pubseekoff(off_type off, ios_base::seekdir way,
40 ios_base::openmode which = ios_base::in | ios_base::out);
41 pos_type pubseekpos(pos_type sp,
42 ios_base::openmode which = ios_base::in | ios_base::out);
43 int pubsync();
44
45 // Get and put areas:
46 // 27.6.2.2.3 Get area:
47 streamsize in_avail();
48 int_type snextc();
49 int_type sbumpc();
50 int_type sgetc();
51 streamsize sgetn(char_type* s, streamsize n);
52
53 // 27.6.2.2.4 Putback:
54 int_type sputbackc(char_type c);
55 int_type sungetc();
56
57 // 27.6.2.2.5 Put area:
58 int_type sputc(char_type c);
59 streamsize sputn(const char_type* s, streamsize n);
60
61protected:
62 basic_streambuf();
63 basic_streambuf(const basic_streambuf& rhs);
64 basic_streambuf& operator=(const basic_streambuf& rhs);
65 void swap(basic_streambuf& rhs);
66
67 // 27.6.2.3.2 Get area:
68 char_type* eback() const;
69 char_type* gptr() const;
70 char_type* egptr() const;
71 void gbump(int n);
72 void setg(char_type* gbeg, char_type* gnext, char_type* gend);
73
74 // 27.6.2.3.3 Put area:
75 char_type* pbase() const;
76 char_type* pptr() const;
77 char_type* epptr() const;
78 void pbump(int n);
79 void setp(char_type* pbeg, char_type* pend);
80
81 // 27.6.2.4 virtual functions:
82 // 27.6.2.4.1 Locales:
83 virtual void imbue(const locale& loc);
84
85 // 27.6.2.4.2 Buffer management and positioning:
86 virtual basic_streambuf* setbuf(char_type* s, streamsize n);
87 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
88 ios_base::openmode which = ios_base::in | ios_base::out);
89 virtual pos_type seekpos(pos_type sp,
90 ios_base::openmode which = ios_base::in | ios_base::out);
91 virtual int sync();
92
93 // 27.6.2.4.3 Get area:
94 virtual streamsize showmanyc();
95 virtual streamsize xsgetn(char_type* s, streamsize n);
96 virtual int_type underflow();
97 virtual int_type uflow();
98
99 // 27.6.2.4.4 Putback:
100 virtual int_type pbackfail(int_type c = traits_type::eof());
101
102 // 27.6.2.4.5 Put area:
103 virtual streamsize xsputn(const char_type* s, streamsize n);
104 virtual int_type overflow (int_type c = traits_type::eof());
105};
106
107} // std
108
109*/
110
111#include <__config>
112#include <iosfwd>
113#include <ios>
114
Howard Hinnant08e17472011-10-17 20:05:10 +0000115#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000117#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118
Eric Fiselier018a3d52017-05-31 22:07:49 +0000119_LIBCPP_PUSH_MACROS
120#include <__undef_macros>
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122_LIBCPP_BEGIN_NAMESPACE_STD
123
124template <class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000125class _LIBCPP_TEMPLATE_VIS basic_streambuf
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126{
127public:
128 // types:
129 typedef _CharT char_type;
130 typedef _Traits traits_type;
131 typedef typename traits_type::int_type int_type;
132 typedef typename traits_type::pos_type pos_type;
133 typedef typename traits_type::off_type off_type;
134
135 virtual ~basic_streambuf();
136
137 // 27.6.2.2.1 locales:
Eric Fiselier6dbed462016-09-16 00:00:48 +0000138 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
139 locale pubimbue(const locale& __loc) {
140 imbue(__loc);
141 locale __r = __loc_;
142 __loc_ = __loc;
143 return __r;
144 }
145
146 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
147 locale getloc() const { return __loc_; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148
149 // 27.6.2.2.2 buffer and positioning:
Eric Fiselier6dbed462016-09-16 00:00:48 +0000150 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
151 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n)
152 { return setbuf(__s, __n); }
153
154 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
Eric Fiselier6dbed462016-09-16 00:00:48 +0000156 ios_base::openmode __which = ios_base::in | ios_base::out)
157 { return seekoff(__off, __way, __which); }
158
159 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 pos_type pubseekpos(pos_type __sp,
Eric Fiselier6dbed462016-09-16 00:00:48 +0000161 ios_base::openmode __which = ios_base::in | ios_base::out)
162 { return seekpos(__sp, __which); }
163
164 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
165 int pubsync() { return sync(); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166
167 // Get and put areas:
168 // 27.6.2.2.3 Get area:
Eric Fiselier6dbed462016-09-16 00:00:48 +0000169 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
170 streamsize in_avail() {
171 if (__ninp_ < __einp_)
172 return static_cast<streamsize>(__einp_ - __ninp_);
173 return showmanyc();
174 }
175
176 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
177 int_type snextc() {
178 if (sbumpc() == traits_type::eof())
179 return traits_type::eof();
180 return sgetc();
181 }
182
183 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
184 int_type sbumpc() {
185 if (__ninp_ == __einp_)
186 return uflow();
187 return traits_type::to_int_type(*__ninp_++);
188 }
189
190 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
191 int_type sgetc() {
192 if (__ninp_ == __einp_)
193 return underflow();
194 return traits_type::to_int_type(*__ninp_);
195 }
196
197 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
198 streamsize sgetn(char_type* __s, streamsize __n)
199 { return xsgetn(__s, __n); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200
201 // 27.6.2.2.4 Putback:
Eric Fiselier6dbed462016-09-16 00:00:48 +0000202 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
203 int_type sputbackc(char_type __c) {
204 if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
205 return pbackfail(traits_type::to_int_type(__c));
206 return traits_type::to_int_type(*--__ninp_);
207 }
208
209 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
210 int_type sungetc() {
211 if (__binp_ == __ninp_)
212 return pbackfail();
213 return traits_type::to_int_type(*--__ninp_);
214 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215
216 // 27.6.2.2.5 Put area:
Eric Fiselier6dbed462016-09-16 00:00:48 +0000217 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
218 int_type sputc(char_type __c) {
219 if (__nout_ == __eout_)
220 return overflow(traits_type::to_int_type(__c));
221 *__nout_++ = __c;
222 return traits_type::to_int_type(__c);
223 }
224
225 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
226 streamsize sputn(const char_type* __s, streamsize __n)
227 { return xsputn(__s, __n); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228
229protected:
230 basic_streambuf();
231 basic_streambuf(const basic_streambuf& __rhs);
232 basic_streambuf& operator=(const basic_streambuf& __rhs);
233 void swap(basic_streambuf& __rhs);
234
235 // 27.6.2.3.2 Get area:
236 _LIBCPP_ALWAYS_INLINE char_type* eback() const {return __binp_;}
237 _LIBCPP_ALWAYS_INLINE char_type* gptr() const {return __ninp_;}
238 _LIBCPP_ALWAYS_INLINE char_type* egptr() const {return __einp_;}
Eric Fiselier6dbed462016-09-16 00:00:48 +0000239
240 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
241 void gbump(int __n) { __ninp_ += __n; }
242
243 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
244 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
245 __binp_ = __gbeg;
246 __ninp_ = __gnext;
247 __einp_ = __gend;
248 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249
250 // 27.6.2.3.3 Put area:
251 _LIBCPP_ALWAYS_INLINE char_type* pbase() const {return __bout_;}
252 _LIBCPP_ALWAYS_INLINE char_type* pptr() const {return __nout_;}
253 _LIBCPP_ALWAYS_INLINE char_type* epptr() const {return __eout_;}
Eric Fiselier6dbed462016-09-16 00:00:48 +0000254
255 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
256 void pbump(int __n) { __nout_ += __n; }
257
258 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
259 void setp(char_type* __pbeg, char_type* __pend) {
260 __bout_ = __nout_ = __pbeg;
261 __eout_ = __pend;
262 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263
264 // 27.6.2.4 virtual functions:
265 // 27.6.2.4.1 Locales:
266 virtual void imbue(const locale& __loc);
267
268 // 27.6.2.4.2 Buffer management and positioning:
269 virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
270 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
271 ios_base::openmode __which = ios_base::in | ios_base::out);
272 virtual pos_type seekpos(pos_type __sp,
273 ios_base::openmode __which = ios_base::in | ios_base::out);
274 virtual int sync();
275
276 // 27.6.2.4.3 Get area:
277 virtual streamsize showmanyc();
278 virtual streamsize xsgetn(char_type* __s, streamsize __n);
279 virtual int_type underflow();
280 virtual int_type uflow();
281
282 // 27.6.2.4.4 Putback:
283 virtual int_type pbackfail(int_type __c = traits_type::eof());
284
285 // 27.6.2.4.5 Put area:
286 virtual streamsize xsputn(const char_type* __s, streamsize __n);
287 virtual int_type overflow(int_type __c = traits_type::eof());
288
289private:
290 locale __loc_;
291 char_type* __binp_;
292 char_type* __ninp_;
293 char_type* __einp_;
294 char_type* __bout_;
295 char_type* __nout_;
296 char_type* __eout_;
297};
298
299template <class _CharT, class _Traits>
300basic_streambuf<_CharT, _Traits>::~basic_streambuf()
301{
302}
303
304template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305basic_streambuf<_CharT, _Traits>::basic_streambuf()
306 : __binp_(0),
307 __ninp_(0),
308 __einp_(0),
309 __bout_(0),
310 __nout_(0),
311 __eout_(0)
312{
313}
314
315template <class _CharT, class _Traits>
316basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
317 : __loc_(__sb.__loc_),
318 __binp_(__sb.__binp_),
319 __ninp_(__sb.__ninp_),
320 __einp_(__sb.__einp_),
321 __bout_(__sb.__bout_),
322 __nout_(__sb.__nout_),
323 __eout_(__sb.__eout_)
324{
325}
326
327template <class _CharT, class _Traits>
328basic_streambuf<_CharT, _Traits>&
329basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
330{
331 __loc_ = __sb.__loc_;
332 __binp_ = __sb.__binp_;
333 __ninp_ = __sb.__ninp_;
334 __einp_ = __sb.__einp_;
335 __bout_ = __sb.__bout_;
336 __nout_ = __sb.__nout_;
337 __eout_ = __sb.__eout_;
338 return *this;
339}
340
341template <class _CharT, class _Traits>
342void
343basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
344{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000345 _VSTD::swap(__loc_, __sb.__loc_);
346 _VSTD::swap(__binp_, __sb.__binp_);
347 _VSTD::swap(__ninp_, __sb.__ninp_);
348 _VSTD::swap(__einp_, __sb.__einp_);
349 _VSTD::swap(__bout_, __sb.__bout_);
350 _VSTD::swap(__nout_, __sb.__nout_);
351 _VSTD::swap(__eout_, __sb.__eout_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352}
353
354template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355void
356basic_streambuf<_CharT, _Traits>::imbue(const locale&)
357{
358}
359
360template <class _CharT, class _Traits>
361basic_streambuf<_CharT, _Traits>*
362basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
363{
364 return this;
365}
366
367template <class _CharT, class _Traits>
368typename basic_streambuf<_CharT, _Traits>::pos_type
Howard Hinnantec3773c2011-12-01 20:21:04 +0000369basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
370 ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
372 return pos_type(off_type(-1));
373}
374
375template <class _CharT, class _Traits>
376typename basic_streambuf<_CharT, _Traits>::pos_type
Howard Hinnantec3773c2011-12-01 20:21:04 +0000377basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378{
379 return pos_type(off_type(-1));
380}
381
382template <class _CharT, class _Traits>
383int
384basic_streambuf<_CharT, _Traits>::sync()
385{
386 return 0;
387}
388
389template <class _CharT, class _Traits>
390streamsize
391basic_streambuf<_CharT, _Traits>::showmanyc()
392{
393 return 0;
394}
395
396template <class _CharT, class _Traits>
397streamsize
398basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
399{
400 const int_type __eof = traits_type::eof();
401 int_type __c;
402 streamsize __i = 0;
Evandro Menezes42e85272016-06-10 16:00:29 +0000403 while(__i < __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 {
405 if (__ninp_ < __einp_)
Evandro Menezes42e85272016-06-10 16:00:29 +0000406 {
407 const streamsize __len = _VSTD::min(__einp_ - __ninp_, __n - __i);
408 traits_type::copy(__s, __ninp_, __len);
409 __s += __len;
410 __i += __len;
411 this->gbump(__len);
412 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413 else if ((__c = uflow()) != __eof)
Evandro Menezes42e85272016-06-10 16:00:29 +0000414 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 *__s = traits_type::to_char_type(__c);
Evandro Menezes42e85272016-06-10 16:00:29 +0000416 ++__s;
417 ++__i;
418 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 else
420 break;
421 }
422 return __i;
423}
424
425template <class _CharT, class _Traits>
426typename basic_streambuf<_CharT, _Traits>::int_type
427basic_streambuf<_CharT, _Traits>::underflow()
428{
429 return traits_type::eof();
430}
431
432template <class _CharT, class _Traits>
433typename basic_streambuf<_CharT, _Traits>::int_type
434basic_streambuf<_CharT, _Traits>::uflow()
435{
436 if (underflow() == traits_type::eof())
437 return traits_type::eof();
438 return traits_type::to_int_type(*__ninp_++);
439}
440
441template <class _CharT, class _Traits>
442typename basic_streambuf<_CharT, _Traits>::int_type
443basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
444{
445 return traits_type::eof();
446}
447
448template <class _CharT, class _Traits>
449streamsize
450basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
451{
452 streamsize __i = 0;
453 int_type __eof = traits_type::eof();
Marshall Clow6b5a5e52015-02-19 16:17:46 +0000454 while( __i < __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455 {
Marshall Clow6b5a5e52015-02-19 16:17:46 +0000456 if (__nout_ >= __eout_)
457 {
458 if (overflow(traits_type::to_int_type(*__s)) == __eof)
459 break;
460 ++__s;
461 ++__i;
462 }
463 else
464 {
465 streamsize __chunk_size = _VSTD::min(__eout_ - __nout_, __n - __i);
466 traits_type::copy(__nout_, __s, __chunk_size);
467 __nout_ += __chunk_size;
468 __s += __chunk_size;
469 __i += __chunk_size;
470 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 }
472 return __i;
473}
474
475template <class _CharT, class _Traits>
476typename basic_streambuf<_CharT, _Traits>::int_type
Howard Hinnantec3773c2011-12-01 20:21:04 +0000477basic_streambuf<_CharT, _Traits>::overflow(int_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
479 return traits_type::eof();
480}
481
Mehdi Amini907c1192017-05-04 17:08:54 +0000482#ifndef _LIBCPP_AVAILABILITY_NO_STREAMS_EXTERN_TEMPLATE
Eric Fiselier833d6442016-09-15 22:27:07 +0000483_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>)
484_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485
Eric Fiselier833d6442016-09-15 22:27:07 +0000486_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)
487_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<wchar_t>)
Mehdi Amini907c1192017-05-04 17:08:54 +0000488#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490_LIBCPP_END_NAMESPACE_STD
491
Eric Fiselier018a3d52017-05-31 22:07:49 +0000492_LIBCPP_POP_MACROS
493
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494#endif // _LIBCPP_STEAMBUF