summaryrefslogtreecommitdiff
path: root/win32.c
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2006-12-03 17:47:53 +0000
committerDarkvater <Darkvater@openttd.org>2006-12-03 17:47:53 +0000
commit9a5a37abbeba7db507ae841262339aa3a1c87523 (patch)
tree369238bf31629ca32a4d9c0963f419ff2fda78f8 /win32.c
parent32ac800be60811ca433dc59a63e8aec44e4853d3 (diff)
downloadopenttd-9a5a37abbeba7db507ae841262339aa3a1c87523.tar.xz
(svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
into a predefined buffer insted of using the static (global) buffer. This is useful if the converted value will be used later on; no need to copy it somewhere else to save it. -CodeChange: [win32] Added appropiate macros WIDE_TO_MB_BUFFER, MB_TO_WIDE_BUFFER next to existing WIDE_TO_MB and MB_TO_WIDE that only do work when UNICODE is defined, saves #ifdefs all over the place
Diffstat (limited to 'win32.c')
-rw-r--r--win32.c71
1 files changed, 46 insertions, 25 deletions
diff --git a/win32.c b/win32.c
index 7cd1648aa..6ca040cbd 100644
--- a/win32.c
+++ b/win32.c
@@ -368,14 +368,10 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM l
switch (msg) {
case WM_INITDIALOG: {
#if defined(UNICODE)
-# define crash_msg crash_msgW
- TCHAR crash_msgW[8096];
- MultiByteToWideChar(CP_ACP, 0, _crash_msg, -1, crash_msgW, lengthof(crash_msgW));
-#else
-# define crash_msg _crash_msg
+ wchar_t crash_msgW[8096];
#endif
SetDlgItemText(wnd, 10, _crash_desc);
- SetDlgItemText(wnd, 11, crash_msg);
+ SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(_crash_msg, crash_msgW, lengthof(crash_msgW)));
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
SetWndSize(wnd, -1);
} return TRUE;
@@ -917,7 +913,7 @@ void DeterminePaths(void)
_path.personal_dir = _path.game_data_dir = cfg = malloc(MAX_PATH);
GetCurrentDirectoryW(MAX_PATH - 1, path);
- WideCharToMultiByte(CP_UTF8, 0, path, -1, cfg, MAX_PATH, NULL, NULL);
+ convert_from_fs(path, cfg, MAX_PATH);
cfg[0] = toupper(cfg[0]);
s = strchr(cfg, '\0');
@@ -1037,37 +1033,62 @@ int64 GetTS(void)
return (__int64)(value * freq);
}
-/** Convert from OpenTTD's encoding to that of the local environment
- * First convert from UTF8 to wide-char, then to local
+/** Convert from OpenTTD's encoding to that of the local environment in
+ * UNICODE. OpenTTD encoding is UTF8, local is wide-char
* @param name pointer to a valid string that will be converted
- * @return pointer to a new stringbuffer that contains the converted string */
-const wchar_t *OTTD2FS(const char *name)
+ * @param utf16_buf pointer to a valid wide-char buffer that will receive the
+ * converted string
+ * @param buflen length in wide characters of the receiving buffer
+ * @return pointer to utf16_buf. If conversion fails the string is of zero-length */
+wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
{
- static wchar_t ucs2_buf[MAX_PATH];
- int len;
-
- len = MultiByteToWideChar(CP_UTF8, 0, name, -1, ucs2_buf, lengthof(ucs2_buf));
+ int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, utf16_buf, buflen);
if (len == 0) {
DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", name, GetLastError());
- return L"";
+ utf16_buf[0] = '\0';
}
- return (const wchar_t*)ucs2_buf;
+ return utf16_buf;
}
-/** Convert to OpenTTD's encoding from that of the local environment
+/** Convert from OpenTTD's encoding to that of the local environment in
+ * UNICODE. OpenTTD encoding is UTF8, local is wide-char.
+ * The returned value's contents can only be guaranteed until the next call to
+ * this function. So if the value is needed for anything else, use convert_from_fs
* @param name pointer to a valid string that will be converted
- * @return pointer to a new stringbuffer that contains the converted string */
-const char *FS2OTTD(const wchar_t *name)
+ * @return pointer to the converted string; if failed string is of zero-length */
+const wchar_t *OTTD2FS(const char *name)
{
- static char utf8_buf[512];
- int len;
+ static wchar_t utf16_buf[MAX_PATH];
+ return convert_to_fs(name, utf16_buf, lengthof(utf16_buf));
+}
+
- len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, lengthof(utf8_buf), NULL, NULL);
+/** Convert to OpenTTD's encoding from that of the local environment in
+ * UNICODE. OpenTTD encoding is UTF8, local is wide-char
+ * @param name pointer to a valid string that will be converted
+ * @param utf8_buf pointer to a valid buffer that will receive the converted string
+ * @param buflen length in characters of the receiving buffer
+ * @return pointer to utf8_buf. If conversion fails the string is of zero-length */
+char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
+{
+ int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, buflen, NULL, NULL);
if (len == 0) {
DEBUG(misc, 0) ("[utf8] Error converting string. Errno %d", GetLastError());
- return "";
+ utf8_buf[0] = '\0';
}
- return (const char*)utf8_buf;
+ return utf8_buf;
+}
+
+/** Convert to OpenTTD's encoding from that of the local environment in
+ * UNICODE. OpenTTD encoding is UTF8, local is wide-char.
+ * The returned value's contents can only be guaranteed until the next call to
+ * this function. So if the value is needed for anything else, use convert_from_fs
+ * @param name pointer to a valid string that will be converted
+ * @return pointer to the converted string; if failed string is of zero-length */
+const char *FS2OTTD(const wchar_t *name)
+{
+ static char utf8_buf[512];
+ return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
}