You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

2 months ago
  1. #pragma once
  2. #include <iostream>
  3. #include <ostream>
  4. #include <sstream>
  5. #ifndef _MSC_VER
  6. #include <boost/locale.hpp>
  7. #else
  8. #include <boost/scoped_array.hpp>
  9. #include "windows.h"
  10. #include "winnls.h" // unicode-multibyte conversion
  11. #endif
  12. inline std::string wide2utf8(const std::wstring& text) {
  13. #ifndef _MSC_VER
  14. return boost::locale::conv::to_utf<wchar_t>(text, "UTF-8");
  15. #else
  16. const size_t len_needed = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (UINT)(text.length()) , NULL, 0, NULL, NULL) ;
  17. boost::scoped_array<char> buff(new char[len_needed+1]) ;
  18. const size_t num_copied = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (UINT)(text.length()) , buff.get(), len_needed+1, NULL, NULL) ;
  19. return std::string(buff.get(), num_copied) ;
  20. #endif
  21. }
  22. namespace std {
  23. inline ostream& operator<<(ostream& out, const wchar_t* value)
  24. {
  25. wstring text(value) ;
  26. out << wide2utf8(text);
  27. return out;
  28. }
  29. inline ostream& operator<<(ostream& out, const wstring& value)
  30. {
  31. out << wide2utf8(value);
  32. return out;
  33. }
  34. }