HexBinaryEncoder.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // HexBinaryEncoder.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/HexBinaryEncoder.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: HexBinary
  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/HexBinaryEncoder.h"
  16. namespace Poco {
  17. HexBinaryEncoderBuf::HexBinaryEncoderBuf(std::ostream& ostr):
  18. _pos(0),
  19. _lineLength(72),
  20. _uppercase(0),
  21. _buf(*ostr.rdbuf())
  22. {
  23. }
  24. HexBinaryEncoderBuf::~HexBinaryEncoderBuf()
  25. {
  26. try
  27. {
  28. close();
  29. }
  30. catch (...)
  31. {
  32. }
  33. }
  34. void HexBinaryEncoderBuf::setLineLength(int lineLength)
  35. {
  36. _lineLength = lineLength;
  37. }
  38. int HexBinaryEncoderBuf::getLineLength() const
  39. {
  40. return _lineLength;
  41. }
  42. void HexBinaryEncoderBuf::setUppercase(bool flag)
  43. {
  44. _uppercase = flag ? 16 : 0;
  45. }
  46. int HexBinaryEncoderBuf::writeToDevice(char c)
  47. {
  48. static const int eof = std::char_traits<char>::eof();
  49. static const char digits[] = "0123456789abcdef0123456789ABCDEF";
  50. if (_buf.sputc(digits[_uppercase + ((c >> 4) & 0xF)]) == eof) return eof;
  51. ++_pos;
  52. if (_buf.sputc(digits[_uppercase + (c & 0xF)]) == eof) return eof;
  53. if (++_pos >= _lineLength && _lineLength > 0)
  54. {
  55. if (_buf.sputc('\n') == eof) return eof;
  56. _pos = 0;
  57. }
  58. return charToInt(c);
  59. }
  60. int HexBinaryEncoderBuf::close()
  61. {
  62. sync();
  63. return _buf.pubsync();
  64. }
  65. HexBinaryEncoderIOS::HexBinaryEncoderIOS(std::ostream& ostr): _buf(ostr)
  66. {
  67. poco_ios_init(&_buf);
  68. }
  69. HexBinaryEncoderIOS::~HexBinaryEncoderIOS()
  70. {
  71. }
  72. int HexBinaryEncoderIOS::close()
  73. {
  74. return _buf.close();
  75. }
  76. HexBinaryEncoderBuf* HexBinaryEncoderIOS::rdbuf()
  77. {
  78. return &_buf;
  79. }
  80. HexBinaryEncoder::HexBinaryEncoder(std::ostream& ostr): HexBinaryEncoderIOS(ostr), std::ostream(&_buf)
  81. {
  82. }
  83. HexBinaryEncoder::~HexBinaryEncoder()
  84. {
  85. }
  86. } // namespace Poco