stdstream.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: stdstream.h
  3. // Purpose: interface of wxStdInputStream, wxStdInputStreamBuffer,
  4. // wxStdOutputStream, wxStdOutputStreamBuffer
  5. // Author: Jonathan Liu <net147@gmail.com>
  6. // Copyright: (c) 2009 Jonathan Liu
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. @class wxStdInputStreamBuffer
  11. wxStdInputStreamBuffer is a std::streambuf derived stream buffer which
  12. reads from a wxInputStream.
  13. Example:
  14. @code
  15. wxFFileInputStream file("input.txt.gz");
  16. wxZlibInputStream gzipInput(file, wxZLIB_GZIP);
  17. wxStdInputStreamBuffer gzipStreamBuffer(gzipInput);
  18. // redirect std::cin to read from compressed file
  19. std::streambuf* streamBufferOld = std::cin.rdbuf(&gzipStreamBuffer);
  20. // prompt for integer
  21. int number;
  22. std::cout << "Enter an integer: " << std::flush;
  23. std::cin >> number;
  24. std::cout << std::endl;
  25. std::cout << "You entered the integer " << number << "." << std::endl;
  26. // restore std::cin
  27. std::cin.rdbuf(streamBufferOld);
  28. @endcode
  29. @library{wxbase}
  30. @category{streams}
  31. @see wxInputStream, wxStdInputStream
  32. */
  33. class wxStdInputStreamBuffer : public std::streambuf
  34. {
  35. public:
  36. /**
  37. Creates a std::steambuf derived stream buffer which reads from a
  38. wxInputStream.
  39. @param stream
  40. Stream to read from.
  41. */
  42. wxStdInputStreamBuffer(wxInputStream& stream);
  43. /**
  44. Destructor.
  45. */
  46. virtual ~wxStdInputStreamBuffer() { }
  47. };
  48. /**
  49. @class wxStdInputStream
  50. wxStdInputStream is a std::istream derived stream which reads from
  51. a wxInputStream.
  52. Example:
  53. @code
  54. wxFFileInputStream file("words.txt");
  55. wxStdInputStream in(file);
  56. std::vector<std::string> words;
  57. // read words from words.txt
  58. std::copy(std::istream_iterator<std::string>(in),
  59. std::istream_iterator<std::string>(),
  60. std::back_inserter(words));
  61. // sort and remove duplicates
  62. std::sort(words.begin(), words.end());
  63. words.resize(std::unique(words.begin(), words.end()) - words.begin());
  64. // print words
  65. std::copy(words.begin(), words.end(),
  66. std::ostream_iterator<std::string>(std::cout, "\n"));
  67. @endcode
  68. @library{wxbase}
  69. @category{streams}
  70. @see wxInputStream, wxStdInputStreamBuffer
  71. */
  72. class wxStdInputStream : public std::istream
  73. {
  74. public:
  75. /**
  76. Creates a std::istream derived stream which reads from a
  77. wxInputStream.
  78. @param stream
  79. Stream to read from.
  80. */
  81. wxStdInputStream(wxInputStream& stream);
  82. /**
  83. Destructor.
  84. */
  85. virtual ~wxStdInputStream() { }
  86. };
  87. /**
  88. @class wxStdOutputStreamBuffer
  89. wxStdOutputStreamBuffer is a std::streambuf derived stream buffer which
  90. writes to a wxOutputStream.
  91. Example:
  92. @code
  93. wxFFileOutputStream file("cout.txt.gz");
  94. wxZlibOutputStream gzipOutput(file, -1, wxZLIB_GZIP);
  95. wxStdOutputStreamBuffer gzipStreamBuffer(gzipOutput);
  96. // redirect std::cout to cout.txt.gz using GZIP compression
  97. std::streambuf* streamBufferOld = std::cout.rdbuf(&gzipStreamBuffer);
  98. // write to std::cout
  99. std::cout << "Hello world!" << std::endl;
  100. // restore std::cout
  101. std::cout.rdbuf(streamBufferOld);
  102. @endcode
  103. @library{wxbase}
  104. @category{streams}
  105. @see wxOutputStream, wxStdOutputStream
  106. */
  107. class wxStdOutputStreamBuffer : public std::streambuf
  108. {
  109. public:
  110. /**
  111. Creates a std::steambuf derived stream buffer which writes to a
  112. wxOutputStream.
  113. @param stream
  114. Stream to write to.
  115. */
  116. wxStdOutputStreamBuffer(wxOutputStream& stream);
  117. /**
  118. Destructor.
  119. */
  120. virtual ~wxStdOutputStreamBuffer() { }
  121. };
  122. /**
  123. @class wxStdOutputStream
  124. wxStdOutputStream is a std::ostream derived stream which writes to a
  125. wxOutputStream.
  126. Example:
  127. @code
  128. wxFFileOutputStream file("out.txt.gz");
  129. wxZlibOutputStream gzipOutput(file, -1, wxZLIB_GZIP);
  130. wxStdOutputStream out(gzipOutput);
  131. out << "Hello world!" << std::endl;
  132. @endcode
  133. @library{wxbase}
  134. @category{streams}
  135. @see wxOutputStream, wxStdOutputStreamBuffer
  136. */
  137. class wxStdOutputStream : public std::ostream
  138. {
  139. public:
  140. /**
  141. Creates a std::ostream derived stream which writes to a
  142. wxOutputStream.
  143. @param stream
  144. Stream to write to.
  145. */
  146. wxStdOutputStream(wxOutputStream& stream);
  147. /**
  148. Destructor.
  149. */
  150. virtual ~wxStdOutputStream() { }
  151. };