test_locale_tools.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_LOCLAE_TEST_LOCALE_TOOLS_HPP
  9. #define BOOST_LOCLAE_TEST_LOCALE_TOOLS_HPP
  10. #include <boost/locale/encoding.hpp>
  11. #include <fstream>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. template<typename Char>
  15. std::basic_string<Char> to_correct_string(std::string const &e,std::locale /*l*/)
  16. {
  17. return boost::locale::conv::to_utf<Char>(e,"UTF-8");
  18. }
  19. template<>
  20. inline std::string to_correct_string(std::string const &e,std::locale l)
  21. {
  22. return boost::locale::conv::from_utf(e,l);
  23. }
  24. bool has_std_locale(std::string const &name)
  25. {
  26. try {
  27. std::locale tmp(name.c_str());
  28. return true;
  29. }
  30. catch(...) {
  31. return false;
  32. }
  33. }
  34. inline bool test_std_supports_SJIS_codecvt(std::string const &locale_name)
  35. {
  36. bool res = true;
  37. {
  38. // Japan in Shift JIS/cp932
  39. char const *japan_932 = "\x93\xfa\x96\x7b";
  40. std::ofstream f("test-siftjis.txt");
  41. f<<japan_932;
  42. f.close();
  43. }
  44. try {
  45. std::wfstream test;
  46. test.imbue(std::locale(locale_name.c_str()));
  47. test.open("test-siftjis.txt");
  48. // Japan in Unicode
  49. std::wstring cmp = L"\u65e5\u672c";
  50. std::wstring ref;
  51. test >> ref;
  52. res = ref == cmp;
  53. }
  54. catch(std::exception const &)
  55. {
  56. res = false;
  57. }
  58. remove("test-siftjis.txt");
  59. return res;
  60. }
  61. std::string get_std_name(std::string const &name,std::string *real_name = 0)
  62. {
  63. if(has_std_locale(name)) {
  64. if(real_name)
  65. *real_name = name;
  66. return name;
  67. }
  68. #ifdef BOOST_WINDOWS
  69. bool utf8=name.find("UTF-8")!=std::string::npos;
  70. if(name=="en_US.UTF-8" || name == "en_US.ISO8859-1") {
  71. if(has_std_locale("English_United States.1252")) {
  72. if(real_name)
  73. *real_name = "English_United States.1252";
  74. return utf8 ? name : "en_US.windows-1252";
  75. }
  76. return "";
  77. }
  78. else if(name=="he_IL.UTF-8" || name == "he_IL.ISO8859-8") {
  79. if(has_std_locale("Hebrew_Israel.1255")) {
  80. if(real_name)
  81. *real_name = "Hebrew_Israel.1255";
  82. return utf8 ? name : "he_IL.windows-1255";
  83. return name;
  84. }
  85. }
  86. else if(name=="ru_RU.UTF-8") {
  87. if(has_std_locale("Russian_Russia.1251")) {
  88. if(real_name)
  89. *real_name = "Russian_Russia.1251";
  90. return name;
  91. }
  92. }
  93. else if(name == "tr_TR.UTF-8") {
  94. if(has_std_locale("Turkish_Turkey.1254")) {
  95. if(real_name)
  96. *real_name = "Turkish_Turkey.1254";
  97. return name;
  98. }
  99. }
  100. if(name == "ja_JP.SJIS") {
  101. if(has_std_locale("Japanese_Japan.932")) {
  102. if(real_name)
  103. *real_name = "Japanese_Japan.932";
  104. return name;
  105. }
  106. return "";
  107. }
  108. #endif
  109. return "";
  110. }
  111. #endif
  112. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4