CountingStream.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // CountingStream.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/CountingStream.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: CountingStream
  9. //
  10. // Definition of the CountingStreamBuf, CountingInputStream and CountingOutputStream classes.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_CountingStream_INCLUDED
  18. #define Foundation_CountingStream_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/UnbufferedStreamBuf.h"
  21. #include <istream>
  22. #include <ostream>
  23. namespace Poco {
  24. class Foundation_API CountingStreamBuf: public UnbufferedStreamBuf
  25. /// This stream buffer counts all characters and lines
  26. /// going through it.
  27. {
  28. public:
  29. CountingStreamBuf();
  30. /// Creates an unconnected CountingStreamBuf.
  31. CountingStreamBuf(std::istream& istr);
  32. /// Creates the CountingStreamBuf and connects it
  33. /// to the given input stream.
  34. CountingStreamBuf(std::ostream& ostr);
  35. /// Creates the CountingStreamBuf and connects it
  36. /// to the given output stream.
  37. ~CountingStreamBuf();
  38. /// Destroys the CountingStream.
  39. int chars() const;
  40. /// Returns the total number of characters.
  41. int lines() const;
  42. /// Returns the total number of lines.
  43. int pos() const;
  44. /// Returns the number of characters on the current line.
  45. void reset();
  46. /// Resets all counters.
  47. void setCurrentLineNumber(int line);
  48. /// Sets the current line number.
  49. ///
  50. /// This is mainly useful when parsing C/C++
  51. /// preprocessed source code containing #line directives.
  52. int getCurrentLineNumber() const;
  53. /// Returns the current line number (same as lines()).
  54. void addChars(int chars);
  55. /// Add to the total number of characters.
  56. void addLines(int lines);
  57. /// Add to the total number of lines.
  58. void addPos(int pos);
  59. /// Add to the number of characters on the current line.
  60. protected:
  61. int readFromDevice();
  62. int writeToDevice(char c);
  63. private:
  64. std::istream* _pIstr;
  65. std::ostream* _pOstr;
  66. int _chars;
  67. int _lines;
  68. int _pos;
  69. };
  70. class Foundation_API CountingIOS: public virtual std::ios
  71. /// The base class for CountingInputStream and CountingOutputStream.
  72. ///
  73. /// This class is needed to ensure the correct initialization
  74. /// order of the stream buffer and base classes.
  75. {
  76. public:
  77. CountingIOS();
  78. /// Creates the basic stream and leaves it unconnected.
  79. CountingIOS(std::istream& istr);
  80. /// Creates the basic stream and connects it
  81. /// to the given input stream.
  82. CountingIOS(std::ostream& ostr);
  83. /// Creates the basic stream and connects it
  84. /// to the given output stream.
  85. ~CountingIOS();
  86. /// Destroys the stream.
  87. int chars() const;
  88. /// Returns the total number of characters.
  89. int lines() const;
  90. /// Returns the total number of lines.
  91. int pos() const;
  92. /// Returns the number of characters on the current line.
  93. void reset();
  94. /// Resets all counters.
  95. void setCurrentLineNumber(int line);
  96. /// Sets the current line number.
  97. ///
  98. /// This is mainly useful when parsing C/C++
  99. /// preprocessed source code containing #line directives.
  100. int getCurrentLineNumber() const;
  101. /// Returns the current line number (same as lines()).
  102. void addChars(int chars);
  103. /// Add to the total number of characters.
  104. void addLines(int lines);
  105. /// Add to the total number of lines.
  106. void addPos(int pos);
  107. /// Add to the number of characters on the current line.
  108. CountingStreamBuf* rdbuf();
  109. /// Returns a pointer to the underlying streambuf.
  110. protected:
  111. CountingStreamBuf _buf;
  112. };
  113. class Foundation_API CountingInputStream: public CountingIOS, public std::istream
  114. /// This stream counts all characters and lines
  115. /// going through it. This is useful for lexers and parsers
  116. /// that need to determine the current position in the stream.
  117. {
  118. public:
  119. CountingInputStream(std::istream& istr);
  120. /// Creates the CountingInputStream and connects it
  121. /// to the given input stream.
  122. ~CountingInputStream();
  123. /// Destroys the stream.
  124. };
  125. class Foundation_API CountingOutputStream: public CountingIOS, public std::ostream
  126. /// This stream counts all characters and lines
  127. /// going through it.
  128. {
  129. public:
  130. CountingOutputStream();
  131. /// Creates an unconnected CountingOutputStream.
  132. CountingOutputStream(std::ostream& ostr);
  133. /// Creates the CountingOutputStream and connects it
  134. /// to the given output stream.
  135. ~CountingOutputStream();
  136. /// Destroys the CountingOutputStream.
  137. };
  138. //
  139. // inlines
  140. //
  141. inline int CountingStreamBuf::chars() const
  142. {
  143. return _chars;
  144. }
  145. inline int CountingStreamBuf::lines() const
  146. {
  147. return _lines;
  148. }
  149. inline int CountingStreamBuf::pos() const
  150. {
  151. return _pos;
  152. }
  153. inline int CountingStreamBuf::getCurrentLineNumber() const
  154. {
  155. return _lines;
  156. }
  157. inline int CountingIOS::chars() const
  158. {
  159. return _buf.chars();
  160. }
  161. inline int CountingIOS::lines() const
  162. {
  163. return _buf.lines();
  164. }
  165. inline int CountingIOS::pos() const
  166. {
  167. return _buf.pos();
  168. }
  169. inline int CountingIOS::getCurrentLineNumber() const
  170. {
  171. return _buf.getCurrentLineNumber();
  172. }
  173. } // namespace Poco
  174. #endif // Foundation_CountingStream_INCLUDED