summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/strconv.cpp
diff options
context:
space:
mode:
author Nathan Woods <npwoods@mess.org>2016-10-01 14:53:32 -0400
committer Nathan Woods <npwoods@mess.org>2016-10-01 14:53:32 -0400
commitc0ff37c30da931bd1ea203aba1e8dc8a05ea1bf1 (patch)
tree97265a831a19f4ef76e049886a5771033d25edcc /src/osd/strconv.cpp
parent0e2299e424d7da177a57bb530ae740c79a3c92a0 (diff)
Adding new string conversion overloads
[a|w|t|utf8]_from_[a|w|t|utf8_]string(xyz.c_str()) seems to be common enough to justify overloads. Also, I'm explicitly assuming that it is legal to override the NUL pointer within a C++ basic_string (e.g. - s[s.size()] = '\0'). As far as I can tell, this seems to be legal - please don't shoot if I am wrong.
Diffstat (limited to 'src/osd/strconv.cpp')
-rw-r--r--src/osd/strconv.cpp132
1 files changed, 126 insertions, 6 deletions
diff --git a/src/osd/strconv.cpp b/src/osd/strconv.cpp
index 7ab8f2f401b..303b32c51a9 100644
--- a/src/osd/strconv.cpp
+++ b/src/osd/strconv.cpp
@@ -17,20 +17,58 @@
#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS)
//============================================================
+// astring_from_wstring
+//============================================================
+
+static std::string &astring_from_wstring(std::string &dst, const std::wstring &wstring)
+{
+ // convert UTF-16 to "ANSI code page" string
+ int char_count = WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), wstring.size() + 1, nullptr, 0, nullptr, nullptr);
+ dst.resize(char_count - 1);
+ WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), wstring.size(), &dst[0], char_count, nullptr, nullptr);
+
+ return dst;
+}
+
+
+//============================================================
+// astring_from_utf8
+//============================================================
+
+std::string &astring_from_utf8(std::string &dst, const std::string &s)
+{
+ // convert MAME string (UTF-8) to UTF-16
+ std::wstring wstring = wstring_from_utf8(s);
+
+ // convert UTF-16 to "ANSI code page" string
+ return astring_from_wstring(dst, wstring);
+}
+
+
+
+//============================================================
// astring_from_utf8
//============================================================
std::string &astring_from_utf8(std::string &dst, const char *s)
{
// convert MAME string (UTF-8) to UTF-16
- std::basic_string<WCHAR> wstring = wstring_from_utf8(s);
+ std::wstring wstring = wstring_from_utf8(s);
// convert UTF-16 to "ANSI code page" string
- int char_count = WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), wstring.size(), nullptr, 0, nullptr, nullptr);
- dst.resize(char_count);
- WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), wstring.size(), &dst[0], char_count, nullptr, nullptr);
+ return astring_from_wstring(dst, wstring);
+}
- return dst;
+
+//============================================================
+// astring_from_utf8
+//============================================================
+
+std::string astring_from_utf8(const std::string &s)
+{
+ std::string result;
+ astring_from_utf8(result, s);
+ return result;
}
@@ -50,6 +88,22 @@ std::string astring_from_utf8(const char *s)
// utf8_from_astring
//============================================================
+std::string &utf8_from_astring(std::string &dst, const std::string &s)
+{
+ // convert "ANSI code page" string to UTF-16
+ int char_count = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size() + 1, nullptr, 0);
+ std::wstring wstring(char_count - 1, 0);
+ MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size() + 1, &wstring[0], char_count - 1);
+
+ // convert UTF-16 to MAME string (UTF-8)
+ return utf8_from_wstring(dst, wstring);
+}
+
+
+//============================================================
+// utf8_from_astring
+//============================================================
+
std::string &utf8_from_astring(std::string &dst, const CHAR *s)
{
// convert "ANSI code page" string to UTF-16
@@ -58,7 +112,19 @@ std::string &utf8_from_astring(std::string &dst, const CHAR *s)
MultiByteToWideChar(CP_ACP, 0, s, -1, &wstring[0], char_count - 1);
// convert UTF-16 to MAME string (UTF-8)
- return utf8_from_wstring(dst, wstring.c_str());
+ return utf8_from_wstring(dst, wstring);
+}
+
+
+//============================================================
+// utf8_from_astring
+//============================================================
+
+std::string utf8_from_astring(const std::string &s)
+{
+ std::string result;
+ utf8_from_astring(result, s);
+ return result;
}
@@ -78,6 +144,21 @@ std::string utf8_from_astring(const CHAR *s)
// wstring_from_utf8
//============================================================
+std::wstring &wstring_from_utf8(std::wstring &dst, const std::string &s)
+{
+ // convert MAME string (UTF-8) to UTF-16
+ int char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size() + 1, nullptr, 0);
+ dst.resize(char_count - 1);
+ MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size() + 1, &dst[0], char_count - 1);
+
+ return dst;
+}
+
+
+//============================================================
+// wstring_from_utf8
+//============================================================
+
std::wstring &wstring_from_utf8(std::wstring &dst, const char *s)
{
// convert MAME string (UTF-8) to UTF-16
@@ -93,6 +174,18 @@ std::wstring &wstring_from_utf8(std::wstring &dst, const char *s)
// wstring_from_utf8
//============================================================
+std::wstring wstring_from_utf8(const std::string &s)
+{
+ std::wstring result;
+ wstring_from_utf8(result, s);
+ return result;
+}
+
+
+//============================================================
+// wstring_from_utf8
+//============================================================
+
std::wstring wstring_from_utf8(const char *s)
{
std::wstring result;
@@ -105,6 +198,21 @@ std::wstring wstring_from_utf8(const char *s)
// utf8_from_wstring
//============================================================
+std::string &utf8_from_wstring(std::string &dst, const std::wstring &s)
+{
+ // convert UTF-16 to MAME string (UTF-8)
+ int char_count = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), s.size() + 1, nullptr, 0, nullptr, nullptr);
+ dst.resize(char_count - 1);
+ WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, &dst[0], char_count - 1, nullptr, nullptr);
+
+ return dst;
+}
+
+
+//============================================================
+// utf8_from_wstring
+//============================================================
+
std::string &utf8_from_wstring(std::string &dst, const WCHAR *s)
{
// convert UTF-16 to MAME string (UTF-8)
@@ -120,6 +228,18 @@ std::string &utf8_from_wstring(std::string &dst, const WCHAR *s)
// utf8_from_wstring
//============================================================
+std::string utf8_from_wstring(const std::wstring &s)
+{
+ std::string result;
+ utf8_from_wstring(result, s);
+ return result;
+}
+
+
+//============================================================
+// utf8_from_wstring
+//============================================================
+
std::string utf8_from_wstring(const WCHAR *s)
{
std::string result;