From ab73291e479be171d6f89798ed706b5ea58aa09f Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sun, 24 Jul 2016 22:55:00 -0400 Subject: [PATCH] Changed strconv.[cpp|h] functions to return their results as std::string and std::wstring --- .../modules/debugger/win/consolewininfo.cpp | 45 +++----- .../modules/debugger/win/debugviewinfo.cpp | 5 +- src/osd/modules/debugger/win/editwininfo.cpp | 14 +-- src/osd/modules/debugger/win/uimetrics.cpp | 5 +- src/osd/modules/file/windir.cpp | 30 ++--- src/osd/modules/file/winfile.cpp | 64 +++-------- src/osd/modules/file/winptty.cpp | 7 +- src/osd/modules/font/font_windows.cpp | 15 +-- src/osd/modules/input/input_dinput.cpp | 19 +--- src/osd/modules/input/input_dinput.h | 5 +- src/osd/modules/input/input_rawinput.cpp | 11 +- src/osd/modules/lib/osdlib_win32.cpp | 22 ++-- src/osd/modules/render/d3d/d3dhlsl.cpp | 6 +- src/osd/strconv.cpp | 107 +++++++++++------- src/osd/strconv.h | 33 ++---- src/osd/windows/main.cpp | 28 ++--- src/osd/windows/video.cpp | 8 +- src/osd/windows/winutf8.cpp | 85 +++++--------- src/osd/windows/winutil.cpp | 9 +- 19 files changed, 194 insertions(+), 324 deletions(-) diff --git a/src/osd/modules/debugger/win/consolewininfo.cpp b/src/osd/modules/debugger/win/consolewininfo.cpp index 9245250b384..735f59a2fbb 100644 --- a/src/osd/modules/debugger/win/consolewininfo.cpp +++ b/src/osd/modules/debugger/win/consolewininfo.cpp @@ -43,12 +43,8 @@ consolewin_info::consolewin_info(debugger_windows_interface &debugger) : m_devices_menu = CreatePopupMenu(); for (device_image_interface &img : iter) { - TCHAR *tc_buf = tstring_from_utf8(string_format("%s : %s", img.device().name(), img.exists() ? img.filename() : "[no image]").c_str()); - if (tc_buf != nullptr) - { - AppendMenu(m_devices_menu, MF_ENABLED, 0, tc_buf); - osd_free(tc_buf); - } + auto tc_buf = tstring_from_utf8(string_format("%s : %s", img.device().name(), img.exists() ? img.filename() : "[no image]").c_str()); + 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")); } @@ -195,12 +191,8 @@ void consolewin_info::update_menu() AppendMenu(devicesubmenu, flags_for_exists, new_item + DEVOPTION_CASSETTE_FASTFORWARD, TEXT("Fast Forward")); } - TCHAR *tc_buf = tstring_from_utf8(string_format("%s :%s", img.device().name(), img.exists() ? img.filename() : "[empty slot]").c_str()); - if (tc_buf != nullptr) - { - ModifyMenu(m_devices_menu, cnt, MF_BYPOSITION | MF_POPUP, (UINT_PTR)devicesubmenu, tc_buf); - osd_free(tc_buf); - } + auto tc_buf = tstring_from_utf8(string_format("%s :%s", img.device().name(), img.exists() ? img.filename() : "[empty slot]").c_str()); + ModifyMenu(m_devices_menu, cnt, MF_BYPOSITION | MF_POPUP, (UINT_PTR)devicesubmenu, tc_buf.c_str()); cnt++; } @@ -223,9 +215,9 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) { std::string filter; build_generic_filter(img, false, filter); - LPTSTR t_filter = tstring_from_utf8(filter.c_str()); - if (t_filter) { + auto t_filter = tstring_from_utf8(filter.c_str()); + // convert a pipe-char delimited string into a NUL delimited string for (int i = 0; t_filter[i] != '\0'; i++) { @@ -242,7 +234,7 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) ofn.lpstrFile = selectedFilename; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = MAX_PATH; - ofn.lpstrFilter = t_filter; + ofn.lpstrFilter = t_filter.c_str(); ofn.nFilterIndex = 1; ofn.lpstrFileTitle = nullptr; ofn.nMaxFileTitle = 0; @@ -251,14 +243,9 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) if (GetOpenFileName(&ofn)) { - char *utf8_buf = utf8_from_tstring(selectedFilename); - if (utf8_buf != nullptr) - { - img->load(utf8_buf); - osd_free(utf8_buf); - } + auto utf8_buf = utf8_from_tstring(selectedFilename); + img->load(utf8_buf.c_str()); } - osd_free(t_filter); } } return true; @@ -266,9 +253,8 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) { std::string filter; build_generic_filter(img, true, filter); - LPTSTR t_filter = tstring_from_utf8(filter.c_str()); - if (t_filter) { + auto t_filter = tstring_from_utf8(filter.c_str()); // convert a pipe-char delimited string into a NUL delimited string for (int i = 0; t_filter[i] != '\0'; i++) { @@ -285,7 +271,7 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) ofn.lpstrFile = selectedFilename; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = MAX_PATH; - ofn.lpstrFilter = t_filter; + ofn.lpstrFilter = t_filter.c_str(); ofn.nFilterIndex = 1; ofn.lpstrFileTitle = nullptr; ofn.nMaxFileTitle = 0; @@ -294,14 +280,9 @@ bool consolewin_info::handle_command(WPARAM wparam, LPARAM lparam) if (GetSaveFileName(&ofn)) { - char *utf8_buf = utf8_from_tstring(selectedFilename); - if (utf8_buf != nullptr) - { - img->create(utf8_buf, img->device_get_indexed_creatable_format(0), nullptr); - osd_free(utf8_buf); - } + auto utf8_buf = utf8_from_tstring(selectedFilename); + img->create(utf8_buf.c_str(), img->device_get_indexed_creatable_format(0), nullptr); } - osd_free(t_filter); } } return true; diff --git a/src/osd/modules/debugger/win/debugviewinfo.cpp b/src/osd/modules/debugger/win/debugviewinfo.cpp index c2d4bdb85ca..2104e09246e 100644 --- a/src/osd/modules/debugger/win/debugviewinfo.cpp +++ b/src/osd/modules/debugger/win/debugviewinfo.cpp @@ -268,9 +268,8 @@ HWND debugview_info::create_source_combobox(HWND parent, LONG_PTR userdata) int const length = strlen(source->name()); if (length > maxlength) maxlength = length; - TCHAR *t_name = tstring_from_utf8(source->name()); - SendMessage(result, CB_ADDSTRING, 0, (LPARAM)t_name); - osd_free(t_name); + auto t_name = tstring_from_utf8(source->name()); + SendMessage(result, CB_ADDSTRING, 0, (LPARAM)t_name.c_str()); } if (cursource != nullptr) { diff --git a/src/osd/modules/debugger/win/editwininfo.cpp b/src/osd/modules/debugger/win/editwininfo.cpp index 6bf98df4352..5859ae5fb40 100644 --- a/src/osd/modules/debugger/win/editwininfo.cpp +++ b/src/osd/modules/debugger/win/editwininfo.cpp @@ -79,12 +79,8 @@ void editwin_info::set_editwnd_bounds(RECT const &bounds) void editwin_info::set_editwnd_text(char const *text) { - TCHAR *tc_buffer = tstring_from_utf8(text); - if (tc_buffer != nullptr) - { - SendMessage(m_editwnd, WM_SETTEXT, (WPARAM)0, (LPARAM)tc_buffer); - osd_free(tc_buffer); - } + auto tc_buffer = tstring_from_utf8(text); + SendMessage(m_editwnd, WM_SETTEXT, (WPARAM)0, (LPARAM)tc_buffer.c_str()); } @@ -190,11 +186,9 @@ LRESULT editwin_info::edit_proc(UINT message, WPARAM wparam, LPARAM lparam) m_last_history = m_history_count - 1; // process - char *utf8_buffer = utf8_from_tstring(buffer); - if (utf8_buffer != nullptr) { - process_string(utf8_buffer); - osd_free(utf8_buffer); + auto utf8_buffer = utf8_from_tstring(buffer); + process_string(utf8_buffer.c_str()); } break; } diff --git a/src/osd/modules/debugger/win/uimetrics.cpp b/src/osd/modules/debugger/win/uimetrics.cpp index c2d165c6a94..ab45ac35312 100644 --- a/src/osd/modules/debugger/win/uimetrics.cpp +++ b/src/osd/modules/debugger/win/uimetrics.cpp @@ -27,10 +27,9 @@ ui_metrics::ui_metrics(osd_options const &options) : char const *const face = options.debugger_font(); // create a standard font - TCHAR *t_face = tstring_from_utf8((!*face || !strcmp(OSDOPTVAL_AUTO, face)) ? "Lucida Console" : face); + auto t_face = tstring_from_utf8((!*face || !strcmp(OSDOPTVAL_AUTO, face)) ? "Lucida Console" : face); m_debug_font = CreateFont(-MulDiv((size <= 0) ? 9 : size, GetDeviceCaps(temp_dc, LOGPIXELSY), 72), 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, - ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, t_face); - osd_free(t_face); + ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, t_face.c_str()); if (m_debug_font == nullptr) fatalerror("Unable to create debugger font\n"); diff --git a/src/osd/modules/file/windir.cpp b/src/osd/modules/file/windir.cpp index 3ecd2412910..9b67e163652 100644 --- a/src/osd/modules/file/windir.cpp +++ b/src/osd/modules/file/windir.cpp @@ -14,6 +14,7 @@ // MAME headers #include "osdcore.h" +#include "strformat.h" // MAMEOS headers #include "strconv.h" @@ -47,6 +48,7 @@ private: bool m_is_first; // true if this is the first entry entry m_entry; // current entry's data WIN32_FIND_DATA m_data; // current raw data + std::string m_name; // converted name of directory }; //============================================================ @@ -69,8 +71,6 @@ win_directory::win_directory() win_directory::~win_directory() { // free any data associated - if (m_entry.name != nullptr) - osd_free((void *)m_entry.name); if (m_find != INVALID_HANDLE_VALUE) FindClose(m_find); } @@ -82,13 +82,6 @@ win_directory::~win_directory() const directory::entry *win_directory::read() { - // if we've previously allocated a name, free it now - if (m_entry.name != nullptr) - { - osd_free((void *)m_entry.name); - m_entry.name = nullptr; - } - // if this isn't the first file, do a find next if (!m_is_first) { @@ -98,7 +91,8 @@ const directory::entry *win_directory::read() m_is_first = false; // extract the data - m_entry.name = utf8_from_tstring(m_data.cFileName); + utf8_from_tstring(m_name, m_data.cFileName); + m_entry.name = m_name.c_str(); m_entry.type = win_attributes_to_entry_type(m_data.dwFileAttributes); m_entry.size = m_data.nFileSizeLow | (std::uint64_t(m_data.nFileSizeHigh) << 32); m_entry.last_modified = win_time_point_from_filetime(&m_data.ftLastWriteTime); @@ -114,20 +108,14 @@ bool win_directory::open_impl(std::string const &dirname) { assert(m_find == INVALID_HANDLE_VALUE); - // convert the path to TCHARs - std::unique_ptr const t_dirname(tstring_from_utf8(dirname.c_str()), &osd_free); - if (!t_dirname) - return false; - // append \*.* to the directory name - auto const dirfilter_size = _tcslen(t_dirname.get()) + 5; - std::unique_ptr dirfilter; - try { dirfilter.reset(new TCHAR[dirfilter_size]); } - catch (...) { return false; } - _sntprintf(dirfilter.get(), dirfilter_size, TEXT("%s\\*.*"), t_dirname.get()); + std::string dirfilter = string_format("%s\\*.*", dirname); + + // convert the path to TCHARs + auto t_dirfilter = tstring_from_utf8(dirfilter.c_str()); // attempt to find the first file - m_find = FindFirstFileEx(dirfilter.get(), FindExInfoStandard, &m_data, FindExSearchNameMatch, nullptr, 0); + m_find = FindFirstFileEx(t_dirfilter.c_str(), FindExInfoStandard, &m_data, FindExSearchNameMatch, nullptr, 0); return m_find != INVALID_HANDLE_VALUE; } diff --git a/src/osd/modules/file/winfile.cpp b/src/osd/modules/file/winfile.cpp index 1885925f9ee..1484caeb818 100644 --- a/src/osd/modules/file/winfile.cpp +++ b/src/osd/modules/file/winfile.cpp @@ -184,14 +184,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 - TCHAR *t_path = tstring_from_utf8(path.c_str()); - osd_disposer t_path_disposer(t_path); - if (!t_path) - return error::OUT_OF_MEMORY; - - // convert the path into something Windows compatible - for (TCHAR *src = t_path; *src != 0; src++) - *src = /* ('/' == *src) ? '\\' : */ *src; + auto t_path = tstring_from_utf8(path.c_str()); // select the file open modes DWORD disposition, access, sharemode; @@ -213,25 +206,25 @@ osd_file::error osd_file::open(std::string const &orig_path, UINT32 openflags, p } // attempt to open the file - HANDLE h = CreateFile(t_path, access, sharemode, nullptr, disposition, 0, nullptr); + HANDLE h = CreateFile(t_path.c_str(), access, sharemode, nullptr, disposition, 0, nullptr); if (INVALID_HANDLE_VALUE == h) { DWORD err = GetLastError(); // create the path if necessary if ((ERROR_PATH_NOT_FOUND == err) && (openflags & OPEN_FLAG_CREATE) && (openflags & OPEN_FLAG_CREATE_PATHS)) { - TCHAR *pathsep = _tcsrchr(t_path, '\\'); + TCHAR *pathsep = _tcsrchr(&t_path[0], '\\'); if (pathsep != nullptr) { // create the path up to the file *pathsep = 0; - err = create_path_recursive(t_path); + err = create_path_recursive(&t_path[0]); *pathsep = '\\'; // attempt to reopen the file if (err == NO_ERROR) { - h = CreateFile(t_path, access, sharemode, nullptr, disposition, 0, nullptr); + h = CreateFile(t_path.c_str(), access, sharemode, nullptr, disposition, 0, nullptr); err = GetLastError(); } } @@ -287,15 +280,12 @@ osd_file::error osd_file::openpty(ptr &file, std::string &name) osd_file::error osd_file::remove(std::string const &filename) { - TCHAR *tempstr = tstring_from_utf8(filename.c_str()); - if (!tempstr) - return error::OUT_OF_MEMORY; + auto tempstr = tstring_from_utf8(filename.c_str()); error filerr = error::NONE; - if (!DeleteFile(tempstr)) + if (!DeleteFile(tempstr.c_str())) filerr = win_error_to_file_error(GetLastError()); - osd_free(tempstr); return filerr; } @@ -309,7 +299,6 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN { DISK_GEOMETRY dg; DWORD bytesRead; - TCHAR *t_filename; HANDLE file; int result; @@ -318,11 +307,8 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN return FALSE; // do a create file on the drive - t_filename = tstring_from_utf8(filename); - if (t_filename == nullptr) - return FALSE; - file = CreateFile(t_filename, GENERIC_READ, FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, nullptr); - osd_free(t_filename); + auto t_filename = tstring_from_utf8(filename); + file = CreateFile(t_filename.c_str(), GENERIC_READ, FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, nullptr); if (file == INVALID_HANDLE_VALUE) return FALSE; @@ -358,9 +344,7 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN std::unique_ptr osd_stat(const std::string &path) { // convert the path to TCHARs - std::unique_ptr const t_path(tstring_from_utf8(path.c_str()), &osd_free); - if (!t_path) - return nullptr; + auto t_path = tstring_from_utf8(path.c_str()); // is this path a root directory (e.g. - C:)? WIN32_FIND_DATA find_data; @@ -368,13 +352,13 @@ std::unique_ptr osd_stat(const std::string &path) if (isalpha(path[0]) && (path[1] == ':') && (path[2] == '\0')) { // need to do special logic for root directories - if (!GetFileAttributesEx(t_path.get(), GetFileExInfoStandard, &find_data.dwFileAttributes)) + if (!GetFileAttributesEx(t_path.c_str(), GetFileExInfoStandard, &find_data.dwFileAttributes)) find_data.dwFileAttributes = INVALID_FILE_ATTRIBUTES; } else { // attempt to find the first file - HANDLE find = FindFirstFileEx(t_path.get(), FindExInfoStandard, &find_data, FindExSearchNameMatch, nullptr, 0); + HANDLE find = FindFirstFileEx(t_path.c_str(), FindExInfoStandard, &find_data, FindExSearchNameMatch, nullptr, 0); if (find == INVALID_HANDLE_VALUE) return nullptr; FindClose(find); @@ -404,23 +388,15 @@ std::unique_ptr 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 - TCHAR *t_path = tstring_from_utf8(path.c_str()); - osd_disposer t_path_disposer(t_path); - if (!t_path) - return osd_file::error::OUT_OF_MEMORY; + auto t_path = tstring_from_utf8(path.c_str()); // cannonicalize the path TCHAR buffer[MAX_PATH]; - if (!GetFullPathName(t_path, ARRAY_LENGTH(buffer), buffer, nullptr)) + if (!GetFullPathName(t_path.c_str(), ARRAY_LENGTH(buffer), buffer, nullptr)) return win_error_to_file_error(GetLastError()); // convert the result back to UTF-8 - char *result = utf8_from_tstring(buffer); - osd_disposer result_disposer(result); - if (!result) - return osd_file::error::OUT_OF_MEMORY; - - dst = result; + utf8_from_tstring(dst, buffer); return osd_file::error::NONE; } @@ -432,14 +408,8 @@ osd_file::error osd_get_full_path(std::string &dst, std::string const &path) bool osd_is_absolute_path(std::string const &path) { - bool result = false; - TCHAR *t_path = tstring_from_utf8(path.c_str()); - if (t_path != nullptr) - { - result = !PathIsRelative(t_path); - osd_free(t_path); - } - return result; + auto t_path = tstring_from_utf8(path.c_str()); + return !PathIsRelative(t_path.c_str()); } diff --git a/src/osd/modules/file/winptty.cpp b/src/osd/modules/file/winptty.cpp index 88920d69099..54aba00c253 100644 --- a/src/osd/modules/file/winptty.cpp +++ b/src/osd/modules/file/winptty.cpp @@ -87,12 +87,9 @@ 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) { - TCHAR *t_name = tstring_from_utf8(path.c_str()); - if (!t_name) - return osd_file::error::OUT_OF_MEMORY; + auto t_name = tstring_from_utf8(path.c_str()); - HANDLE pipe = CreateNamedPipe(t_name, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 32, 32, 0, nullptr); - osd_free(t_name); + HANDLE pipe = CreateNamedPipe(t_name.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 32, 32, 0, nullptr); if (INVALID_HANDLE_VALUE == pipe) return osd_file::error::ACCESS_DENIED; diff --git a/src/osd/modules/font/font_windows.cpp b/src/osd/modules/font/font_windows.cpp index 5dbae434854..707cb8ef5d0 100644 --- a/src/osd/modules/font/font_windows.cpp +++ b/src/osd/modules/font/font_windows.cpp @@ -85,10 +85,9 @@ 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 - TCHAR *face = tstring_from_utf8(name.c_str()); - _tcsncpy(logfont.lfFaceName, face, sizeof(logfont.lfFaceName) / sizeof(TCHAR)); + std::basic_string face = tstring_from_utf8(name.c_str()); + _tcsncpy(logfont.lfFaceName, face.c_str(), sizeof(logfont.lfFaceName) / sizeof(TCHAR)); logfont.lfFaceName[sizeof(logfont.lfFaceName) / sizeof(TCHAR)-1] = 0; - osd_free(face); // create the font height = logfont.lfHeight; @@ -111,9 +110,8 @@ bool osd_font_windows::open(std::string const &font_path, std::string const &_na } // if it doesn't match our request, fail - char *utf = utf8_from_tstring(&realname[0]); - int result = core_stricmp(utf, name.c_str()); - osd_free(utf); + std::string utf = utf8_from_tstring(&realname[0]); + int result = core_stricmp(utf.c_str(), name.c_str()); // if we didn't match, nuke our font and fall back if (result != 0) @@ -301,9 +299,8 @@ private: static int CALLBACK font_family_callback(LOGFONT const *lpelfe, TEXTMETRIC const *lpntme, DWORD FontType, LPARAM lParam) { auto &result = *reinterpret_cast > *>(lParam); - char *face = utf8_from_tstring(lpelfe->lfFaceName); - if ((*face != '@') && (result.empty() || (result.back().first != face))) result.emplace_back(face, face); - osd_free(face); + std::string face = utf8_from_tstring(lpelfe->lfFaceName); + if ((face[0] != '@') && (result.empty() || (result.back().first != face))) result.emplace_back(face, face); return TRUE; } }; diff --git a/src/osd/modules/input/input_dinput.cpp b/src/osd/modules/input/input_dinput.cpp index 061eab446e8..0db6695565d 100644 --- a/src/osd/modules/input/input_dinput.cpp +++ b/src/osd/modules/input/input_dinput.cpp @@ -260,29 +260,18 @@ public: return std::string(defstring); } - auto osd_free_deleter = [](char *p) { osd_free(p); }; - // convert the name to utf8 - auto namestring = std::unique_ptr(utf8_from_tstring(instance.tszName), osd_free_deleter); + std::string namestring = utf8_from_tstring(instance.tszName); // if no suffix, return as-is if (suffix == nullptr) - { - return std::string(namestring.get()); - } - - // otherwise, allocate space to add the suffix - auto combined = std::make_unique(strlen(namestring.get()) + 1 + _tcslen(suffix) + 1); + return namestring; // convert the suffix to utf8 - auto suffix_utf8 = std::unique_ptr(utf8_from_tstring(suffix), osd_free_deleter); + std::string suffix_utf8 = utf8_from_tstring(suffix); // Concat the name and suffix - strcpy(combined.get(), namestring.get()); - strcat(combined.get(), " "); - strcat(combined.get(), suffix_utf8.get()); - - return std::string(combined.get()); + return namestring + " " + suffix_utf8; } protected: diff --git a/src/osd/modules/input/input_dinput.h b/src/osd/modules/input/input_dinput.h index 5fc3dec30f6..d7142c35480 100644 --- a/src/osd/modules/input/input_dinput.h +++ b/src/osd/modules/input/input_dinput.h @@ -79,11 +79,10 @@ public: HRESULT result; // convert instance name to utf8 - auto osd_deleter = [](void *ptr) { osd_free(ptr); }; - auto utf8_instance_name = std::unique_ptr(utf8_from_tstring(instance->tszInstanceName), osd_deleter); + std::string utf8_instance_name = utf8_from_tstring(instance->tszInstanceName); // allocate memory for the device object - TDevice* devinfo = module.devicelist()->create_device(machine, utf8_instance_name.get(), module); + TDevice* devinfo = module.devicelist()->create_device(machine, utf8_instance_name.c_str(), module); // attempt to create a device result = m_dinput->CreateDevice(instance->guidInstance, devinfo->dinput.device.GetAddressOf(), nullptr); diff --git a/src/osd/modules/input/input_rawinput.cpp b/src/osd/modules/input/input_rawinput.cpp index f16d79cbe7a..3c617ef191a 100644 --- a/src/osd/modules/input/input_rawinput.cpp +++ b/src/osd/modules/input/input_rawinput.cpp @@ -507,10 +507,9 @@ protected: std::wstring name = rawinput_device_improve_name(tname.get()); // convert name to utf8 - auto osd_deleter = [](void *ptr) { osd_free(ptr); }; - auto utf8_name = std::unique_ptr(utf8_from_wstring(name.c_str()), osd_deleter); + std::string utf8_name = utf8_from_wstring(name.c_str()); - devinfo = devicelist()->create_device(machine, utf8_name.get(), *this); + devinfo = devicelist()->create_device(machine, utf8_name.c_str(), *this); // Add the handle devinfo->set_handle(rawinputdevice->hDevice); @@ -609,16 +608,14 @@ protected: { input_item_id itemid = table.map_di_scancode_to_itemid(keynum); TCHAR keyname[100]; - char *name; // generate the name if (GetKeyNameText(((keynum & 0x7f) << 16) | ((keynum & 0x80) << 17), keyname, ARRAY_LENGTH(keyname)) == 0) _sntprintf(keyname, ARRAY_LENGTH(keyname), TEXT("Scan%03d"), keynum); - name = utf8_from_tstring(keyname); + std::string name = utf8_from_tstring(keyname); // add the item to the device - devinfo->device()->add_item(name, itemid, generic_button_get_state, &devinfo->keyboard.state[keynum]); - osd_free(name); + devinfo->device()->add_item(name.c_str(), itemid, generic_button_get_state, &devinfo->keyboard.state[keynum]); } } }; diff --git a/src/osd/modules/lib/osdlib_win32.cpp b/src/osd/modules/lib/osdlib_win32.cpp index 015504f06dc..f69ad5050d2 100644 --- a/src/osd/modules/lib/osdlib_win32.cpp +++ b/src/osd/modules/lib/osdlib_win32.cpp @@ -251,7 +251,7 @@ void osd_break_into_debugger(const char *message) // get_clipboard_text_by_format //============================================================ -static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID data)) +static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LPCVOID data)) { char *result = nullptr; HANDLE data_handle; @@ -272,7 +272,12 @@ static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID if (data != nullptr) { // invoke the convert - result = (*convert)(data); + std::string s = (*convert)(data); + + // copy the string + result = (char *) osd_malloc(s.size() + 1); + if (result != nullptr) + memcpy(result, s.data(), s.size() * sizeof(*result)); // unlock the data GlobalUnlock(data_handle); @@ -290,7 +295,7 @@ static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID // convert_wide //============================================================ -static char *convert_wide(LPCVOID data) +static std::string convert_wide(LPCVOID data) { return utf8_from_wstring((LPCWSTR) data); } @@ -299,7 +304,7 @@ static char *convert_wide(LPCVOID data) // convert_ansi //============================================================ -static char *convert_ansi(LPCVOID data) +static std::string convert_ansi(LPCVOID data) { return utf8_from_astring((LPCSTR) data); } @@ -362,13 +367,8 @@ protected: for (auto const &library : m_libraries) { - TCHAR *tempstr = tstring_from_utf8(library.c_str()); - if (!tempstr) - return nullptr; - - HMODULE module = load_library(tempstr); - - osd_free(tempstr); + auto tempstr = tstring_from_utf8(library.c_str()); + HMODULE module = load_library(tempstr.c_str()); if (module != nullptr) { diff --git a/src/osd/modules/render/d3d/d3dhlsl.cpp b/src/osd/modules/render/d3d/d3dhlsl.cpp index 866143b3b3c..8daea6c0c33 100644 --- a/src/osd/modules/render/d3d/d3dhlsl.cpp +++ b/src/osd/modules/render/d3d/d3dhlsl.cpp @@ -2569,9 +2569,9 @@ effect::effect(shaders *shadersys, IDirect3DDevice9 *dev, const char *name, cons char name_cstr[1024]; sprintf(name_cstr, "%s\\%s", path, name); - TCHAR *effect_name = tstring_from_utf8(name_cstr); + auto effect_name = tstring_from_utf8(name_cstr); - HRESULT hr = (*shadersys->d3dx_create_effect_from_file_ptr)(dev, effect_name, nullptr, nullptr, 0, nullptr, &m_effect, &buffer_errors); + HRESULT hr = (*shadersys->d3dx_create_effect_from_file_ptr)(dev, effect_name.c_str(), nullptr, nullptr, 0, nullptr, &m_effect, &buffer_errors); if (FAILED(hr)) { if (buffer_errors != nullptr) @@ -2588,8 +2588,6 @@ effect::effect(shaders *shadersys, IDirect3DDevice9 *dev, const char *name, cons { m_valid = true; } - - osd_free(effect_name); } effect::~effect() diff --git a/src/osd/strconv.cpp b/src/osd/strconv.cpp index eec1bcc4059..88e8d4a1b8b 100644 --- a/src/osd/strconv.cpp +++ b/src/osd/strconv.cpp @@ -2,7 +2,7 @@ // copyright-holders:Aaron Giles //============================================================ // -// strconv.c - Win32 string conversion +// strconv.cpp - Win32 string conversion // //============================================================ @@ -19,23 +19,28 @@ // astring_from_utf8 //============================================================ -CHAR *astring_from_utf8(const char *utf8string) +std::string &astring_from_utf8(std::string &dst, const char *s) { - WCHAR *wstring; - int char_count; - CHAR *result; - // convert MAME string (UTF-8) to UTF-16 - char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0); - wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); - MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); + std::basic_string wstring = wstring_from_utf8(s); // convert UTF-16 to "ANSI code page" string - char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, nullptr, 0, nullptr, nullptr); - result = (CHAR *)osd_malloc_array(char_count * sizeof(*result)); - if (result != nullptr) - WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, nullptr, nullptr); + int char_count = WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), -1, nullptr, 0, nullptr, nullptr); + dst.resize(char_count - 1); + WideCharToMultiByte(CP_ACP, 0, wstring.c_str(), -1, &dst[0], char_count - 1, nullptr, nullptr); + return dst; +} + + +//============================================================ +// astring_from_utf8 +//============================================================ + +std::string astring_from_utf8(const char *s) +{ + std::string result; + astring_from_utf8(result, s); return result; } @@ -44,23 +49,26 @@ CHAR *astring_from_utf8(const char *utf8string) // utf8_from_astring //============================================================ -char *utf8_from_astring(const CHAR *astring) +std::string &utf8_from_astring(std::string &dst, const CHAR *s) { - WCHAR *wstring; - int char_count; - CHAR *result; - // convert "ANSI code page" string to UTF-16 - char_count = MultiByteToWideChar(CP_ACP, 0, astring, -1, nullptr, 0); - wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); - MultiByteToWideChar(CP_ACP, 0, astring, -1, wstring, char_count); + int char_count = MultiByteToWideChar(CP_ACP, 0, s, -1, nullptr, 0); + std::wstring wstring(char_count - 1, 0); + MultiByteToWideChar(CP_ACP, 0, s, -1, &wstring[0], char_count - 1); // convert UTF-16 to MAME string (UTF-8) - char_count = WideCharToMultiByte(CP_UTF8, 0, wstring, -1, nullptr, 0, nullptr, nullptr); - result = (CHAR *)osd_malloc_array(char_count * sizeof(*result)); - if (result != nullptr) - WideCharToMultiByte(CP_UTF8, 0, wstring, -1, result, char_count, nullptr, nullptr); + return utf8_from_wstring(dst, wstring.c_str()); +} + +//============================================================ +// utf8_from_astring +//============================================================ + +std::string utf8_from_astring(const CHAR *s) +{ + std::string result; + utf8_from_astring(result, s); return result; } @@ -69,17 +77,25 @@ char *utf8_from_astring(const CHAR *astring) // wstring_from_utf8 //============================================================ -WCHAR *wstring_from_utf8(const char *utf8string) +std::wstring &wstring_from_utf8(std::wstring &dst, const char *s) { - int char_count; - WCHAR *result; - // convert MAME string (UTF-8) to UTF-16 - char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0); - result = (WCHAR *)osd_malloc_array(char_count * sizeof(*result)); - if (result != nullptr) - MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); + int char_count = MultiByteToWideChar(CP_UTF8, 0, s, -1, nullptr, 0); + dst.resize(char_count - 1); + MultiByteToWideChar(CP_UTF8, 0, s, -1, &dst[0], char_count - 1); + return dst; +} + + +//============================================================ +// wstring_from_utf8 +//============================================================ + +std::wstring wstring_from_utf8(const char *s) +{ + std::wstring result; + wstring_from_utf8(result, s); return result; } @@ -88,20 +104,29 @@ WCHAR *wstring_from_utf8(const char *utf8string) // utf8_from_wstring //============================================================ -char *utf8_from_wstring(const WCHAR *wstring) +std::string &utf8_from_wstring(std::string &dst, const WCHAR *s) { - int char_count; - char *result; - // convert UTF-16 to MAME string (UTF-8) - char_count = WideCharToMultiByte(CP_UTF8, 0, wstring, -1, nullptr, 0, nullptr, nullptr); - result = (char *)osd_malloc_array(char_count * sizeof(*result)); - if (result != nullptr) - WideCharToMultiByte(CP_UTF8, 0, wstring, -1, result, char_count, nullptr, nullptr); + int char_count = WideCharToMultiByte(CP_UTF8, 0, s, -1, nullptr, 0, nullptr, nullptr); + dst.resize(char_count - 1); + WideCharToMultiByte(CP_UTF8, 0, s, -1, &dst[0], char_count - 1, nullptr, nullptr); + return dst; +} + + +//============================================================ +// utf8_from_wstring +//============================================================ + +std::string utf8_from_wstring(const WCHAR *s) +{ + std::string result; + utf8_from_wstring(result, s); return result; } + //============================================================ // osd_uchar_from_osdchar //============================================================ diff --git a/src/osd/strconv.h b/src/osd/strconv.h index 20ef9da6381..69aedd9841c 100644 --- a/src/osd/strconv.h +++ b/src/osd/strconv.h @@ -27,32 +27,15 @@ // the result of these functions has to be released with osd_free() -CHAR *astring_from_utf8(const char *s); -char *utf8_from_astring(const CHAR *s); +std::string astring_from_utf8(const char *s); +std::string &astring_from_utf8(std::string &dst, const char *s); +std::string utf8_from_astring(const CHAR *s); +std::string &utf8_from_astring(std::string &dst, const CHAR *s); -WCHAR *wstring_from_utf8(const char *s); -char *utf8_from_wstring(const WCHAR *s); - -struct osd_wstr_deleter -{ - void operator () (wchar_t* wstr) const - { - if (wstr != nullptr) - osd_free(wstr); - } -}; - -struct osd_str_deleter -{ - void operator () (char* str) const - { - if (str != nullptr) - osd_free(str); - } -}; - -typedef std::unique_ptr osd_unique_wstr; -typedef std::unique_ptr osd_unique_str; +std::wstring wstring_from_utf8(const char *s); +std::wstring &wstring_from_utf8(std::wstring &dst, const char *s); +std::string utf8_from_wstring(const WCHAR *s); +std::string &utf8_from_wstring(std::string &dst, const WCHAR *s); #ifdef UNICODE #define tstring_from_utf8 wstring_from_utf8 diff --git a/src/osd/windows/main.cpp b/src/osd/windows/main.cpp index f4be66a2b98..cc9f4580b30 100644 --- a/src/osd/windows/main.cpp +++ b/src/osd/windows/main.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include "strconv.h" @@ -27,29 +29,19 @@ extern int utf8_main(int argc, char *argv[]); #ifdef UNICODE extern "C" int _tmain(int argc, TCHAR **argv) { - int i, rc; - char **utf8_argv; + int i; + std::vector argv_vectors(argc); + char **utf8_argv = (char **) alloca(argc * sizeof(char *)); - /* convert arguments to UTF-8 */ - utf8_argv = (char **) malloc(argc * sizeof(*argv)); - if (utf8_argv == nullptr) - return 999; + // convert arguments to UTF-8 for (i = 0; i < argc; i++) { - utf8_argv[i] = utf8_from_tstring(argv[i]); - if (utf8_argv[i] == nullptr) - return 999; + argv_vectors[i] = utf8_from_tstring(argv[i]); + utf8_argv[i] = (char *) argv_vectors[i].c_str(); } - /* run utf8_main */ - rc = utf8_main(argc, utf8_argv); - - /* free arguments */ - for (i = 0; i < argc; i++) - osd_free(utf8_argv[i]); - free(utf8_argv); - - return rc; + // run utf8_main + return utf8_main(argc, utf8_argv); } #endif diff --git a/src/osd/windows/video.cpp b/src/osd/windows/video.cpp index 1638bfe9a6b..c38770ad719 100644 --- a/src/osd/windows/video.cpp +++ b/src/osd/windows/video.cpp @@ -125,9 +125,7 @@ void win_monitor_info::refresh() result = GetMonitorInfo(m_handle, static_cast(&m_info)); assert(result); - osd_unique_str temp(utf8_from_tstring(m_info.szDevice)); - - if (temp) m_name.assign(temp.get()); + m_name = utf8_from_tstring(m_info.szDevice); m_pos_size = RECT_to_osd_rect(m_info.rcMonitor); m_usuable_pos_size = RECT_to_osd_rect(m_info.rcWork); @@ -202,10 +200,10 @@ BOOL CALLBACK win_monitor_info::monitor_enum_callback(HMONITOR handle, HDC dc, L float aspect = static_cast(info.rcMonitor.right - info.rcMonitor.left) / static_cast(info.rcMonitor.bottom - info.rcMonitor.top); // allocate a new monitor info - osd_unique_str temp(utf8_from_tstring(info.szDevice)); + auto temp = utf8_from_tstring(info.szDevice); // copy in the data - auto monitor = std::make_shared(handle, temp.get(), aspect); + auto monitor = std::make_shared(handle, temp.c_str(), aspect); // hook us into the list osd_monitor_info::list.push_back(monitor); diff --git a/src/osd/windows/winutf8.cpp b/src/osd/windows/winutf8.cpp index 08abcb58dd8..0086ad7b383 100644 --- a/src/osd/windows/winutf8.cpp +++ b/src/osd/windows/winutf8.cpp @@ -23,12 +23,8 @@ void win_output_debug_string_utf8(const char *string) { - TCHAR *t_string = tstring_from_utf8(string); - if (t_string != nullptr) - { - OutputDebugString(t_string); - osd_free(t_string); - } + auto t_string = tstring_from_utf8(string); + OutputDebugString(t_string.c_str()); } @@ -39,32 +35,24 @@ void win_output_debug_string_utf8(const char *string) int win_message_box_utf8(HWND window, const char *text, const char *caption, UINT type) { - int result = IDNO; - TCHAR *t_text = nullptr; - TCHAR *t_caption = nullptr; + LPCTSTR t_text = nullptr; + LPCTSTR t_caption = nullptr; + std::basic_string ts_text; + std::basic_string ts_caption; if (text) { - t_text = tstring_from_utf8(text); - if (!t_text) - goto done; + ts_text = tstring_from_utf8(text); + t_text = ts_text.c_str(); } if (caption) { - t_caption = tstring_from_utf8(caption); - if (!t_caption) - goto done; + ts_caption = tstring_from_utf8(caption); + t_caption = ts_caption.c_str(); } - result = MessageBox(window, t_text, t_caption, type); - -done: - if (t_text) - osd_free(t_text); - if (t_caption) - osd_free(t_caption); - return result; + return MessageBox(window, t_text, t_caption, type); } @@ -76,13 +64,13 @@ done: BOOL win_set_window_text_utf8(HWND window, const char *text) { BOOL result = FALSE; - TCHAR *t_text = nullptr; + LPCTSTR t_text = nullptr; + std::basic_string ts_text; if (text) { - t_text = tstring_from_utf8(text); - if (!t_text) - goto done; + ts_text = tstring_from_utf8(text); + t_text = ts_text.c_str(); } #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) @@ -92,9 +80,6 @@ BOOL win_set_window_text_utf8(HWND window, const char *text) result = TRUE; #endif -done: - if (t_text) - osd_free(t_text); return result; } @@ -107,7 +92,6 @@ done: int win_get_window_text_utf8(HWND window, char *buffer, size_t buffer_size) { int result = 0; - char *utf8_buffer = nullptr; TCHAR t_buffer[256]; t_buffer[0] = '\0'; @@ -120,15 +104,10 @@ int win_get_window_text_utf8(HWND window, char *buffer, size_t buffer_size) wcsncpy(t_buffer, title->Data(), ARRAY_LENGTH(t_buffer)); #endif - utf8_buffer = utf8_from_tstring(t_buffer); - if (!utf8_buffer) - goto done; + std::string utf8_buffer = utf8_from_tstring(t_buffer); - result = snprintf(buffer, buffer_size, "%s", utf8_buffer); + result = snprintf(buffer, buffer_size, "%s", utf8_buffer.c_str()); -done: - if (utf8_buffer) - osd_free(utf8_buffer); return result; } @@ -142,28 +121,16 @@ HWND win_create_window_ex_utf8(DWORD exstyle, const char* classname, const char* int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE instance, void* param) { - TCHAR* t_classname; - TCHAR* t_windowname = nullptr; - HWND result = nullptr; + std::basic_string ts_classname = tstring_from_utf8(classname); - t_classname = tstring_from_utf8(classname); - if( !t_classname ) - return result; - - if( windowname ) { - t_windowname = tstring_from_utf8(windowname); - if( !t_windowname ) { - osd_free(t_classname); - return result; - } + LPCTSTR t_windowname = nullptr; + std::basic_string ts_windowname; + if (windowname != nullptr) + { + ts_windowname = tstring_from_utf8(windowname); + t_windowname = ts_windowname.c_str(); } - result = CreateWindowEx(exstyle, t_classname, t_windowname, style, x, y, width, height, parent, - menu, instance, param); - - if( t_windowname ) - osd_free(t_windowname); - osd_free(t_classname); - - return result; + return CreateWindowEx(exstyle, ts_classname.c_str(), t_windowname, style, x, y, width, height, parent, + menu, instance, param); } diff --git a/src/osd/windows/winutil.cpp b/src/osd/windows/winutil.cpp index 23354bf0140..5c3d216b8a1 100644 --- a/src/osd/windows/winutil.cpp +++ b/src/osd/windows/winutil.cpp @@ -106,12 +106,9 @@ void osd_subst_env(std::string &dst, const std::string &src) { TCHAR buffer[MAX_PATH]; - TCHAR *t_src = tstring_from_utf8(src.c_str()); - ExpandEnvironmentStrings(t_src, buffer, ARRAY_LENGTH(buffer)); - osd_free(t_src); - char *cnv = utf8_from_tstring(buffer); - dst = cnv; - osd_free(cnv); + auto t_src = tstring_from_utf8(src.c_str()); + ExpandEnvironmentStrings(t_src.c_str(), buffer, ARRAY_LENGTH(buffer)); + utf8_from_tstring(dst, buffer); } //-------------------------------------------------