OSDN Git Service

refactoring.
[meshio/meshio.git] / include / text.h
old mode 100755 (executable)
new mode 100644 (file)
index be8ba4f..fcb6dfa
@@ -7,6 +7,10 @@
 #include <string>
 #include <stdlib.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 namespace meshio {
 namespace text {
 
@@ -367,6 +371,39 @@ inline void copyStringAndFillZero(char *dst, const std::string &src)
        }
 }
 
+#ifdef _WIN32
+inline std::wstring to_WideChar(UINT uCodePage, const std::string &text)
+{
+       int size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, NULL, 0);
+       std::vector<wchar_t> buf(size);
+       size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size());
+       return std::wstring(buf.begin(), buf.begin()+size);
+}
+
+inline std::string to_MultiByte(UINT uCodePage, const std::wstring &text)
+{
+       int size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, NULL, 0, 0, NULL);
+       std::vector<char> buf(size);
+       size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size(), 0, NULL);
+       return std::string(buf.begin(), buf.begin()+size);
+}
+
+inline std::string cp932_to_utf8(const std::string &text)
+{
+       return to_MultiByte(CP_UTF8, to_WideChar(CP_OEMCP, text));
+}
+
+inline std::wstring trim(const std::wstring &src){
+       std::wstring::const_iterator end=src.begin();
+       for(; end!=src.end(); ++end){
+               if(*end==L'\0'){
+                       break;
+               }
+       }
+       return std::wstring(src.begin(), end);
+}
+#endif
+
 } // namespace text
 } // namespace meshio
 #endif // MESH_IO_TEXT_H_INCLUDED