StreamConverter.h 4.1 KB

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