StreamConverter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // StreamConverter.h
  3. //
  4. // Library: Foundation
  5. // Package: Text
  6. // Module: StreamConverter
  7. //
  8. // Definition of the StreamConverter class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_StreamConverter_INCLUDED
  16. #define Foundation_StreamConverter_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/TextEncoding.h"
  19. #include "Poco/UnbufferedStreamBuf.h"
  20. #include <istream>
  21. #include <ostream>
  22. namespace Poco {
  23. class Foundation_API StreamConverterBuf: public UnbufferedStreamBuf
  24. /// A StreamConverter converts streams from one encoding (inEncoding)
  25. /// into another (outEncoding).
  26. /// If a character cannot be represented in outEncoding, defaultChar
  27. /// is used instead.
  28. /// If a byte sequence is not valid in inEncoding, defaultChar is used
  29. /// instead and the encoding error count is incremented.
  30. {
  31. public:
  32. StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  33. /// Creates the StreamConverterBuf and connects it
  34. /// to the given input stream.
  35. StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  36. /// Creates the StreamConverterBuf and connects it
  37. /// to the given output stream.
  38. ~StreamConverterBuf();
  39. /// Destroys the StreamConverterBuf.
  40. int errors() const;
  41. /// Returns the number of encoding errors encountered.
  42. protected:
  43. int readFromDevice();
  44. int writeToDevice(char c);
  45. private:
  46. std::istream* _pIstr;
  47. std::ostream* _pOstr;
  48. const TextEncoding& _inEncoding;
  49. const TextEncoding& _outEncoding;
  50. int _defaultChar;
  51. unsigned char _buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
  52. int _sequenceLength;
  53. int _pos;
  54. int _errors;
  55. };
  56. class Foundation_API StreamConverterIOS: public virtual std::ios
  57. /// The base class for InputStreamConverter and OutputStreamConverter.
  58. ///
  59. /// This class is needed to ensure the correct initialization
  60. /// order of the stream buffer and base classes.
  61. {
  62. public:
  63. StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  64. StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  65. ~StreamConverterIOS();
  66. StreamConverterBuf* rdbuf();
  67. int errors() const;
  68. protected:
  69. StreamConverterBuf _buf;
  70. };
  71. class Foundation_API InputStreamConverter: public StreamConverterIOS, public std::istream
  72. /// This stream converts all characters read from the
  73. /// underlying istream from one character encoding into another.
  74. /// If a character cannot be represented in outEncoding, defaultChar
  75. /// is used instead.
  76. /// If a byte sequence read from the underlying stream is not valid in inEncoding,
  77. /// defaultChar is used instead and the encoding error count is incremented.
  78. {
  79. public:
  80. InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  81. /// Creates the InputStreamConverter and connects it
  82. /// to the given input stream.
  83. ~InputStreamConverter();
  84. /// Destroys the stream.
  85. };
  86. class Foundation_API OutputStreamConverter: public StreamConverterIOS, public std::ostream
  87. /// This stream converts all characters written to the
  88. /// underlying ostream from one character encoding into another.
  89. /// If a character cannot be represented in outEncoding, defaultChar
  90. /// is used instead.
  91. /// If a byte sequence written to the stream is not valid in inEncoding,
  92. /// defaultChar is used instead and the encoding error count is incremented.
  93. {
  94. public:
  95. OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?');
  96. /// Creates the OutputStreamConverter and connects it
  97. /// to the given input stream.
  98. ~OutputStreamConverter();
  99. /// Destroys the CountingOutputStream.
  100. };
  101. } // namespace Poco
  102. #endif // Foundation_StreamConverter_INCLUDED