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 |
11 #include "bin/log.h" | 11 #include "bin/log.h" |
12 #include "bin/utils.h" | 12 #include "bin/utils.h" |
13 #include "bin/utils_win.h" | 13 #include "bin/utils_win.h" |
14 #include "platform/assert.h" | 14 #include "platform/assert.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 namespace bin { | 17 namespace bin { |
18 | 18 |
19 void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) { | 19 void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) { |
20 DWORD message_size = | 20 DWORD message_size = FormatMessageW( |
21 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 21 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, |
22 NULL, | 22 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, buffer_length, NULL); |
23 code, | |
24 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
25 buffer, | |
26 buffer_length, | |
27 NULL); | |
28 if (message_size == 0) { | 23 if (message_size == 0) { |
29 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | 24 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
30 Log::PrintErr("FormatMessage failed for error code %d (error %d)\n", | 25 Log::PrintErr("FormatMessage failed for error code %d (error %d)\n", code, |
31 code, | |
32 GetLastError()); | 26 GetLastError()); |
33 } | 27 } |
34 _snwprintf(buffer, buffer_length, L"OS Error %d", code); | 28 _snwprintf(buffer, buffer_length, L"OS Error %d", code); |
35 } | 29 } |
36 // Ensure string termination. | 30 // Ensure string termination. |
37 buffer[buffer_length - 1] = 0; | 31 buffer[buffer_length - 1] = 0; |
38 } | 32 } |
39 | 33 |
40 | 34 |
41 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 35 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
(...skipping 30 matching lines...) Expand all Loading... |
72 char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len); | 66 char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len); |
73 return utf8; | 67 return utf8; |
74 } | 68 } |
75 | 69 |
76 | 70 |
77 char* StringUtils::Utf8ToConsoleString(char* utf8, | 71 char* StringUtils::Utf8ToConsoleString(char* utf8, |
78 intptr_t len, | 72 intptr_t len, |
79 intptr_t* result_len) { | 73 intptr_t* result_len) { |
80 intptr_t wide_len; | 74 intptr_t wide_len; |
81 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len); | 75 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len); |
82 int system_len = WideCharToMultiByte( | 76 int system_len = |
83 CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); | 77 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); |
84 char* ansi; | 78 char* ansi; |
85 ansi = | 79 ansi = |
86 reinterpret_cast<char*>(Dart_ScopeAllocate(system_len * sizeof(*ansi))); | 80 reinterpret_cast<char*>(Dart_ScopeAllocate(system_len * sizeof(*ansi))); |
87 if (ansi == NULL) { | 81 if (ansi == NULL) { |
88 return NULL; | 82 return NULL; |
89 } | 83 } |
90 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); | 84 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); |
91 if (result_len != NULL) { | 85 if (result_len != NULL) { |
92 *result_len = system_len; | 86 *result_len = system_len; |
93 } | 87 } |
94 return ansi; | 88 return ansi; |
95 } | 89 } |
96 | 90 |
97 | 91 |
98 char* StringUtilsWin::WideToUtf8(wchar_t* wide, | 92 char* StringUtilsWin::WideToUtf8(wchar_t* wide, |
99 intptr_t len, | 93 intptr_t len, |
100 intptr_t* result_len) { | 94 intptr_t* result_len) { |
101 // If len is -1 then WideCharToMultiByte will include the terminating | 95 // If len is -1 then WideCharToMultiByte will include the terminating |
102 // NUL byte in the length. | 96 // NUL byte in the length. |
103 int utf8_len = WideCharToMultiByte( | 97 int utf8_len = |
104 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); | 98 WideCharToMultiByte(CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); |
105 char* utf8; | 99 char* utf8; |
106 utf8 = reinterpret_cast<char*>(Dart_ScopeAllocate(utf8_len * sizeof(*utf8))); | 100 utf8 = reinterpret_cast<char*>(Dart_ScopeAllocate(utf8_len * sizeof(*utf8))); |
107 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); | 101 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); |
108 if (result_len != NULL) { | 102 if (result_len != NULL) { |
109 *result_len = utf8_len; | 103 *result_len = utf8_len; |
110 } | 104 } |
111 return utf8; | 105 return utf8; |
112 } | 106 } |
113 | 107 |
114 | 108 |
115 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, | 109 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, |
116 intptr_t len, | 110 intptr_t len, |
117 intptr_t* result_len) { | 111 intptr_t* result_len) { |
118 // If len is -1 then MultiByteToWideChar will include the terminating | 112 // If len is -1 then MultiByteToWideChar will include the terminating |
119 // NUL byte in the length. | 113 // NUL byte in the length. |
120 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); | 114 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); |
121 wchar_t* wide; | 115 wchar_t* wide; |
122 wide = | 116 wide = |
123 reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide))); | 117 reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide))); |
124 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); | 118 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); |
125 if (result_len != NULL) { | 119 if (result_len != NULL) { |
126 *result_len = wide_len; | 120 *result_len = wide_len; |
127 } | 121 } |
128 return wide; | 122 return wide; |
129 } | 123 } |
130 | 124 |
131 | 125 |
132 const char* StringUtils::Utf8ToConsoleString( | 126 const char* StringUtils::Utf8ToConsoleString(const char* utf8, |
133 const char* utf8, intptr_t len, intptr_t* result_len) { | 127 intptr_t len, |
134 return const_cast<const char*>( | 128 intptr_t* result_len) { |
135 StringUtils::Utf8ToConsoleString( | 129 return const_cast<const char*>(StringUtils::Utf8ToConsoleString( |
136 const_cast<char*>(utf8), len, result_len)); | 130 const_cast<char*>(utf8), len, result_len)); |
137 } | 131 } |
138 | 132 |
139 | 133 |
140 const char* StringUtils::ConsoleStringToUtf8( | 134 const char* StringUtils::ConsoleStringToUtf8(const char* str, |
141 const char* str, intptr_t len, intptr_t* result_len) { | 135 intptr_t len, |
142 return const_cast<const char*>( | 136 intptr_t* result_len) { |
143 StringUtils::ConsoleStringToUtf8( | 137 return const_cast<const char*>(StringUtils::ConsoleStringToUtf8( |
144 const_cast<char*>(str), len, result_len)); | 138 const_cast<char*>(str), len, result_len)); |
145 } | 139 } |
146 | 140 |
147 | 141 |
148 const char* StringUtilsWin::WideToUtf8( | 142 const char* StringUtilsWin::WideToUtf8(const wchar_t* wide, |
149 const wchar_t* wide, intptr_t len, intptr_t* result_len) { | 143 intptr_t len, |
| 144 intptr_t* result_len) { |
150 return const_cast<const char*>( | 145 return const_cast<const char*>( |
151 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); | 146 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); |
152 } | 147 } |
153 | 148 |
154 | 149 |
155 const wchar_t* StringUtilsWin::Utf8ToWide( | 150 const wchar_t* StringUtilsWin::Utf8ToWide(const char* utf8, |
156 const char* utf8, intptr_t len, intptr_t* result_len) { | 151 intptr_t len, |
| 152 intptr_t* result_len) { |
157 return const_cast<const wchar_t*>( | 153 return const_cast<const wchar_t*>( |
158 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); | 154 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); |
159 } | 155 } |
160 | 156 |
161 | 157 |
162 char* StringUtils::StrNDup(const char* s, intptr_t n) { | 158 char* StringUtils::StrNDup(const char* s, intptr_t n) { |
163 intptr_t len = strlen(s); | 159 intptr_t len = strlen(s); |
164 if ((n < 0) || (len < 0)) { | 160 if ((n < 0) || (len < 0)) { |
165 return NULL; | 161 return NULL; |
166 } | 162 } |
(...skipping 16 matching lines...) Expand all Loading... |
183 if (unicode_argv == NULL) { | 179 if (unicode_argv == NULL) { |
184 return false; | 180 return false; |
185 } | 181 } |
186 // The argc passed to main should have the same argc as we get here. | 182 // The argc passed to main should have the same argc as we get here. |
187 ASSERT(argc == unicode_argc); | 183 ASSERT(argc == unicode_argc); |
188 if (argc < unicode_argc) { | 184 if (argc < unicode_argc) { |
189 unicode_argc = argc; | 185 unicode_argc = argc; |
190 } | 186 } |
191 for (int i = 0; i < unicode_argc; i++) { | 187 for (int i = 0; i < unicode_argc; i++) { |
192 wchar_t* arg = unicode_argv[i]; | 188 wchar_t* arg = unicode_argv[i]; |
193 int arg_len = | 189 int arg_len = WideCharToMultiByte(CP_UTF8, 0, arg, -1, NULL, 0, NULL, NULL); |
194 WideCharToMultiByte(CP_UTF8, 0, arg, -1, NULL, 0, NULL, NULL); | |
195 char* utf8_arg = reinterpret_cast<char*>(malloc(arg_len)); | 190 char* utf8_arg = reinterpret_cast<char*>(malloc(arg_len)); |
196 WideCharToMultiByte(CP_UTF8, 0, arg, -1, utf8_arg, arg_len, NULL, NULL); | 191 WideCharToMultiByte(CP_UTF8, 0, arg, -1, utf8_arg, arg_len, NULL, NULL); |
197 argv[i] = utf8_arg; | 192 argv[i] = utf8_arg; |
198 } | 193 } |
199 LocalFree(unicode_argv); | 194 LocalFree(unicode_argv); |
200 return true; | 195 return true; |
201 } | 196 } |
202 | 197 |
203 | 198 |
204 // Although win32 uses 64-bit integers for representing timestamps, | 199 // Although win32 uses 64-bit integers for representing timestamps, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 254 |
260 | 255 |
261 void TimerUtils::Sleep(int64_t millis) { | 256 void TimerUtils::Sleep(int64_t millis) { |
262 ::Sleep(millis); | 257 ::Sleep(millis); |
263 } | 258 } |
264 | 259 |
265 } // namespace bin | 260 } // namespace bin |
266 } // namespace dart | 261 } // namespace dart |
267 | 262 |
268 #endif // defined(TARGET_OS_WINDOWS) | 263 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |