HexBinaryEncoder.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // HexBinaryEncoder.h
  3. //
  4. // $Id: //poco/svn/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. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_HexBinaryEncoder_INCLUDED
  38. #define Foundation_HexBinaryEncoder_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/UnbufferedStreamBuf.h"
  41. #include <ostream>
  42. namespace Poco {
  43. class Foundation_API HexBinaryEncoderBuf: public UnbufferedStreamBuf
  44. /// This streambuf encodes all data written
  45. /// to it in hexBinary encoding and forwards it to a connected
  46. /// ostream.
  47. /// In hexBinary encoding, each binary octet is encoded as a character tuple,
  48. /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code.
  49. /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/),
  50. /// section 3.2.15.
  51. {
  52. public:
  53. HexBinaryEncoderBuf(std::ostream& ostr);
  54. ~HexBinaryEncoderBuf();
  55. int close();
  56. /// Closes the stream buffer.
  57. void setLineLength(int lineLength);
  58. /// Specify the line length.
  59. ///
  60. /// After the given number of characters have been written,
  61. /// a newline character will be written.
  62. ///
  63. /// Specify 0 for an unlimited line length.
  64. int getLineLength() const;
  65. /// Returns the currently set line length.
  66. void setUppercase(bool flag = true);
  67. /// Specify whether hex digits a-f are written in upper or lower case.
  68. private:
  69. int writeToDevice(char c);
  70. int _pos;
  71. int _lineLength;
  72. int _uppercase;
  73. std::ostream& _ostr;
  74. };
  75. class Foundation_API HexBinaryEncoderIOS: public virtual std::ios
  76. /// The base class for HexBinaryEncoder.
  77. ///
  78. /// This class is needed to ensure the correct initialization
  79. /// order of the stream buffer and base classes.
  80. {
  81. public:
  82. HexBinaryEncoderIOS(std::ostream& ostr);
  83. ~HexBinaryEncoderIOS();
  84. int close();
  85. HexBinaryEncoderBuf* rdbuf();
  86. protected:
  87. HexBinaryEncoderBuf _buf;
  88. };
  89. class Foundation_API HexBinaryEncoder: public HexBinaryEncoderIOS, public std::ostream
  90. /// This ostream encodes all data
  91. /// written to it in BinHex encoding and forwards it to
  92. /// a connected ostream.
  93. /// Always call close() when done
  94. /// writing data, to ensure proper
  95. /// completion of the encoding operation.
  96. /// In hexBinary encoding, each binary octet is encoded as a character tuple,
  97. /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code.
  98. /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/),
  99. /// section 3.2.15.
  100. {
  101. public:
  102. HexBinaryEncoder(std::ostream& ostr);
  103. ~HexBinaryEncoder();
  104. };
  105. } // namespace Poco
  106. #endif // Foundation_HexBinaryEncoder_INCLUDED