_strstream.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_INTERNAL_STRSTREAM
  19. #define _STLP_INTERNAL_STRSTREAM
  20. #ifndef _STLP_INTERNAL_STREAMBUF
  21. # include <stl/_streambuf.h>
  22. #endif
  23. #ifndef _STLP_INTERNAL_ISTREAM
  24. # include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
  25. #endif
  26. #ifndef _STLP_INTERNAL_STRING_H
  27. # include <stl/_string.h>
  28. #endif
  29. _STLP_BEGIN_NAMESPACE
  30. #ifndef _STLP_USE_NAMESPACES
  31. # define strstream _STLP_strstream
  32. # define ostrstream _STLP_ostrstream
  33. # define istrstream _STLP_istrstream
  34. # define strstreambuf _STLP_strstreambuf
  35. #endif
  36. //----------------------------------------------------------------------
  37. // Class strstreambuf, a streambuf class that manages an array of char.
  38. // Note that this class is not a template.
  39. class _STLP_CLASS_DECLSPEC strstreambuf : public basic_streambuf<char, char_traits<char> > {
  40. public: // Types.
  41. typedef char_traits<char> _Traits;
  42. typedef basic_streambuf<char, char_traits<char> > _Base;
  43. typedef void* (*__alloc_fn)(size_t);
  44. typedef void (*__free_fn)(void*);
  45. public: // Constructor, destructor
  46. explicit strstreambuf(streamsize _Initial_capacity = 0);
  47. strstreambuf(__alloc_fn, __free_fn);
  48. strstreambuf(char* __get, streamsize __n, char* __put = 0);
  49. strstreambuf(signed char* __get, streamsize __n, signed char* __put = 0);
  50. strstreambuf(unsigned char* __get, streamsize __n, unsigned char* __put=0);
  51. strstreambuf(const char* __get, streamsize __n);
  52. strstreambuf(const signed char* __get, streamsize __n);
  53. strstreambuf(const unsigned char* __get, streamsize __n);
  54. virtual ~strstreambuf();
  55. public: // strstreambuf operations.
  56. void freeze(bool = true);
  57. char* str();
  58. int pcount() const;
  59. protected: // Overridden virtual member functions.
  60. virtual int_type overflow(int_type __c = _Traits::eof());
  61. virtual int_type pbackfail(int_type __c = _Traits::eof());
  62. virtual int_type underflow();
  63. virtual _Base* setbuf(char* __buf, streamsize __n);
  64. virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
  65. ios_base::openmode __mode
  66. = ios_base::in | ios_base::out);
  67. virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
  68. = ios_base::in | ios_base::out);
  69. private: // Helper functions.
  70. // Dynamic allocation, possibly using _M_alloc_fun and _M_free_fun.
  71. char* _M_alloc(size_t);
  72. void _M_free(char*);
  73. // Helper function used in constructors.
  74. void _M_setup(char* __get, char* __put, streamsize __n);
  75. private: // Data members.
  76. __alloc_fn _M_alloc_fun;
  77. __free_fn _M_free_fun;
  78. bool _M_dynamic : 1;
  79. bool _M_frozen : 1;
  80. bool _M_constant : 1;
  81. };
  82. //----------------------------------------------------------------------
  83. // Class istrstream, an istream that manages a strstreambuf.
  84. class _STLP_CLASS_DECLSPEC istrstream : public basic_istream<char, char_traits<char> > {
  85. public:
  86. explicit istrstream(char*);
  87. explicit istrstream(const char*);
  88. istrstream(char* , streamsize);
  89. istrstream(const char*, streamsize);
  90. virtual ~istrstream();
  91. strstreambuf* rdbuf() const;
  92. char* str();
  93. private:
  94. strstreambuf _M_buf;
  95. };
  96. //----------------------------------------------------------------------
  97. // Class ostrstream
  98. class _STLP_CLASS_DECLSPEC ostrstream : public basic_ostream<char, char_traits<char> >
  99. {
  100. public:
  101. ostrstream();
  102. ostrstream(char*, int, ios_base::openmode = ios_base::out);
  103. virtual ~ostrstream();
  104. strstreambuf* rdbuf() const;
  105. void freeze(bool = true);
  106. char* str();
  107. int pcount() const;
  108. private:
  109. strstreambuf _M_buf;
  110. };
  111. //----------------------------------------------------------------------
  112. // Class strstream
  113. class _STLP_CLASS_DECLSPEC strstream : public basic_iostream<char, char_traits<char> > {
  114. public:
  115. typedef char char_type;
  116. typedef char_traits<char>::int_type int_type;
  117. typedef char_traits<char>::pos_type pos_type;
  118. typedef char_traits<char>::off_type off_type;
  119. strstream();
  120. strstream(char*, int, ios_base::openmode = ios_base::in | ios_base::out);
  121. virtual ~strstream();
  122. strstreambuf* rdbuf() const;
  123. void freeze(bool = true);
  124. int pcount() const;
  125. char* str();
  126. private:
  127. strstreambuf _M_buf;
  128. //explicitely defined as private to avoid warnings:
  129. strstream(strstream const&);
  130. strstream& operator = (strstream const&);
  131. };
  132. _STLP_END_NAMESPACE
  133. #endif /* _STLP_INTERNAL_STRSTREAM */