HexBinaryEncoder.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // HexBinaryEncoder.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/HexBinaryEncoder.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: HexBinary
  9. //
  10. // Definition of the HexBinaryEncoder class.
  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_HexBinaryEncoder_INCLUDED
  18. #define Foundation_HexBinaryEncoder_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/UnbufferedStreamBuf.h"
  21. #include <ostream>
  22. namespace Poco {
  23. class Foundation_API HexBinaryEncoderBuf: public UnbufferedStreamBuf
  24. /// This streambuf encodes all data written
  25. /// to it in hexBinary encoding and forwards it to a connected
  26. /// ostream.
  27. /// In hexBinary encoding, each binary octet is encoded as a character tuple,
  28. /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code.
  29. /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/),
  30. /// section 3.2.15.
  31. ///
  32. /// Note: The characters are directly written
  33. /// to the ostream's streambuf, thus bypassing
  34. /// the ostream. The ostream's state is therefore
  35. /// not updated to match the buffer's state.
  36. {
  37. public:
  38. HexBinaryEncoderBuf(std::ostream& ostr);
  39. ~HexBinaryEncoderBuf();
  40. int close();
  41. /// Closes the stream buffer.
  42. void setLineLength(int lineLength);
  43. /// Specify the line length.
  44. ///
  45. /// After the given number of characters have been written,
  46. /// a newline character will be written.
  47. ///
  48. /// Specify 0 for an unlimited line length.
  49. int getLineLength() const;
  50. /// Returns the currently set line length.
  51. void setUppercase(bool flag = true);
  52. /// Specify whether hex digits a-f are written in upper or lower case.
  53. private:
  54. int writeToDevice(char c);
  55. int _pos;
  56. int _lineLength;
  57. int _uppercase;
  58. std::streambuf& _buf;
  59. };
  60. class Foundation_API HexBinaryEncoderIOS: public virtual std::ios
  61. /// The base class for HexBinaryEncoder.
  62. ///
  63. /// This class is needed to ensure the correct initialization
  64. /// order of the stream buffer and base classes.
  65. {
  66. public:
  67. HexBinaryEncoderIOS(std::ostream& ostr);
  68. ~HexBinaryEncoderIOS();
  69. int close();
  70. HexBinaryEncoderBuf* rdbuf();
  71. protected:
  72. HexBinaryEncoderBuf _buf;
  73. };
  74. class Foundation_API HexBinaryEncoder: public HexBinaryEncoderIOS, public std::ostream
  75. /// This ostream encodes all data
  76. /// written to it in BinHex encoding and forwards it to
  77. /// a connected ostream.
  78. /// Always call close() when done
  79. /// writing data, to ensure proper
  80. /// completion of the encoding operation.
  81. /// In hexBinary encoding, each binary octet is encoded as a character tuple,
  82. /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code.
  83. /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/),
  84. /// section 3.2.15.
  85. ///
  86. /// Note: The characters are directly written
  87. /// to the ostream's streambuf, thus bypassing
  88. /// the ostream. The ostream's state is therefore
  89. /// not updated to match the buffer's state.
  90. {
  91. public:
  92. HexBinaryEncoder(std::ostream& ostr);
  93. ~HexBinaryEncoder();
  94. };
  95. } // namespace Poco
  96. #endif // Foundation_HexBinaryEncoder_INCLUDED