CountingStream.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // CountingStream.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/CountingStream.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: CountingStream
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/CountingStream.h"
  16. namespace Poco {
  17. CountingStreamBuf::CountingStreamBuf():
  18. _pIstr(0),
  19. _pOstr(0),
  20. _chars(0),
  21. _lines(0),
  22. _pos(0)
  23. {
  24. }
  25. CountingStreamBuf::CountingStreamBuf(std::istream& istr):
  26. _pIstr(&istr),
  27. _pOstr(0),
  28. _chars(0),
  29. _lines(0),
  30. _pos(0)
  31. {
  32. }
  33. CountingStreamBuf::CountingStreamBuf(std::ostream& ostr):
  34. _pIstr(0),
  35. _pOstr(&ostr),
  36. _chars(0),
  37. _lines(0),
  38. _pos(0)
  39. {
  40. }
  41. CountingStreamBuf::~CountingStreamBuf()
  42. {
  43. }
  44. int CountingStreamBuf::readFromDevice()
  45. {
  46. if (_pIstr)
  47. {
  48. int c = _pIstr->get();
  49. if (c != -1)
  50. {
  51. ++_chars;
  52. if (_pos++ == 0) ++_lines;
  53. if (c == '\n') _pos = 0;
  54. }
  55. return c;
  56. }
  57. return -1;
  58. }
  59. int CountingStreamBuf::writeToDevice(char c)
  60. {
  61. ++_chars;
  62. if (_pos++ == 0) ++_lines;
  63. if (c == '\n') _pos = 0;
  64. if (_pOstr) _pOstr->put(c);
  65. return charToInt(c);
  66. }
  67. void CountingStreamBuf::reset()
  68. {
  69. _chars = 0;
  70. _lines = 0;
  71. _pos = 0;
  72. }
  73. void CountingStreamBuf::setCurrentLineNumber(int line)
  74. {
  75. _lines = line;
  76. }
  77. void CountingStreamBuf::addChars(int chars)
  78. {
  79. _chars += chars;
  80. }
  81. void CountingStreamBuf::addLines(int lines)
  82. {
  83. _lines += lines;
  84. }
  85. void CountingStreamBuf::addPos(int pos)
  86. {
  87. _pos += pos;
  88. }
  89. CountingIOS::CountingIOS()
  90. {
  91. poco_ios_init(&_buf);
  92. }
  93. CountingIOS::CountingIOS(std::istream& istr): _buf(istr)
  94. {
  95. poco_ios_init(&_buf);
  96. }
  97. CountingIOS::CountingIOS(std::ostream& ostr): _buf(ostr)
  98. {
  99. poco_ios_init(&_buf);
  100. }
  101. CountingIOS::~CountingIOS()
  102. {
  103. }
  104. void CountingIOS::reset()
  105. {
  106. _buf.reset();
  107. }
  108. void CountingIOS::setCurrentLineNumber(int line)
  109. {
  110. _buf.setCurrentLineNumber(line);
  111. }
  112. void CountingIOS::addChars(int chars)
  113. {
  114. _buf.addChars(chars);
  115. }
  116. void CountingIOS::addLines(int lines)
  117. {
  118. _buf.addLines(lines);
  119. }
  120. void CountingIOS::addPos(int pos)
  121. {
  122. _buf.addPos(pos);
  123. }
  124. CountingStreamBuf* CountingIOS::rdbuf()
  125. {
  126. return &_buf;
  127. }
  128. CountingInputStream::CountingInputStream(std::istream& istr): CountingIOS(istr), std::istream(&_buf)
  129. {
  130. }
  131. CountingInputStream::~CountingInputStream()
  132. {
  133. }
  134. CountingOutputStream::CountingOutputStream(): std::ostream(&_buf)
  135. {
  136. }
  137. CountingOutputStream::CountingOutputStream(std::ostream& ostr): CountingIOS(ostr), std::ostream(&_buf)
  138. {
  139. }
  140. CountingOutputStream::~CountingOutputStream()
  141. {
  142. }
  143. } // namespace Poco