LineEndingConverter.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // LineEndingConverter.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/LineEndingConverter.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: LineEndingConverter
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/LineEndingConverter.h"
  16. namespace Poco {
  17. const std::string LineEnding::NEWLINE_DEFAULT(POCO_DEFAULT_NEWLINE_CHARS);
  18. const std::string LineEnding::NEWLINE_CR("\r");
  19. const std::string LineEnding::NEWLINE_CRLF("\r\n");
  20. const std::string LineEnding::NEWLINE_LF("\n");
  21. LineEndingConverterStreamBuf::LineEndingConverterStreamBuf(std::istream& istr):
  22. _pIstr(&istr),
  23. _pOstr(0),
  24. _newLine(LineEnding::NEWLINE_DEFAULT),
  25. _lastChar(0)
  26. {
  27. _it = _newLine.end();
  28. }
  29. LineEndingConverterStreamBuf::LineEndingConverterStreamBuf(std::ostream& ostr):
  30. _pIstr(0),
  31. _pOstr(&ostr),
  32. _newLine(LineEnding::NEWLINE_DEFAULT),
  33. _lastChar(0)
  34. {
  35. _it = _newLine.end();
  36. }
  37. LineEndingConverterStreamBuf::~LineEndingConverterStreamBuf()
  38. {
  39. }
  40. void LineEndingConverterStreamBuf::setNewLine(const std::string& newLineCharacters)
  41. {
  42. _newLine = newLineCharacters;
  43. _it = _newLine.end();
  44. }
  45. const std::string& LineEndingConverterStreamBuf::getNewLine() const
  46. {
  47. return _newLine;
  48. }
  49. int LineEndingConverterStreamBuf::readFromDevice()
  50. {
  51. poco_assert_dbg (_pIstr);
  52. while (_it == _newLine.end())
  53. {
  54. int c = _pIstr->get();
  55. if (c == '\r')
  56. {
  57. if (_pIstr->peek() == '\n') _pIstr->get();
  58. _it = _newLine.begin();
  59. }
  60. else if (c == '\n')
  61. {
  62. _it = _newLine.begin();
  63. }
  64. else return c;
  65. }
  66. return *_it++;
  67. }
  68. int LineEndingConverterStreamBuf::writeToDevice(char c)
  69. {
  70. poco_assert_dbg (_pOstr);
  71. if (c == '\r' || (c == '\n' && _lastChar != '\r'))
  72. _pOstr->write(_newLine.data(), (std::streamsize) _newLine.length());
  73. if (c != '\n' && c != '\r')
  74. _pOstr->put(c);
  75. _lastChar = c;
  76. return charToInt(c);
  77. }
  78. LineEndingConverterIOS::LineEndingConverterIOS(std::istream& istr): _buf(istr)
  79. {
  80. poco_ios_init(&_buf);
  81. }
  82. LineEndingConverterIOS::LineEndingConverterIOS(std::ostream& ostr): _buf(ostr)
  83. {
  84. poco_ios_init(&_buf);
  85. }
  86. LineEndingConverterIOS::~LineEndingConverterIOS()
  87. {
  88. }
  89. void LineEndingConverterIOS::setNewLine(const std::string& newLineCharacters)
  90. {
  91. _buf.setNewLine(newLineCharacters);
  92. }
  93. const std::string& LineEndingConverterIOS::getNewLine() const
  94. {
  95. return _buf.getNewLine();
  96. }
  97. LineEndingConverterStreamBuf* LineEndingConverterIOS::rdbuf()
  98. {
  99. return &_buf;
  100. }
  101. InputLineEndingConverter::InputLineEndingConverter(std::istream& istr):
  102. LineEndingConverterIOS(istr),
  103. std::istream(&_buf)
  104. {
  105. }
  106. InputLineEndingConverter::InputLineEndingConverter(std::istream& istr, const std::string& newLineCharacters):
  107. LineEndingConverterIOS(istr),
  108. std::istream(&_buf)
  109. {
  110. setNewLine(newLineCharacters);
  111. }
  112. InputLineEndingConverter::~InputLineEndingConverter()
  113. {
  114. }
  115. OutputLineEndingConverter::OutputLineEndingConverter(std::ostream& ostr):
  116. LineEndingConverterIOS(ostr),
  117. std::ostream(&_buf)
  118. {
  119. }
  120. OutputLineEndingConverter::OutputLineEndingConverter(std::ostream& ostr, const std::string& newLineCharacters):
  121. LineEndingConverterIOS(ostr),
  122. std::ostream(&_buf)
  123. {
  124. setNewLine(newLineCharacters);
  125. }
  126. OutputLineEndingConverter::~OutputLineEndingConverter()
  127. {
  128. }
  129. } // namespace Poco