Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <time.h> // NOLINT | 9 #include <time.h> // NOLINT |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 set_code(code); | 54 set_code(code); |
| 55 | 55 |
| 56 static const int kMaxMessageLength = 256; | 56 static const int kMaxMessageLength = 256; |
| 57 wchar_t message[kMaxMessageLength]; | 57 wchar_t message[kMaxMessageLength]; |
| 58 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 58 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
| 59 char* utf8 = StringUtils::WideToUtf8(message); | 59 char* utf8 = StringUtils::WideToUtf8(message); |
| 60 SetMessage(utf8); | 60 SetMessage(utf8); |
| 61 free(utf8); | 61 free(utf8); |
| 62 } | 62 } |
| 63 | 63 |
| 64 char* StringUtils::ConsoleStringToUtf8(char* str) { | 64 char* StringUtils::ConsoleStringToUtf8(char* str, |
| 65 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); | 65 intptr_t len, |
| 66 wchar_t* unicode = new wchar_t[len+1]; | 66 intptr_t* result_len) { |
| 67 MultiByteToWideChar(CP_ACP, 0, str, -1, unicode, len); | 67 int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); |
| 68 unicode[len] = '\0'; | 68 wchar_t* wide = new wchar_t[wide_len]; |
| 69 char* utf8 = StringUtils::WideToUtf8(unicode); | 69 MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len); |
| 70 delete[] unicode; | 70 char* utf8 = StringUtils::WideToUtf8(wide, wide_len, result_len); |
| 71 delete[] wide; | |
|
kustermann
2015/06/22 11:11:21
The extra copy is a bit unfortunate :-/
Lasse Reichstein Nielsen
2015/06/22 12:08:34
But probably unavoidable if we want to use the sys
| |
| 71 return utf8; | 72 return utf8; |
| 72 } | 73 } |
| 73 | 74 |
| 74 char* StringUtils::Utf8ToConsoleString(char* utf8) { | 75 char* StringUtils::Utf8ToConsoleString(char* utf8, |
| 75 wchar_t* unicode = Utf8ToWide(utf8); | 76 intptr_t len, |
| 76 int len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); | 77 intptr_t* result_len) { |
| 77 char* ansi = reinterpret_cast<char*>(malloc(len + 1)); | 78 intptr_t wide_len; |
| 78 WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, len, NULL, NULL); | 79 wchar_t* wide = Utf8ToWide(utf8, len, &wide_len); |
|
kustermann
2015/06/22 11:11:21
Maybe qualify with StringUtils:: as you normally d
Søren Gjesse
2015/06/23 11:17:59
Done.
| |
| 79 ansi[len] = '\0'; | 80 int system_len = WideCharToMultiByte( |
| 80 free(unicode); | 81 CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); |
| 82 char* ansi = reinterpret_cast<char*>(malloc(system_len + 1)); | |
|
kustermann
2015/06/22 11:11:21
Do we not want to handle allocation failures?
Als
Søren Gjesse
2015/06/23 11:17:59
Added a check here. However we don't handle failed
| |
| 83 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); | |
| 84 ansi[system_len] = '\0'; | |
|
Lasse Reichstein Nielsen
2015/06/22 12:08:34
Should this only be set when result_len is NULL?
Søren Gjesse
2015/06/23 11:17:59
I removed this, as when len is -1 the NUL char is
| |
| 85 free(wide); | |
| 86 if (result_len != NULL) { | |
| 87 *result_len = system_len; | |
| 88 } | |
| 81 return ansi; | 89 return ansi; |
| 82 } | 90 } |
| 83 | 91 |
| 84 char* StringUtils::WideToUtf8(wchar_t* wide) { | 92 char* StringUtils::WideToUtf8(wchar_t* wide, |
| 85 int len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL); | 93 intptr_t len, |
| 86 char* utf8 = reinterpret_cast<char*>(malloc(len + 1)); | 94 intptr_t* result_len) { |
| 87 WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, len, NULL, NULL); | 95 int utf8_len = WideCharToMultiByte( |
| 88 utf8[len] = '\0'; | 96 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); |
| 97 char* utf8 = reinterpret_cast<char*>(malloc(utf8_len + 1)); | |
| 98 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); | |
| 99 utf8[utf8_len] = '\0'; | |
|
Lasse Reichstein Nielsen
2015/06/22 12:08:34
Again, only include the extra NUL if result_len is
Søren Gjesse
2015/06/23 11:17:59
Removed, see above.
| |
| 100 if (result_len != NULL) { | |
| 101 *result_len = utf8_len; | |
| 102 } | |
| 89 return utf8; | 103 return utf8; |
| 90 } | 104 } |
| 91 | 105 |
| 92 | 106 |
| 93 wchar_t* StringUtils::Utf8ToWide(char* utf8) { | 107 wchar_t* StringUtils::Utf8ToWide(char* utf8, |
| 94 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); | 108 intptr_t len, |
| 95 wchar_t* unicode = | 109 intptr_t* result_len) { |
| 96 reinterpret_cast<wchar_t*>(malloc((len + 1) * sizeof(wchar_t))); | 110 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); |
| 97 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, unicode, len); | 111 wchar_t* wide = |
| 98 unicode[len] = '\0'; | 112 reinterpret_cast<wchar_t*>(malloc((wide_len + 1) * sizeof(wchar_t))); |
|
kustermann
2015/06/22 11:11:21
The distinction between the number-of-bytes/number
Lasse Reichstein Nielsen
2015/06/22 12:08:34
Sadly because the function is documented as return
| |
| 99 return unicode; | 113 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); |
| 114 wide[wide_len] = '\0'; | |
|
kustermann
2015/06/22 11:11:21
Is this common to 0-terminate a 2-byte wide string
Søren Gjesse
2015/06/23 11:17:59
It is, all the w versions of string functions on W
| |
| 115 if (result_len != NULL) { | |
| 116 *result_len = wide_len; | |
| 117 } | |
| 118 return wide; | |
| 100 } | 119 } |
| 101 | 120 |
| 102 const char* StringUtils::Utf8ToConsoleString(const char* utf8) { | 121 const char* StringUtils::Utf8ToConsoleString( |
| 103 return const_cast<const char*>(Utf8ToConsoleString(const_cast<char*>(utf8))); | 122 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 123 return const_cast<const char*>( | |
| 124 Utf8ToConsoleString(const_cast<char*>(utf8), len, result_len)); | |
| 104 } | 125 } |
| 105 | 126 |
| 106 const char* StringUtils::ConsoleStringToUtf8(const char* str) { | 127 const char* StringUtils::ConsoleStringToUtf8( |
| 107 return const_cast<const char*>(ConsoleStringToUtf8(const_cast<char*>(str))); | 128 const char* str, intptr_t len, intptr_t* result_len) { |
| 129 return const_cast<const char*>( | |
| 130 ConsoleStringToUtf8(const_cast<char*>(str), len, result_len)); | |
| 108 } | 131 } |
| 109 | 132 |
| 110 const char* StringUtils::WideToUtf8(const wchar_t* wide) { | 133 const char* StringUtils::WideToUtf8( |
| 111 return const_cast<const char*>(WideToUtf8(const_cast<wchar_t*>(wide))); | 134 const wchar_t* wide, intptr_t len, intptr_t* result_len) { |
| 135 return const_cast<const char*>( | |
| 136 WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); | |
| 112 } | 137 } |
| 113 | 138 |
| 114 const wchar_t* StringUtils::Utf8ToWide(const char* utf8) { | 139 const wchar_t* StringUtils::Utf8ToWide( |
| 115 return const_cast<const wchar_t*>(Utf8ToWide(const_cast<char*>(utf8))); | 140 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 141 return const_cast<const wchar_t*>( | |
| 142 Utf8ToWide(const_cast<char*>(utf8), len, result_len)); | |
| 116 } | 143 } |
| 117 | 144 |
| 118 wchar_t** ShellUtils::GetUnicodeArgv(int* argc) { | 145 wchar_t** ShellUtils::GetUnicodeArgv(int* argc) { |
| 119 wchar_t* command_line = GetCommandLineW(); | 146 wchar_t* command_line = GetCommandLineW(); |
| 120 return CommandLineToArgvW(command_line, argc); | 147 return CommandLineToArgvW(command_line, argc); |
| 121 } | 148 } |
| 122 | 149 |
| 123 void ShellUtils::FreeUnicodeArgv(wchar_t** argv) { | 150 void ShellUtils::FreeUnicodeArgv(wchar_t** argv) { |
| 124 LocalFree(argv); | 151 LocalFree(argv); |
| 125 } | 152 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 148 } | 175 } |
| 149 | 176 |
| 150 void TimerUtils::Sleep(int64_t millis) { | 177 void TimerUtils::Sleep(int64_t millis) { |
| 151 ::Sleep(millis); | 178 ::Sleep(millis); |
| 152 } | 179 } |
| 153 | 180 |
| 154 } // namespace bin | 181 } // namespace bin |
| 155 } // namespace dart | 182 } // namespace dart |
| 156 | 183 |
| 157 #endif // defined(TARGET_OS_WINDOWS) | 184 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |