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.
This commit is contained in:
Nathan Woods 2016-10-01 14:53:32 -04:00
parent 0e2299e424
commit c0ff37c30d
9 changed files with 150 additions and 22 deletions

View File

@ -43,7 +43,7 @@ consolewin_info::consolewin_info(debugger_windows_interface &debugger) :
m_devices_menu = CreatePopupMenu();
for (device_image_interface &img : iter)
{
auto tc_buf = tstring_from_utf8(string_format("%s : %s", img.device().name(), img.exists() ? img.filename() : "[no image]").c_str());
tstring tc_buf = tstring_from_utf8(string_format("%s : %s", img.device().name(), img.exists() ? img.filename() : "[no image]"));
AppendMenu(m_devices_menu, MF_ENABLED, 0, tc_buf.c_str());
}
AppendMenu(GetMenu(window()), MF_ENABLED | MF_POPUP, (UINT_PTR)m_devices_menu, TEXT("Images"));
@ -189,7 +189,7 @@ void consolewin_info::update_menu()
AppendMenu(devicesubmenu, flags_for_exists, new_item + DEVOPTION_CASSETTE_FASTFORWARD, TEXT("Fast Forward"));
}
auto tc_buf = tstring_from_utf8(string_format("%s :%s", img.device().name(), img.exists() ? img.filename() : "[empty slot]").c_str());
tstring tc_buf = tstring_from_utf8(string_format("%s :%s", img.device().name(), img.exists() ? img.filename() : "[empty slot]"));
ModifyMenu(m_devices_menu, cnt, MF_BYPOSITION | MF_POPUP, (UINT_PTR)devicesubmenu, tc_buf.c_str());
cnt++;
@ -214,7 +214,7 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam)
std::string filter;
build_generic_filter(img, false, filter);
{
auto t_filter = tstring_from_utf8(filter.c_str());
tstring t_filter = tstring_from_utf8(filter);
// convert a pipe-char delimited string into a NUL delimited string
for (int i = 0; t_filter[i] != '\0'; i++)
@ -252,7 +252,7 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam)
std::string filter;
build_generic_filter(img, true, filter);
{
auto t_filter = tstring_from_utf8(filter.c_str());
tstring t_filter = tstring_from_utf8(filter);
// convert a pipe-char delimited string into a NUL delimited string
for (int i = 0; t_filter[i] != '\0'; i++)
{

View File

@ -112,7 +112,7 @@ bool win_directory::open_impl(std::string const &dirname)
std::string dirfilter = string_format("%s\\*.*", dirname);
// convert the path to TCHARs
auto t_dirfilter = tstring_from_utf8(dirfilter.c_str());
tstring t_dirfilter = tstring_from_utf8(dirfilter);
// attempt to find the first file
m_find = FindFirstFileEx(t_dirfilter.c_str(), FindExInfoStandard, &m_data, FindExSearchNameMatch, nullptr, 0);

View File

@ -173,7 +173,7 @@ osd_file::error osd_file::open(std::string const &orig_path, UINT32 openflags, p
return win_open_ptty(path, openflags, file, filesize);
// convert path to TCHAR
auto t_path = tstring_from_utf8(path.c_str());
tstring t_path = tstring_from_utf8(path);
// convert the path into something Windows compatible (the actual interesting part appears
// to have been commented out???)
@ -274,7 +274,7 @@ osd_file::error osd_file::openpty(ptr &file, std::string &name)
osd_file::error osd_file::remove(std::string const &filename)
{
auto tempstr = tstring_from_utf8(filename.c_str());
tstring tempstr = tstring_from_utf8(filename);
error filerr = error::NONE;
if (!DeleteFile(tempstr.c_str()))
@ -338,7 +338,7 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN
std::unique_ptr<osd::directory::entry> osd_stat(const std::string &path)
{
// convert the path to TCHARs
auto t_path = tstring_from_utf8(path.c_str());
tstring t_path = tstring_from_utf8(path);
// is this path a root directory (e.g. - C:)?
WIN32_FIND_DATA find_data;
@ -382,7 +382,7 @@ std::unique_ptr<osd::directory::entry> osd_stat(const std::string &path)
osd_file::error osd_get_full_path(std::string &dst, std::string const &path)
{
// convert the path to TCHARs
auto t_path = tstring_from_utf8(path.c_str());
tstring t_path = tstring_from_utf8(path);
// cannonicalize the path
TCHAR buffer[MAX_PATH];
@ -402,7 +402,7 @@ osd_file::error osd_get_full_path(std::string &dst, std::string const &path)
bool osd_is_absolute_path(std::string const &path)
{
auto t_path = tstring_from_utf8(path.c_str());
tstring t_path = tstring_from_utf8(path);
return !PathIsRelative(t_path.c_str());
}

View File

@ -87,7 +87,7 @@ bool win_check_ptty_path(std::string const &path)
osd_file::error win_open_ptty(std::string const &path, std::uint32_t openflags, osd_file::ptr &file, std::uint64_t &filesize)
{
auto t_name = tstring_from_utf8(path.c_str());
tstring t_name = tstring_from_utf8(path);
HANDLE pipe = CreateNamedPipe(t_name.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 32, 32, 0, nullptr);

View File

@ -85,7 +85,7 @@ bool osd_font_windows::open(std::string const &font_path, std::string const &_na
logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
// copy in the face name
std::basic_string<TCHAR> face = tstring_from_utf8(name.c_str());
tstring face = tstring_from_utf8(name);
_tcsncpy(logfont.lfFaceName, face.c_str(), ARRAY_LENGTH(logfont.lfFaceName));
logfont.lfFaceName[sizeof(logfont.lfFaceName) / sizeof(TCHAR)-1] = 0;

View File

@ -367,7 +367,7 @@ protected:
for (auto const &library : m_libraries)
{
auto tempstr = tstring_from_utf8(library.c_str());
tstring tempstr = tstring_from_utf8(library);
HMODULE module = load_library(tempstr.c_str());
if (module != nullptr)

View File

@ -16,6 +16,36 @@
#include "strconv.h"
#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
//============================================================
@ -23,14 +53,22 @@
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;
}
@ -46,6 +84,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
//============================================================
@ -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;
}
@ -74,6 +140,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
//============================================================
@ -89,6 +170,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
//============================================================
@ -101,6 +194,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
//============================================================
@ -116,6 +224,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
//============================================================

View File

@ -25,22 +25,30 @@
#include <windows.h>
// the result of these functions has to be released with osd_free()
std::string astring_from_utf8(const std::string &s);
std::string astring_from_utf8(const char *s);
std::string &astring_from_utf8(std::string &dst, std::string &s);
std::string &astring_from_utf8(std::string &dst, const char *s);
std::string utf8_from_astring(const std::string &s);
std::string utf8_from_astring(const CHAR *s);
std::string &utf8_from_astring(std::string &dst, const std::string &s);
std::string &utf8_from_astring(std::string &dst, const CHAR *s);
std::wstring wstring_from_utf8(const std::string &s);
std::wstring wstring_from_utf8(const char *s);
std::wstring &wstring_from_utf8(std::wstring &dst, std::string &s);
std::wstring &wstring_from_utf8(std::wstring &dst, const char *s);
std::string utf8_from_wstring(const std::wstring &s);
std::string utf8_from_wstring(const WCHAR *s);
std::string &utf8_from_wstring(std::string &dst, const std::wstring &s);
std::string &utf8_from_wstring(std::string &dst, const WCHAR *s);
#ifdef UNICODE
typedef std::wstring tstring;
#define tstring_from_utf8 wstring_from_utf8
#define utf8_from_tstring utf8_from_wstring
#else // !UNICODE
typedef std::string tstring;
#define tstring_from_utf8 astring_from_utf8
#define utf8_from_tstring utf8_from_astring
#endif // UNICODE

View File

@ -106,7 +106,7 @@ void osd_subst_env(std::string &dst, const std::string &src)
{
TCHAR buffer[MAX_PATH];
auto t_src = tstring_from_utf8(src.c_str());
tstring t_src = tstring_from_utf8(src);
ExpandEnvironmentStrings(t_src.c_str(), buffer, ARRAY_LENGTH(buffer));
utf8_from_tstring(dst, buffer);
}