iconv.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include "codepage.h"
  3. #include <util/generic/noncopyable.h>
  4. // WARNING: Do not use this functions - use functions from wide.h or recyr.hh instead.
  5. namespace NICONVPrivate {
  6. inline const char* CharsetName(ECharset code) {
  7. return NameByCharset(code);
  8. }
  9. inline const char* CharsetName(const char* code) {
  10. return code;
  11. }
  12. template <int size>
  13. inline const char* UnicodeNameBySize();
  14. template <>
  15. inline const char* UnicodeNameBySize<1>() {
  16. return "UTF-8";
  17. }
  18. template <>
  19. inline const char* UnicodeNameBySize<2>() {
  20. return "UTF-16LE";
  21. }
  22. template <>
  23. inline const char* UnicodeNameBySize<4>() {
  24. return "UCS-4LE";
  25. }
  26. template <class C>
  27. inline const char* UnicodeName() {
  28. return UnicodeNameBySize<sizeof(C)>();
  29. }
  30. class TDescriptor : NNonCopyable::TNonCopyable {
  31. private:
  32. void* Descriptor_;
  33. const char* From_;
  34. const char* To_;
  35. public:
  36. template <class TFrom, class TTo>
  37. inline TDescriptor(TFrom from, TTo to)
  38. : TDescriptor(CharsetName(from), CharsetName(to))
  39. {
  40. }
  41. TDescriptor(const char* from, const char* to);
  42. ~TDescriptor();
  43. inline void* Get() const {
  44. return Descriptor_;
  45. }
  46. inline bool Invalid() const {
  47. return Descriptor_ == (void*)(-1);
  48. }
  49. inline const char* From() const noexcept {
  50. return From_;
  51. }
  52. inline const char* To() const noexcept {
  53. return To_;
  54. }
  55. };
  56. template <class TFrom, class TTo>
  57. inline bool CanConvert(TFrom from, TTo to) {
  58. TDescriptor descriptor(from, to);
  59. return !descriptor.Invalid();
  60. }
  61. size_t RecodeImpl(const TDescriptor& descriptor, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written);
  62. void DoRecode(const TDescriptor& descriptor, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written);
  63. template <class TFrom, class TTo>
  64. inline void Recode(TFrom from, TTo to, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  65. TDescriptor descriptor(from, to);
  66. DoRecode(descriptor, in, out, inSize, outSize, read, written);
  67. }
  68. template <class TCharType>
  69. inline void RecodeToUnicode(ECharset from, const char* in, TCharType* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  70. const size_t charSize = sizeof(TCharType);
  71. Recode(from, UnicodeName<TCharType>(), in, reinterpret_cast<char*>(out), inSize, outSize * charSize, read, written);
  72. written /= charSize;
  73. }
  74. template <class TCharType>
  75. inline void RecodeFromUnicode(ECharset to, const TCharType* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  76. const size_t charSize = sizeof(TCharType);
  77. Recode(UnicodeName<TCharType>(), to, reinterpret_cast<const char*>(in), out, inSize * charSize, outSize, read, written);
  78. read /= charSize;
  79. }
  80. RECODE_RESULT DoRecodeNoThrow(const TDescriptor& d, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written);
  81. template <class TFrom, class TTo>
  82. inline RECODE_RESULT RecodeNoThrow(TFrom from, TTo to, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  83. TDescriptor descriptor(from, to);
  84. return DoRecodeNoThrow(descriptor, in, out, inSize, outSize, read, written);
  85. }
  86. template <class TCharType>
  87. inline RECODE_RESULT RecodeToUnicodeNoThrow(ECharset from, const char* in, TCharType* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  88. const size_t charSize = sizeof(TCharType);
  89. RECODE_RESULT res = RecodeNoThrow(from, UnicodeName<TCharType>(), in, reinterpret_cast<char*>(out), inSize, outSize * charSize, read, written);
  90. written /= charSize;
  91. return res;
  92. }
  93. template <class TCharType>
  94. inline RECODE_RESULT RecodeFromUnicodeNoThrow(ECharset to, const TCharType* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
  95. const size_t charSize = sizeof(TCharType);
  96. RECODE_RESULT res = RecodeNoThrow(UnicodeName<TCharType>(), to, reinterpret_cast<const char*>(in), out, inSize * charSize, outSize, read, written);
  97. read /= charSize;
  98. return res;
  99. }
  100. }