diff options
author | Bert Belder <bertbelder@gmail.com> | 2011-07-22 18:28:56 +0400 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2011-07-22 23:06:51 +0400 |
commit | 7a6a77e07c99569ba64a591db36fc845b0f4349f (patch) | |
tree | 3396875caf4485871fce076d468007a5f9b60d4a /src | |
parent | 82905fa3aa7ea1e7930a760792554317c161c7cb (diff) |
Windows/cygwin: no more GetConsoleTitleW errors on XP
Diffstat (limited to 'src')
-rw-r--r-- | src/platform_cygwin.cc | 47 | ||||
-rw-r--r-- | src/platform_win32.cc | 48 |
2 files changed, 30 insertions, 65 deletions
diff --git a/src/platform_cygwin.cc b/src/platform_cygwin.cc index 590cb281b3e..b014e9546b8 100644 --- a/src/platform_cygwin.cc +++ b/src/platform_cygwin.cc @@ -64,15 +64,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) { } +// Max title length; the only thing MSDN tells us about the maximum length +// of the console title is that it is smaller than 64K. However in practice +// it is much smaller, and there is no way to figure out what the exact length +// of the title is or can be, at least not on XP. To make it even more +// annoying, GetConsoleTitle failes when the buffer to be read into is bigger +// than the actual maximum length. So we make a conservative guess here; +// just don't put the novel you're writing in the title, unless the plot +// survives truncation. +#define MAX_TITLE_LENGTH 8192 + void Platform::SetProcessTitle(char *title) { // We need to convert _title_ to UTF-16 first, because that's what windows uses internally. // It would be more efficient to use the UTF-16 value that we can obtain from v8, // but it's not accessible from here. - // Max title length; according to the specs it should be 64K but in practice it's a little over 30000, - // but who needs titles that long anyway? - const int MAX_TITLE_LENGTH = 30001; - int length; WCHAR *title_w; @@ -109,60 +115,37 @@ void Platform::SetProcessTitle(char *title) { static inline char* _getProcessTitle() { - WCHAR *title_w; + WCHAR title_w[MAX_TITLE_LENGTH]; char *title; - int length, length_w; - - length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR)); - - // If length is zero, there may be an error or the title may be empty - if (!length_w) { - if (GetLastError()) { - _winapi_perror("GetConsoleTitleW"); - return NULL; - } else { - // The title is empty, so return empty string - process_title = strdup("\0"); - return process_title; - } - } - - // Room for \0 terminator - length_w++; + int result, length; - title_w = new WCHAR[length_w]; + result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR)); - if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) { + if (result == 0) { _winapi_perror("GetConsoleTitleW"); - delete title_w; return NULL; } // Find out what the size of the buffer is that we need - length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL); + length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL); if (!length) { _winapi_perror("WideCharToMultiByte"); - delete title_w; return NULL; } title = (char *) malloc(length); if (!title) { perror("malloc"); - delete title_w; return NULL; } // Do utf16 -> utf8 conversion here if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) { _winapi_perror("WideCharToMultiByte"); - delete title_w; free(title); return NULL; } - delete title_w; - return title; } diff --git a/src/platform_win32.cc b/src/platform_win32.cc index 2a6811938d0..ce4cb4fe841 100644 --- a/src/platform_win32.cc +++ b/src/platform_win32.cc @@ -94,15 +94,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) { } +// Max title length; the only thing MSDN tells us about the maximum length +// of the console title is that it is smaller than 64K. However in practice +// it is much smaller, and there is no way to figure out what the exact length +// of the title is or can be, at least not on XP. To make it even more +// annoying, GetConsoleTitle failes when the buffer to be read into is bigger +// than the actual maximum length. So we make a conservative guess here; +// just don't put the novel you're writing in the title, unless the plot +// survives truncation. +#define MAX_TITLE_LENGTH 8192 + void Platform::SetProcessTitle(char *title) { // We need to convert _title_ to UTF-16 first, because that's what windows uses internally. // It would be more efficient to use the UTF-16 value that we can obtain from v8, // but it's not accessible from here. - // Max title length; according to the specs it should be 64K but in practice it's a little over 30000, - // but who needs titles that long anyway? - const int MAX_TITLE_LENGTH = 30001; - int length; WCHAR *title_w; @@ -139,61 +145,37 @@ void Platform::SetProcessTitle(char *title) { static inline char* _getProcessTitle() { - WCHAR *title_w; + WCHAR title_w[MAX_TITLE_LENGTH]; char *title; - int length, length_w; - - length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR)); - - // If length is zero, there may be an error or the title may be empty - if (!length_w) { - if (GetLastError()) { - winapi_perror("GetConsoleTitleW"); - return NULL; - } - else { - // The title is empty, so return empty string - process_title = strdup("\0"); - return process_title; - } - } + int result, length; - // Room for \0 terminator - length_w++; + result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR)); - title_w = new WCHAR[length_w]; - - if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) { + if (result == 0) { winapi_perror("GetConsoleTitleW"); - delete title_w; return NULL; } // Find out what the size of the buffer is that we need - length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL); + length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL); if (!length) { winapi_perror("WideCharToMultiByte"); - delete title_w; return NULL; } title = (char *) malloc(length); if (!title) { perror("malloc"); - delete title_w; return NULL; } // Do utf16 -> utf8 conversion here if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) { winapi_perror("WideCharToMultiByte"); - delete title_w; free(title); return NULL; } - delete title_w; - return title; } |