boost_no_std_wstreambuf.ipp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // (C) Copyright John Maddock 2001.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/config for most recent version.
  6. // MACRO: BOOST_NO_STD_WSTREAMBUF
  7. // TITLE: std::basic_streambuf<wchar_t>
  8. // DESCRIPTION: The standard library lacks std::basic_streambuf<wchar_t>.
  9. #include <iostream>
  10. #include <streambuf>
  11. #include <string>
  12. namespace boost_no_std_wstreambuf{
  13. template <class charT,
  14. class traits = ::std::char_traits<charT> >
  15. class parser_buf : public ::std::basic_streambuf<charT, traits>
  16. {
  17. typedef ::std::basic_streambuf<charT, traits> base_type;
  18. typedef typename base_type::int_type int_type;
  19. typedef typename base_type::char_type char_type;
  20. typedef typename base_type::pos_type pos_type;
  21. typedef ::std::streamsize streamsize;
  22. typedef typename base_type::off_type off_type;
  23. public:
  24. parser_buf() : base_type() { setbuf(0, 0); }
  25. const charT* getnext() { return this->gptr(); }
  26. protected:
  27. std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
  28. typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
  29. typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
  30. private:
  31. parser_buf& operator=(const parser_buf&)
  32. { return *this; };
  33. parser_buf(const parser_buf&);
  34. };
  35. template<class charT, class traits>
  36. std::basic_streambuf<charT, traits>*
  37. parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
  38. {
  39. this->setg(s, s, s + n);
  40. return this;
  41. }
  42. template<class charT, class traits>
  43. typename parser_buf<charT, traits>::pos_type
  44. parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
  45. {
  46. typedef typename parser_buf<charT, traits>::pos_type pos_type;
  47. if(which & ::std::ios_base::out)
  48. return pos_type(off_type(-1));
  49. int size = this->egptr() - this->eback();
  50. int pos = this->gptr() - this->eback();
  51. charT* g = this->eback();
  52. switch((int)way)
  53. {
  54. case ::std::ios_base::beg:
  55. if((off < 0) || (off > size))
  56. return pos_type(off_type(-1));
  57. else
  58. this->setg(g, g + off, g + size);
  59. case ::std::ios_base::end:
  60. if((off < 0) || (off > size))
  61. return pos_type(off_type(-1));
  62. else
  63. this->setg(g, g + size - off, g + size);
  64. case ::std::ios_base::cur:
  65. {
  66. int newpos = pos + off;
  67. if((newpos < 0) || (newpos > size))
  68. return pos_type(off_type(-1));
  69. else
  70. this->setg(g, g + newpos, g + size);
  71. }
  72. }
  73. return static_cast<pos_type>(this->gptr() - this->eback());
  74. }
  75. template<class charT, class traits>
  76. typename parser_buf<charT, traits>::pos_type
  77. parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
  78. {
  79. if(which & ::std::ios_base::out)
  80. return pos_type(off_type(-1));
  81. int size = this->egptr() - this->eback();
  82. charT* g = this->eback();
  83. if(off_type(sp) <= size)
  84. {
  85. this->setg(g, g + off_type(sp), g + size);
  86. }
  87. return pos_type(off_type(-1));
  88. }
  89. int test()
  90. {
  91. return 0;
  92. }
  93. template class parser_buf<char>;
  94. template class parser_buf<wchar_t>;
  95. }