Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

conversion.cxx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <algorithm>
  2. #include <string>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <locale.h>
  6. #include "conversion.hxx"
  7. #ifdef _WIN32
  8. #define strdup _strdup
  9. #endif
  10. using namespace std;
  11. namespace replxx {
  12. namespace locale {
  13. void to_lower( std::string& s_ ) {
  14. transform( s_.begin(), s_.end(), s_.begin(), static_cast<int(*)(int)>( &tolower ) );
  15. }
  16. bool is_8bit_encoding( void ) {
  17. bool is8BitEncoding( false );
  18. string origLC( setlocale( LC_CTYPE, nullptr ) );
  19. string lc( origLC );
  20. to_lower( lc );
  21. if ( lc == "c" ) {
  22. setlocale( LC_CTYPE, "" );
  23. }
  24. lc = setlocale( LC_CTYPE, nullptr );
  25. setlocale( LC_CTYPE, origLC.c_str() );
  26. to_lower( lc );
  27. if ( lc.find( "8859" ) != std::string::npos ) {
  28. is8BitEncoding = true;
  29. }
  30. return ( is8BitEncoding );
  31. }
  32. bool is8BitEncoding( is_8bit_encoding() );
  33. }
  34. ConversionResult copyString8to32(char32_t* dst, int dstSize, int& dstCount, const char* src) {
  35. ConversionResult res = ConversionResult::conversionOK;
  36. if ( ! locale::is8BitEncoding ) {
  37. const UTF8* sourceStart = reinterpret_cast<const UTF8*>(src);
  38. const UTF8* sourceEnd = sourceStart + strlen(src);
  39. UTF32* targetStart = reinterpret_cast<UTF32*>(dst);
  40. UTF32* targetEnd = targetStart + dstSize;
  41. res = ConvertUTF8toUTF32(
  42. &sourceStart, sourceEnd, &targetStart, targetEnd, lenientConversion);
  43. if (res == conversionOK) {
  44. dstCount = targetStart - reinterpret_cast<UTF32*>(dst);
  45. if (dstCount < dstSize) {
  46. *targetStart = 0;
  47. }
  48. }
  49. } else {
  50. for ( dstCount = 0; ( dstCount < dstSize ) && src[dstCount]; ++ dstCount ) {
  51. dst[dstCount] = src[dstCount];
  52. }
  53. }
  54. return res;
  55. }
  56. ConversionResult copyString8to32(char32_t* dst, int dstSize, int& dstCount, const char8_t* src) {
  57. return copyString8to32(
  58. dst, dstSize, dstCount, reinterpret_cast<const char*>(src)
  59. );
  60. }
  61. void copyString32to8(
  62. char* dst, int dstSize, const char32_t* src, int srcSize, int* dstCount
  63. ) {
  64. if ( ! locale::is8BitEncoding ) {
  65. const UTF32* sourceStart = reinterpret_cast<const UTF32*>(src);
  66. const UTF32* sourceEnd = sourceStart + srcSize;
  67. UTF8* targetStart = reinterpret_cast<UTF8*>(dst);
  68. UTF8* targetEnd = targetStart + dstSize;
  69. ConversionResult res = ConvertUTF32toUTF8(
  70. &sourceStart, sourceEnd, &targetStart, targetEnd, lenientConversion);
  71. if (res == conversionOK) {
  72. int resCount( targetStart - reinterpret_cast<UTF8*>( dst ) );
  73. if ( resCount < dstSize ) {
  74. *targetStart = 0;
  75. }
  76. if ( dstCount ) {
  77. *dstCount = resCount;
  78. }
  79. }
  80. } else {
  81. int i( 0 );
  82. for ( i = 0; ( i < dstSize ) && ( i < srcSize ) && src[i]; ++ i ) {
  83. dst[i] = static_cast<char>( src[i] );
  84. }
  85. if ( dstCount ) {
  86. *dstCount = i;
  87. }
  88. if ( i < dstSize ) {
  89. dst[i] = 0;
  90. }
  91. }
  92. }
  93. }