cmProcessOutput.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmProcessOutput_h
  4. #define cmProcessOutput_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. /** \class cmProcessOutput
  10. * \brief Decode text data to internal encoding.
  11. *
  12. * cmProcessOutput is used to decode text output from external process
  13. * using external encoding to our internal encoding.
  14. */
  15. class cmProcessOutput
  16. {
  17. public:
  18. enum Encoding
  19. {
  20. None,
  21. Auto,
  22. UTF8,
  23. ANSI,
  24. OEM
  25. };
  26. /**
  27. * Find encoding enum value for given encoding \a name.
  28. * \param name a encoding name.
  29. * \return encoding enum value or Auto if \a name was not found.
  30. */
  31. static Encoding FindEncoding(std::string const& name);
  32. /// The code page that is used as internal encoding to which we will encode.
  33. static unsigned int defaultCodepage;
  34. /**
  35. * A class constructor.
  36. * \param encoding external process encoding from which we will decode.
  37. * \param maxSize a maximal size for process output buffer. It should match
  38. * to KWSYSPE_PIPE_BUFFER_SIZE. If text we decode is same size as \a maxSize
  39. * then we will check for incomplete character at end of buffer and
  40. * we will not return last incomplete character. This character will be
  41. * returned with next DecodeText() call. To disable this behavior specify
  42. * 0 as \a maxSize.
  43. */
  44. cmProcessOutput(Encoding encoding = Auto, unsigned int maxSize = 1024);
  45. ~cmProcessOutput();
  46. /**
  47. * Decode \a raw string using external encoding to internal
  48. * encoding in \a decoded.
  49. * \a id specifies which internal buffer to use. This is important when we
  50. * are decoding both stdout and stderr from process output and we need to
  51. * keep incomplete characters in separate buffers for each stream.
  52. * \return true if successfully decoded \a raw to \a decoded or false if not.
  53. */
  54. bool DecodeText(std::string raw, std::string& decoded, size_t id = 0);
  55. /**
  56. * Decode \a data with \a length from external encoding to internal
  57. * encoding in \a decoded.
  58. * \param data a pointer to process output text data.
  59. * \param length a size of data buffer.
  60. * \param decoded a string which will contain decoded text.
  61. * \param id an internal buffer id to use.
  62. * \return true if successfully decoded \a data to \a decoded or false if
  63. * not.
  64. */
  65. bool DecodeText(const char* data, size_t length, std::string& decoded,
  66. size_t id = 0);
  67. /**
  68. * \overload
  69. */
  70. bool DecodeText(std::vector<char> raw, std::vector<char>& decoded,
  71. size_t id = 0);
  72. private:
  73. #if defined(_WIN32)
  74. unsigned int codepage;
  75. unsigned int bufferSize;
  76. std::vector<std::string> rawparts;
  77. bool DoDecodeText(std::string raw, std::string& decoded, wchar_t* lastChar);
  78. #endif
  79. };
  80. #endif