|
|
|
| 1 |
|
// streambuf standard header |
| 2 |
|
#pragma once |
| 3 |
|
#ifndef _STREAMBUF_ |
| 4 |
|
#define _STREAMBUF_ |
| 5 |
|
#ifndef RC_INVOKED |
| 6 |
|
#include <xiosbase> |
| 7 |
|
|
| 8 |
|
#ifdef _MSC_VER |
| 9 |
|
#pragma pack(push,_CRT_PACKING) |
| 10 |
|
#pragma warning(push,3) |
| 11 |
|
#endif /* _MSC_VER */ |
| 12 |
|
_STD_BEGIN |
| 13 |
|
|
| 14 |
|
// TEMPLATE CLASS basic_streambuf |
| 15 |
|
template<class _Elem, |
| 16 |
|
class _Traits> |
| 17 |
|
class basic_streambuf |
| 18 |
|
{ // control read/write buffers |
| 19 |
|
|
| 20 |
|
basic_streambuf(const basic_streambuf<_Elem, _Traits>&); // not defined |
| 21 |
|
basic_streambuf<_Elem, _Traits>& |
| 22 |
|
operator=(const basic_streambuf<_Elem, _Traits>&); // not defined |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
protected: |
| 26 |
|
basic_streambuf() |
| 27 |
|
: _Plocale(_NEW_CRT(locale)) |
| 28 |
|
{ // construct with no buffers |
| 29 |
|
_Init(); |
| 30 |
|
} |
| 31 |
|
|
| 32 |
|
basic_streambuf(_Uninitialized) |
| 33 |
|
{ // construct uninitialized |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
public: |
| 37 |
|
typedef basic_streambuf<_Elem, _Traits> _Myt; |
| 38 |
|
typedef _Elem char_type; |
| 39 |
|
typedef _Traits traits_type; |
| 40 |
|
|
| 41 |
|
virtual __CLR_OR_THIS_CALL ~basic_streambuf() |
| 42 |
|
{ // destroy the object |
| 43 |
|
_DELETE_CRT(_Plocale); |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
typedef typename _Traits::int_type int_type; |
| 47 |
|
typedef typename _Traits::pos_type pos_type; |
| 48 |
|
typedef typename _Traits::off_type off_type; |
| 49 |
|
|
| 50 |
|
pos_type pubseekoff(off_type _Off, ios_base::seekdir _Way, |
| 51 |
|
ios_base::openmode _Mode = ios_base::in | ios_base::out) |
| 52 |
|
{ // change position by _Off, according to _Way, _Mode |
| 53 |
|
return (seekoff(_Off, _Way, _Mode)); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
pos_type pubseekoff(off_type _Off, ios_base::seek_dir _Way, |
| 57 |
|
ios_base::open_mode _Mode) |
| 58 |
|
{ // change position by _Off, according to _Way, _Mode (old style) |
| 384 |
|
--_Count; |
| 385 |
|
} |
| 386 |
|
|
| 387 |
|
return (_Copied); |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
virtual pos_type __CLR_OR_THIS_CALL seekoff(off_type, ios_base::seekdir, |
| 391 |
|
ios_base::openmode = ios_base::in | ios_base::out) |
| 392 |
|
{ // change position by offset, according to way and mode |
| 393 |
|
return (streampos(_BADOFF)); |
| 394 |
|
} |
| 395 |
|
|
| 396 |
|
virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type, |
| 397 |
|
ios_base::openmode = ios_base::in | ios_base::out) |
| 398 |
|
{ // change to specified position, according to mode |
| 399 |
|
return (streampos(_BADOFF)); |
| 400 |
|
} |
| 401 |
|
|
| 402 |
|
virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize) |
| 403 |
|
{ // offer buffer to external agent (do nothing) |
| 404 |
|
return (this); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
|
virtual int __CLR_OR_THIS_CALL sync() |
| 408 |
|
{ // synchronize with external agent (do nothing) |
| 409 |
|
return (0); |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
virtual void __CLR_OR_THIS_CALL imbue(const locale&) |
| 413 |
|
{ // set locale to argument (do nothing) |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
private: |
| 417 |
|
_Mutex _Mylock; // thread lock |
| 418 |
|
_Elem *_Gfirst; // beginning of read buffer |
| 419 |
|
_Elem *_Pfirst; // beginning of write buffer |
| 420 |
|
_Elem **_IGfirst; // pointer to beginning of read buffer |
| 421 |
|
_Elem **_IPfirst; // pointer to beginning of write buffer |
| 422 |
|
_Elem *_Gnext; // current position in read buffer |
| 423 |
|
_Elem *_Pnext; // current position in write buffer |
| 424 |
|
_Elem **_IGnext; // pointer to current position in read buffer |
| 425 |
|
_Elem **_IPnext; // pointer to current position in write buffer |
| 426 |
|
int _Gcount; // length of read buffer |
| 427 |
|
int _Pcount; // length of write buffer |
| 428 |
|
int *_IGcount; // pointer to length of read buffer |
| 429 |
|
int *_IPcount; // pointer to length of write buffer |
| 430 |
|
locale *_Plocale; // pointer to imbued locale object |
| 431 |
|
}; |
| 432 |
|
|
| 433 |
|
#if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) |
| 434 |
|
|
| 435 |
|
template class _CRTIMP2 basic_streambuf<char, char_traits<char> >; |
| 436 |
|
template class _CRTIMP2 basic_streambuf<wchar_t, char_traits<wchar_t> >; |
| 437 |
|
|
| 438 |
|
|
| 439 |
|
|
| 440 |
|
#endif /* _DLL_CPPLIB */ |
| 441 |
|
|
| 442 |
|
// TEMPLATE CLASS istreambuf_iterator |
| 443 |
|
template<class _Elem, |
| 444 |
|
class _Traits> |
| 445 |
|
class istreambuf_iterator |
| 446 |
|
: public iterator<input_iterator_tag, |
| 447 |
|
_Elem, typename _Traits::off_type, _Elem *, _Elem&> |
| 448 |
|
{ // wrap stream buffer as input iterator |
| 449 |
|
typedef istreambuf_iterator<_Elem, _Traits> _Myt; |
| 450 |
|
public: |
| 451 |
|
typedef _Elem char_type; |
| 452 |
|
typedef _Traits traits_type; |
| 453 |
|
typedef basic_streambuf<_Elem, _Traits> streambuf_type; |
| 454 |
|
typedef basic_istream<_Elem, _Traits> istream_type; |
| 455 |
|
|
| 456 |
|
typedef typename traits_type::int_type int_type; |
| 457 |
|
|
| 458 |
|
#if _SECURE_SCL |
| 459 |
|
typedef _Range_checked_iterator_tag _Checked_iterator_category; |
| 460 |
|
#endif |
| 461 |
|
|
| 462 |
|
istreambuf_iterator(streambuf_type *_Sb = 0) _THROW0() |
| 463 |
|
: _Strbuf(_Sb), _Got(_Sb == 0) |
| 464 |
|
{ // construct from stream buffer _Sb |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
istreambuf_iterator(istream_type& _Istr) _THROW0() |
| 468 |
|
: _Strbuf(_Istr.rdbuf()), _Got(_Istr.rdbuf() == 0) |
| 469 |
|
{ // construct from stream buffer in istream _Istr |
| 470 |
|
} |
| 471 |
|
|
| 472 |
|
_Elem operator*() const |
| 473 |
|
{ // return designated value |
| 474 |
|
if (!_Got) |
| 475 |
|
((_Myt *)this)->_Peek(); |
| 476 |
|
|
| 477 |
|
#if _HAS_ITERATOR_DEBUGGING |
| 478 |
|
if (_Strbuf == 0) |
| 479 |
|
_DEBUG_ERROR("istreambuf_iterator is not dereferencable"); |
| 480 |
|
#endif /* _HAS_ITERATOR_DEBUGGING */ |
| 481 |
|
|
| 482 |
|
return (_Val); |
| 483 |
|
} |
| 484 |
|
|
| 485 |
|
_Myt& operator++() |
| 486 |
|
{ // preincrement |
| 487 |
|
|
| 488 |
|
#if _HAS_ITERATOR_DEBUGGING |
| 489 |
|
if (_Strbuf == 0) |
| 490 |
|
_DEBUG_ERROR("istreambuf_iterator is not incrementable"); |
| 491 |
|
#endif /* _HAS_ITERATOR_DEBUGGING */ |
| 492 |
|
|
| 493 |
|
_Inc(); |
| 494 |
|
return (*this); |
| 495 |
|
} |
| 496 |
|
|
| 497 |
|
_Myt operator++(int) |
| 498 |
|
{ // postincrement |
| 499 |
|
if (!_Got) |
| 500 |
|
_Peek(); |
| 501 |
|
_Myt _Tmp = *this; |
| 502 |
|
++*this; |
| 503 |
|
return (_Tmp); |
| 504 |
|
} |
| 505 |
|
|
| 506 |
|
bool equal(const _Myt& _Right) const |
| 507 |
|
{ // test for equality |
| 508 |
|
if (!_Got) |
| 509 |
|
((_Myt *)this)->_Peek(); |
| 510 |
|
if (!_Right._Got) |
| 511 |
|
((_Myt *)&_Right)->_Peek(); |
| 512 |
|
return (_Strbuf == 0 && _Right._Strbuf == 0 |
| 513 |
|
|| _Strbuf != 0 && _Right._Strbuf != 0); |
| 514 |
|
} |
| 515 |
|
|
| 516 |
|
private: |
| 517 |
|
void _Inc() |
| 518 |
|
{ // skip to next input element |
| 519 |
|
if (_Strbuf == 0 |
| 520 |
|
|| traits_type::eq_int_type(traits_type::eof(), |
| 521 |
|
_Strbuf->sbumpc())) |
| 522 |
|
_Strbuf = 0, _Got = true; |
| 523 |
|
else |
| 524 |
|
_Got = false; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
|
_Elem _Peek() |
| 528 |
|
{ // peek at next input element |
| 529 |
|
int_type _Meta; |
| 530 |
|
if (_Strbuf == 0 |
| 531 |
|
|| traits_type::eq_int_type(traits_type::eof(), |
| 532 |
|
_Meta = _Strbuf->sgetc())) |
| 533 |
|
_Strbuf = 0; |
| 534 |
|
else |
| 535 |
|
_Val = traits_type::to_char_type(_Meta); |
| 536 |
|
_Got = true; |
| 537 |
|
return (_Val); |
| 538 |
|
} |
| 539 |
|
|
| 540 |
|
streambuf_type *_Strbuf; // the wrapped stream buffer |
| 541 |
|
bool _Got; // true if _Val is valid |
| 542 |
|
_Elem _Val; // next element to deliver |
| 543 |
|
}; |
| 544 |
|
|
| 545 |
|
// istreambuf_iterator TEMPLATE OPERATORS |
| 546 |
|
template<class _Elem, |
| 547 |
|
class _Traits> inline |
| 548 |
|
bool __CLR_OR_THIS_CALL operator==( |
| 549 |
|
const istreambuf_iterator<_Elem, _Traits>& _Left, |
| 550 |
|
const istreambuf_iterator<_Elem, _Traits>& _Right) |
| 551 |
|
{ // test for istreambuf_iterator equality |
| 552 |
|
return (_Left.equal(_Right)); |
| 553 |
|
} |
| 554 |
|
|
| 555 |
|
template<class _Elem, |
| 556 |
|
class _Traits> inline |
| 557 |
|
bool __CLR_OR_THIS_CALL operator!=( |
| 558 |
|
const istreambuf_iterator<_Elem, _Traits>& _Left, |
| 559 |
|
const istreambuf_iterator<_Elem, _Traits>& _Right) |
| 560 |
|
{ // test for istreambuf_iterator inequality |
| 561 |
|
return (!(_Left == _Right)); |
| 562 |
|
} |
| 563 |
|
|
| 564 |
|
// TEMPLATE CLASS ostreambuf_iterator |
| 565 |
|
template<class _Elem, |
| 566 |
|
class _Traits> |
| 567 |
|
class ostreambuf_iterator |
| 568 |
|
: public _Outit |
| 569 |
|
{ // wrap stream buffer as output iterator |
| 570 |
|
typedef ostreambuf_iterator<_Elem, _Traits> _Myt; |
| 571 |
|
public: |
| 572 |
|
typedef _Elem char_type; |
| 573 |
|
typedef _Traits traits_type; |
| 574 |
|
typedef basic_streambuf<_Elem, _Traits> streambuf_type; |
| 575 |
|
typedef basic_ostream<_Elem, _Traits> ostream_type; |
| 576 |
|
|
| 577 |
|
#if _SECURE_SCL |
| 578 |
|
typedef _Range_checked_iterator_tag _Checked_iterator_category; |
| 579 |
|
#endif |
| 580 |
|
|
| 581 |
|
ostreambuf_iterator(streambuf_type *_Sb) _THROW0() |
| 582 |
|
: _Failed(false), _Strbuf(_Sb) |
| 583 |
|
{ // construct from stream buffer _Sb |
| 584 |
|
} |
| 585 |
|
|
| 586 |
|
ostreambuf_iterator(ostream_type& _Ostr) _THROW0() |
| 587 |
|
: _Failed(false), _Strbuf(_Ostr.rdbuf()) |
| 588 |
|
{ // construct from stream buffer in _Ostr |
| 589 |
|
} |
| 590 |
|
|
| 591 |
|
_Myt& operator=(_Elem _Right) |
| 592 |
|
{ // store element and increment |
| 593 |
|
if (_Strbuf == 0 |
| 594 |
|
|| traits_type::eq_int_type(_Traits::eof(), |
| 595 |
|
_Strbuf->sputc(_Right))) |
| 596 |
|
_Failed = true; |
| 597 |
|
return (*this); |
| 598 |
|
} |
| 599 |
|
|
| 600 |
|
_Myt& operator*() |
| 601 |
|
{ // pretend to get designated element |
| 602 |
|
return (*this); |
| 603 |
|
} |
| 604 |
|
|
| 605 |
|
_Myt& operator++() |
| 606 |
|
{ // pretend to preincrement |
| 607 |
|
return (*this); |
| 608 |
|
} |
| 609 |
|
|
| 610 |
|
_Myt& operator++(int) |
| 611 |
|
{ // pretend to postincrement |
| 612 |
|
return (*this); |
| 613 |
|
} |
| 614 |
|
|
| 615 |
|
bool failed() const _THROW0() |
| 616 |
|
{ // return true if any stores failed |
| 617 |
|
return (_Failed); |
| 618 |
|
} |
| 619 |
|
|
| 620 |
|
private: |
| 621 |
|
bool _Failed; // true if any stores have failed |
| 622 |
|
streambuf_type *_Strbuf; // the wrapped stream buffer |
| 623 |
|
}; |
| 624 |
|
_STD_END |
| 625 |
|
#ifdef _MSC_VER |
| 626 |
|
#pragma warning(pop) |
| 627 |
|
#pragma pack(pop) |
| 628 |
|
#endif /* _MSC_VER */ |
| 629 |
|
|
| 630 |
|
#endif /* RC_INVOKED */ |
| 631 |
|
#endif /* _STREAMBUF_ */ |
| 632 |
|
|
| 633 |
|
/* |
| 634 |
|
* Copyright (c) 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED. |
| 635 |
|
* Consult your license regarding permissions and restrictions. |
| 636 |
|
V5.03:0009 */ |
| 637 |
|
|
|
|
|