cm_codecvt.hxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 cm_codecvt_hxx
  4. #define cm_codecvt_hxx
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <locale>
  7. #include <wchar.h>
  8. class codecvt : public std::codecvt<char, char, mbstate_t>
  9. {
  10. public:
  11. enum Encoding
  12. {
  13. None,
  14. UTF8,
  15. ANSI
  16. };
  17. #ifdef CMAKE_BUILD_WITH_CMAKE
  18. codecvt(Encoding e);
  19. protected:
  20. ~codecvt() override;
  21. bool do_always_noconv() const throw() override;
  22. result do_out(mbstate_t& state, const char* from, const char* from_end,
  23. const char*& from_next, char* to, char* to_end,
  24. char*& to_next) const override;
  25. result do_unshift(mbstate_t& state, char* to, char*,
  26. char*& to_next) const override;
  27. int do_max_length() const throw() override;
  28. int do_encoding() const throw() override;
  29. private:
  30. // The mbstate_t argument to do_out and do_unshift is responsible
  31. // for storing state between calls. We cannot control the type
  32. // since we want to imbue on standard streams. However, we do
  33. // know that it is a trivial type. Define our own type to overlay
  34. // on it safely with no alignment requirements.
  35. struct State
  36. {
  37. // Buffer bytes we have consumed from a partial codepoint.
  38. char partial[3];
  39. // Number of bytes we have buffered from a partial codepoint.
  40. unsigned char buffered : 4;
  41. // Size of the current codepoint in bytes.
  42. unsigned char size : 4;
  43. };
  44. bool m_noconv;
  45. #if defined(_WIN32)
  46. unsigned int m_codepage;
  47. result Decode(mbstate_t& state, int need, const char*& from_next,
  48. char*& to_next, char* to_end) const;
  49. result DecodePartial(mbstate_t& state, char*& to_next, char* to_end) const;
  50. void BufferPartial(mbstate_t& state, int need, const char*& from_next) const;
  51. #endif
  52. #endif
  53. };
  54. #endif