Changeset - r5312:ffd375effb01
[Not reviewed]
master
0 2 0
Darkvater - 18 years ago 2006-12-10 11:46:43
darkvater@openttd.org
(svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
use them some more in win32 code. Also for the clipboard use the convert_from_fs
function instead of calling Win32 API directly. Make the static buffers in OTTD2FS
and FS2OTTD the same size (character-length wise)
2 files changed with 20 insertions and 14 deletions:
win32.c
14
14
0 comments (0 inline, 0 general)
win32.c
Show inline comments
 
@@ -365,12 +365,14 @@ static bool DoEmergencySave(HWND wnd)
 

	
 
static INT_PTR CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM lParam)
 
{
 
	switch (msg) {
 
		case WM_INITDIALOG: {
 
#if defined(UNICODE)
 
			/* We need to put the crash-log in a seperate buffer because the default
 
			 * buffer in MB_TO_WIDE is not large enough (256 chars) */
 
			wchar_t crash_msgW[8096];
 
#endif
 
			SetDlgItemText(wnd, 10, _crash_desc);
 
			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);
 
@@ -860,22 +862,23 @@ void ShowInfo(const char *str)
 

	
 
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 
	LPTSTR lpCmdLine, int nCmdShow)
 
{
 
	int argc;
 
	char *argv[64]; // max 64 command line arguments
 
	char *cmdline;
 

	
 
#if defined(UNICODE)
 
	/* We need to backup the command line (arguments) because the pointer
 
	 * of FS2OTTD() is only temporary */
 
	char cmdline[MAX_PATH];
 
	ttd_strlcpy(cmdline, FS2OTTD(GetCommandLine()), sizeof(cmdline));
 
#else
 
	char *cmdline = GetCommandLine();
 
	/* For UNICODE we need to convert the commandline to char* _AND_
 
	 * save it because argv[] points into this buffer and thus needs to
 
	 * be available between subsequent calls to FS2OTTD() */
 
	char cmdlinebuf[MAX_PATH];
 
#endif
 

	
 
	cmdline = WIDE_TO_MB_BUFFER(GetCommandLine(), cmdlinebuf, lengthof(cmdlinebuf));
 

	
 
#if defined(_DEBUG)
 
	CreateConsole();
 
#endif
 

	
 
	_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
 

	
 
@@ -951,26 +954,23 @@ bool InsertTextBufferClipboard(Textbuf *
 
	const char *ptr;
 

	
 
	WChar c;
 
	uint16 width, length;
 

	
 
	if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
 
		int bytec;
 
		const char *ret;
 

	
 
		OpenClipboard(NULL);
 
		cbuf = GetClipboardData(CF_UNICODETEXT);
 

	
 
		ptr = GlobalLock(cbuf);
 
		bytec = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)ptr, -1, utf8_buf, lengthof(utf8_buf), NULL, NULL);
 
		ret = convert_from_fs((wchar_t*)ptr, utf8_buf, lengthof(utf8_buf));
 
		GlobalUnlock(cbuf);
 
		CloseClipboard();
 

	
 
		if (bytec == 0) {
 
			DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", ptr, GetLastError());
 
			return false;
 
		}
 
		if (*ret == '\0') return false;
 
	} else if (IsClipboardFormatAvailable(CF_TEXT)) {
 
		OpenClipboard(NULL);
 
		cbuf = GetClipboardData(CF_TEXT);
 

	
 
		ptr = GlobalLock(cbuf);
 
		ttd_strlcpy(utf8_buf, ptr, lengthof(utf8_buf));
 
@@ -1053,13 +1053,13 @@ wchar_t *convert_to_fs(const char *name,
 
 * 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 wchar_t *OTTD2FS(const char *name)
 
{
 
	static wchar_t utf16_buf[MAX_PATH];
 
	static wchar_t utf16_buf[512];
 
	return convert_to_fs(name, utf16_buf, lengthof(utf16_buf));
 
}
 

	
 

	
 
/** Convert to OpenTTD's encoding from that of the local environment in
 
 * UNICODE. OpenTTD encoding is UTF8, local is wide-char
 
@@ -1068,13 +1068,13 @@ const wchar_t *OTTD2FS(const char *name)
 
 * @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());
 
		DEBUG(misc, 0) ("[utf8] Error converting wide-string. Errno %d", GetLastError());
 
		utf8_buf[0] = '\0';
 
	}
 

	
 
	return utf8_buf;
 
}
 

	
win32.h
Show inline comments
 
@@ -9,12 +9,18 @@ bool MyShowCursor(bool show);
 
typedef void (*Function)(int);
 
bool LoadLibraryList(Function proc[], const char *dll);
 

	
 
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
 
wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen);
 

	
 
/* Function shortcuts for UTF-8 <> UNICODE conversion. When unicode is not
 
 * defined these macros return the string passed to them, with UNICODE
 
 * they return a pointer to the converted string. The only difference between
 
 * XX_TO_YY and XX_TO_YY_BUFFER is that with the buffer variant you can
 
 * specify where to put the converted string (and how long it can be). Without
 
 * the buffer and internal buffer is used, of max 512 characters */
 
#if defined(UNICODE)
 
# define MB_TO_WIDE(str) OTTD2FS(str)
 
# define MB_TO_WIDE_BUFFER(str, buffer, buflen) convert_to_fs(str, buffer, buflen)
 
# define WIDE_TO_MB(str) FS2OTTD(str)
 
# define WIDE_TO_MB_BUFFER(str, buffer, buflen) convert_from_fs(str, buffer, buflen)
 
#else
0 comments (0 inline, 0 general)