(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)
This commit is contained in:
		
							
								
								
									
										28
									
								
								win32.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								win32.c
									
									
									
									
									
								
							@@ -368,6 +368,8 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM l
 | 
				
			|||||||
	switch (msg) {
 | 
						switch (msg) {
 | 
				
			||||||
		case WM_INITDIALOG: {
 | 
							case WM_INITDIALOG: {
 | 
				
			||||||
#if defined(UNICODE)
 | 
					#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];
 | 
								wchar_t crash_msgW[8096];
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
			SetDlgItemText(wnd, 10, _crash_desc);
 | 
								SetDlgItemText(wnd, 10, _crash_desc);
 | 
				
			||||||
@@ -863,16 +865,17 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	int argc;
 | 
						int argc;
 | 
				
			||||||
	char *argv[64]; // max 64 command line arguments
 | 
						char *argv[64]; // max 64 command line arguments
 | 
				
			||||||
 | 
						char *cmdline;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(UNICODE)
 | 
					#if defined(UNICODE)
 | 
				
			||||||
	/* We need to backup the command line (arguments) because the pointer
 | 
						/* For UNICODE we need to convert the commandline to char* _AND_
 | 
				
			||||||
	 * of FS2OTTD() is only temporary */
 | 
						 * save it because argv[] points into this buffer and thus needs to
 | 
				
			||||||
	char cmdline[MAX_PATH];
 | 
						 * be available between subsequent calls to FS2OTTD() */
 | 
				
			||||||
	ttd_strlcpy(cmdline, FS2OTTD(GetCommandLine()), sizeof(cmdline));
 | 
						char cmdlinebuf[MAX_PATH];
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
	char *cmdline = GetCommandLine();
 | 
					 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmdline = WIDE_TO_MB_BUFFER(GetCommandLine(), cmdlinebuf, lengthof(cmdlinebuf));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(_DEBUG)
 | 
					#if defined(_DEBUG)
 | 
				
			||||||
	CreateConsole();
 | 
						CreateConsole();
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -954,20 +957,17 @@ bool InsertTextBufferClipboard(Textbuf *tb)
 | 
				
			|||||||
	uint16 width, length;
 | 
						uint16 width, length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
 | 
						if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
 | 
				
			||||||
		int bytec;
 | 
							const char *ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		OpenClipboard(NULL);
 | 
							OpenClipboard(NULL);
 | 
				
			||||||
		cbuf = GetClipboardData(CF_UNICODETEXT);
 | 
							cbuf = GetClipboardData(CF_UNICODETEXT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ptr = GlobalLock(cbuf);
 | 
							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);
 | 
							GlobalUnlock(cbuf);
 | 
				
			||||||
		CloseClipboard();
 | 
							CloseClipboard();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (bytec == 0) {
 | 
							if (*ret == '\0') return false;
 | 
				
			||||||
			DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", ptr, GetLastError());
 | 
					 | 
				
			||||||
			return false;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else if (IsClipboardFormatAvailable(CF_TEXT)) {
 | 
						} else if (IsClipboardFormatAvailable(CF_TEXT)) {
 | 
				
			||||||
		OpenClipboard(NULL);
 | 
							OpenClipboard(NULL);
 | 
				
			||||||
		cbuf = GetClipboardData(CF_TEXT);
 | 
							cbuf = GetClipboardData(CF_TEXT);
 | 
				
			||||||
@@ -1056,7 +1056,7 @@ wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
 | 
				
			|||||||
 * @return pointer to the converted string; if failed string is of zero-length */
 | 
					 * @return pointer to the converted string; if failed string is of zero-length */
 | 
				
			||||||
const wchar_t *OTTD2FS(const char *name)
 | 
					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));
 | 
						return convert_to_fs(name, utf16_buf, lengthof(utf16_buf));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1071,7 +1071,7 @@ 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);
 | 
						int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, buflen, NULL, NULL);
 | 
				
			||||||
	if (len == 0) {
 | 
						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';
 | 
							utf8_buf[0] = '\0';
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								win32.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								win32.h
									
									
									
									
									
								
							@@ -12,6 +12,12 @@ bool LoadLibraryList(Function proc[], const char *dll);
 | 
				
			|||||||
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
 | 
					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);
 | 
					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)
 | 
					#if defined(UNICODE)
 | 
				
			||||||
# define MB_TO_WIDE(str) OTTD2FS(str)
 | 
					# define MB_TO_WIDE(str) OTTD2FS(str)
 | 
				
			||||||
# define MB_TO_WIDE_BUFFER(str, buffer, buflen) convert_to_fs(str, buffer, buflen)
 | 
					# define MB_TO_WIDE_BUFFER(str, buffer, buflen) convert_to_fs(str, buffer, buflen)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user