UTFConverter.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #ifndef ASSIMP_SAMPLES_SHARED_CODE_UTFCONVERTER_H
  35. #define ASSIMP_SAMPLES_SHARED_CODE_UTFCONVERTER_H
  36. #include <string>
  37. #include <locale>
  38. #include <codecvt>
  39. namespace AssimpSamples {
  40. namespace SharedCode {
  41. // Used to convert between multibyte and unicode strings.
  42. class UTFConverter {
  43. using UTFConverterImpl = std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>;
  44. public:
  45. UTFConverter(const char* s) :
  46. s_(s),
  47. ws_(impl_.from_bytes(s)) {
  48. }
  49. UTFConverter(const wchar_t* s) :
  50. s_(impl_.to_bytes(s)),
  51. ws_(s) {
  52. }
  53. UTFConverter(const std::string& s) :
  54. s_(s),
  55. ws_(impl_.from_bytes(s)) {
  56. }
  57. UTFConverter(const std::wstring& s) :
  58. s_(impl_.to_bytes(s)),
  59. ws_(s) {
  60. }
  61. inline const char* c_str() const {
  62. return s_.c_str();
  63. }
  64. inline const std::string& str() const {
  65. return s_;
  66. }
  67. inline const wchar_t* c_wstr() const {
  68. return ws_.c_str();
  69. }
  70. private:
  71. static UTFConverterImpl impl_;
  72. std::string s_;
  73. std::wstring ws_;
  74. };
  75. }
  76. }
  77. #endif // ASSIMP_SAMPLES_SHARED_CODE_UTFCONVERTER_H