]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/streambuf
Upgrade Unbound to 1.6.1. More to follow.
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / streambuf
1 // -*- C++ -*-
2 //===------------------------- streambuf ----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_STEAMBUF
12 #define _LIBCPP_STEAMBUF
13
14 /*
15     streambuf synopsis
16
17 namespace std
18 {
19
20 template <class charT, class traits = char_traits<charT> >
21 class basic_streambuf
22 {
23 public:
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
61 protected:
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
115 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
116 #pragma GCC system_header
117 #endif
118
119 _LIBCPP_PUSH_MACROS
120 #include <__undef_macros>
121
122 _LIBCPP_BEGIN_NAMESPACE_STD
123
124 template <class _CharT, class _Traits>
125 class _LIBCPP_TEMPLATE_VIS basic_streambuf
126 {
127 public:
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:
138     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_; }
148
149     // 27.6.2.2.2 buffer and positioning:
150     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
155     pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
156                         ios_base::openmode __which = ios_base::in | ios_base::out)
157     { return seekoff(__off, __way, __which); }
158
159     inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
160     pos_type pubseekpos(pos_type __sp,
161                         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(); }
166
167     // Get and put areas:
168     // 27.6.2.2.3 Get area:
169     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); }
200
201     // 27.6.2.2.4 Putback:
202     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     }
215
216     // 27.6.2.2.5 Put area:
217     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); }
228
229 protected:
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_;}
239
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     }
249
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_;}
254
255     inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
256     void pbump(int __n) { __nout_ += __n; }
257
258     _LIBCPP_ALWAYS_INLINE
259     void __pbump(streamsize __n) { __nout_ += __n; }
260
261     inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
262     void setp(char_type* __pbeg, char_type* __pend) {
263         __bout_ = __nout_ = __pbeg;
264         __eout_ = __pend;
265     }
266
267     // 27.6.2.4 virtual functions:
268     // 27.6.2.4.1 Locales:
269     virtual void imbue(const locale& __loc);
270
271     // 27.6.2.4.2 Buffer management and positioning:
272     virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
273     virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
274                              ios_base::openmode __which = ios_base::in | ios_base::out);
275     virtual pos_type seekpos(pos_type __sp,
276                              ios_base::openmode __which = ios_base::in | ios_base::out);
277     virtual int sync();
278
279     // 27.6.2.4.3 Get area:
280     virtual streamsize showmanyc();
281     virtual streamsize xsgetn(char_type* __s, streamsize __n);
282     virtual int_type underflow();
283     virtual int_type uflow();
284
285     // 27.6.2.4.4 Putback:
286     virtual int_type pbackfail(int_type __c = traits_type::eof());
287
288     // 27.6.2.4.5 Put area:
289     virtual streamsize xsputn(const char_type* __s, streamsize __n);
290     virtual int_type overflow(int_type __c = traits_type::eof());
291
292 private:
293     locale __loc_;
294     char_type* __binp_;
295     char_type* __ninp_;
296     char_type* __einp_;
297     char_type* __bout_;
298     char_type* __nout_;
299     char_type* __eout_;
300 };
301
302 template <class _CharT, class _Traits>
303 basic_streambuf<_CharT, _Traits>::~basic_streambuf()
304 {
305 }
306
307 template <class _CharT, class _Traits>
308 basic_streambuf<_CharT, _Traits>::basic_streambuf()
309     : __binp_(0),
310       __ninp_(0),
311       __einp_(0),
312       __bout_(0),
313       __nout_(0),
314       __eout_(0)
315 {
316 }
317
318 template <class _CharT, class _Traits>
319 basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
320     : __loc_(__sb.__loc_),
321       __binp_(__sb.__binp_),
322       __ninp_(__sb.__ninp_),
323       __einp_(__sb.__einp_),
324       __bout_(__sb.__bout_),
325       __nout_(__sb.__nout_),
326       __eout_(__sb.__eout_)
327 {
328 }
329
330 template <class _CharT, class _Traits>
331 basic_streambuf<_CharT, _Traits>&
332 basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
333 {
334     __loc_ = __sb.__loc_;
335     __binp_ = __sb.__binp_;
336     __ninp_ = __sb.__ninp_;
337     __einp_ = __sb.__einp_;
338     __bout_ = __sb.__bout_;
339     __nout_ = __sb.__nout_;
340     __eout_ = __sb.__eout_;
341     return *this;
342 }
343
344 template <class _CharT, class _Traits>
345 void
346 basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
347 {
348     _VSTD::swap(__loc_, __sb.__loc_);
349     _VSTD::swap(__binp_, __sb.__binp_);
350     _VSTD::swap(__ninp_, __sb.__ninp_);
351     _VSTD::swap(__einp_, __sb.__einp_);
352     _VSTD::swap(__bout_, __sb.__bout_);
353     _VSTD::swap(__nout_, __sb.__nout_);
354     _VSTD::swap(__eout_, __sb.__eout_);
355 }
356
357 template <class _CharT, class _Traits>
358 void
359 basic_streambuf<_CharT, _Traits>::imbue(const locale&)
360 {
361 }
362
363 template <class _CharT, class _Traits>
364 basic_streambuf<_CharT, _Traits>*
365 basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
366 {
367     return this;
368 }
369
370 template <class _CharT, class _Traits>
371 typename basic_streambuf<_CharT, _Traits>::pos_type
372 basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
373                                           ios_base::openmode)
374 {
375     return pos_type(off_type(-1));
376 }
377
378 template <class _CharT, class _Traits>
379 typename basic_streambuf<_CharT, _Traits>::pos_type
380 basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
381 {
382     return pos_type(off_type(-1));
383 }
384
385 template <class _CharT, class _Traits>
386 int
387 basic_streambuf<_CharT, _Traits>::sync()
388 {
389     return 0;
390 }
391
392 template <class _CharT, class _Traits>
393 streamsize
394 basic_streambuf<_CharT, _Traits>::showmanyc()
395 {
396     return 0;
397 }
398
399 template <class _CharT, class _Traits>
400 streamsize
401 basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
402 {
403     const int_type __eof = traits_type::eof();
404     int_type __c;
405     streamsize __i = 0;
406     while(__i < __n)
407     {
408         if (__ninp_ < __einp_)
409         {
410             const streamsize __len = _VSTD::min(static_cast<streamsize>(INT_MAX),
411                                 _VSTD::min(__einp_ - __ninp_, __n - __i));
412             traits_type::copy(__s, __ninp_, __len);
413             __s +=  __len;
414             __i +=  __len;
415             this->gbump(__len);
416         }
417         else if ((__c = uflow()) != __eof)
418         {
419             *__s = traits_type::to_char_type(__c);
420             ++__s;
421             ++__i;
422         }
423         else
424             break;
425     }
426     return __i;
427 }
428
429 template <class _CharT, class _Traits>
430 typename basic_streambuf<_CharT, _Traits>::int_type
431 basic_streambuf<_CharT, _Traits>::underflow()
432 {
433     return traits_type::eof();
434 }
435
436 template <class _CharT, class _Traits>
437 typename basic_streambuf<_CharT, _Traits>::int_type
438 basic_streambuf<_CharT, _Traits>::uflow()
439 {
440     if (underflow() == traits_type::eof())
441         return traits_type::eof();
442     return traits_type::to_int_type(*__ninp_++);
443 }
444
445 template <class _CharT, class _Traits>
446 typename basic_streambuf<_CharT, _Traits>::int_type
447 basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
448 {
449     return traits_type::eof();
450 }
451
452 template <class _CharT, class _Traits>
453 streamsize
454 basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
455 {
456     streamsize __i = 0;
457     int_type __eof = traits_type::eof();
458     while( __i < __n)
459     {
460         if (__nout_ >= __eout_)
461         {
462             if (overflow(traits_type::to_int_type(*__s)) == __eof)
463                 break;
464             ++__s;
465             ++__i;
466         }
467         else
468         {
469             streamsize __chunk_size = _VSTD::min(__eout_ - __nout_, __n - __i);
470             traits_type::copy(__nout_, __s, __chunk_size);
471             __nout_ += __chunk_size;
472             __s     += __chunk_size;
473             __i     += __chunk_size;
474         }
475     }
476     return __i;
477 }
478
479 template <class _CharT, class _Traits>
480 typename basic_streambuf<_CharT, _Traits>::int_type
481 basic_streambuf<_CharT, _Traits>::overflow(int_type)
482 {
483     return traits_type::eof();
484 }
485
486 #ifndef _LIBCPP_AVAILABILITY_NO_STREAMS_EXTERN_TEMPLATE
487 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>)
488 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>)
489
490 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)
491 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<wchar_t>)
492 #endif
493
494 _LIBCPP_END_NAMESPACE_STD
495
496 _LIBCPP_POP_MACROS
497
498 #endif  // _LIBCPP_STEAMBUF