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/utils.h" | 11 #include "bin/utils.h" |
12 #include "bin/utils_win.h" | 12 #include "bin/utils_win.h" |
13 #include "bin/log.h" | 13 #include "bin/log.h" |
14 #include "platform/assert.h" | |
14 | 15 |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 namespace bin { | 18 namespace bin { |
18 | 19 |
19 void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) { | 20 void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) { |
20 DWORD message_size = | 21 DWORD message_size = |
21 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 22 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
22 NULL, | 23 NULL, |
23 code, | 24 code, |
(...skipping 13 matching lines...) Expand all Loading... | |
37 buffer[buffer_length - 1] = 0; | 38 buffer[buffer_length - 1] = 0; |
38 } | 39 } |
39 | 40 |
40 | 41 |
41 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 42 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
42 set_code(GetLastError()); | 43 set_code(GetLastError()); |
43 | 44 |
44 static const int kMaxMessageLength = 256; | 45 static const int kMaxMessageLength = 256; |
45 wchar_t message[kMaxMessageLength]; | 46 wchar_t message[kMaxMessageLength]; |
46 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 47 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
47 char* utf8 = StringUtils::WideToUtf8(message); | 48 char* utf8 = StringUtilsWin::WideToUtf8(message); |
48 SetMessage(utf8); | 49 SetMessage(utf8); |
49 free(utf8); | 50 free(utf8); |
50 } | 51 } |
51 | 52 |
52 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 53 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
53 set_sub_system(sub_system); | 54 set_sub_system(sub_system); |
54 set_code(code); | 55 set_code(code); |
55 | 56 |
56 static const int kMaxMessageLength = 256; | 57 static const int kMaxMessageLength = 256; |
57 wchar_t message[kMaxMessageLength]; | 58 wchar_t message[kMaxMessageLength]; |
58 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 59 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
59 char* utf8 = StringUtils::WideToUtf8(message); | 60 char* utf8 = StringUtilsWin::WideToUtf8(message); |
60 SetMessage(utf8); | 61 SetMessage(utf8); |
61 free(utf8); | 62 free(utf8); |
62 } | 63 } |
63 | 64 |
64 char* StringUtils::ConsoleStringToUtf8(char* str) { | 65 char* StringUtils::ConsoleStringToUtf8(char* str, |
65 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); | 66 intptr_t len, |
66 wchar_t* unicode = new wchar_t[len+1]; | 67 intptr_t* result_len) { |
67 MultiByteToWideChar(CP_ACP, 0, str, -1, unicode, len); | 68 int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); |
68 unicode[len] = '\0'; | 69 wchar_t* wide = new wchar_t[wide_len]; |
69 char* utf8 = StringUtils::WideToUtf8(unicode); | 70 MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len); |
70 delete[] unicode; | 71 char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len); |
72 delete[] wide; | |
71 return utf8; | 73 return utf8; |
72 } | 74 } |
73 | 75 |
74 char* StringUtils::Utf8ToConsoleString(char* utf8) { | 76 char* StringUtils::Utf8ToConsoleString(char* utf8, |
75 wchar_t* unicode = Utf8ToWide(utf8); | 77 intptr_t len, |
76 int len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); | 78 intptr_t* result_len) { |
77 char* ansi = reinterpret_cast<char*>(malloc(len + 1)); | 79 intptr_t wide_len; |
78 WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, len, NULL, NULL); | 80 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len); |
79 ansi[len] = '\0'; | 81 int system_len = WideCharToMultiByte( |
80 free(unicode); | 82 CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); |
83 char* ansi = reinterpret_cast<char*>(malloc(system_len)); | |
84 if (ansi == NULL) return NULL; | |
kustermann
2015/06/23 12:03:58
This early return is not doing "free(wide)"
Søren Gjesse
2015/06/23 12:13:36
Done.
| |
85 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); | |
86 free(wide); | |
87 if (result_len != NULL) { | |
88 *result_len = system_len; | |
89 } | |
81 return ansi; | 90 return ansi; |
82 } | 91 } |
83 | 92 |
84 char* StringUtils::WideToUtf8(wchar_t* wide) { | 93 char* StringUtilsWin::WideToUtf8(wchar_t* wide, |
85 int len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL); | 94 intptr_t len, |
86 char* utf8 = reinterpret_cast<char*>(malloc(len + 1)); | 95 intptr_t* result_len) { |
87 WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, len, NULL, NULL); | 96 // If len is -1 then WideCharToMultiByte will include the terminating |
88 utf8[len] = '\0'; | 97 // NUL byte in the length. |
98 int utf8_len = WideCharToMultiByte( | |
99 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); | |
100 char* utf8 = reinterpret_cast<char*>(malloc(utf8_len)); | |
101 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); | |
102 if (result_len != NULL) { | |
103 *result_len = utf8_len; | |
104 } | |
89 return utf8; | 105 return utf8; |
90 } | 106 } |
91 | 107 |
92 | 108 |
93 wchar_t* StringUtils::Utf8ToWide(char* utf8) { | 109 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, |
94 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); | 110 intptr_t len, |
95 wchar_t* unicode = | 111 intptr_t* result_len) { |
96 reinterpret_cast<wchar_t*>(malloc((len + 1) * sizeof(wchar_t))); | 112 // If len is -1 then MultiByteToWideChar will include the terminating |
97 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, unicode, len); | 113 // NUL byte in the length. |
98 unicode[len] = '\0'; | 114 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); |
99 return unicode; | 115 wchar_t* wide = |
116 reinterpret_cast<wchar_t*>(malloc((wide_len) * sizeof(wchar_t))); | |
117 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); | |
118 if (result_len != NULL) { | |
119 *result_len = wide_len; | |
120 } | |
121 return wide; | |
100 } | 122 } |
101 | 123 |
102 const char* StringUtils::Utf8ToConsoleString(const char* utf8) { | 124 const char* StringUtils::Utf8ToConsoleString( |
103 return const_cast<const char*>(Utf8ToConsoleString(const_cast<char*>(utf8))); | 125 const char* utf8, intptr_t len, intptr_t* result_len) { |
126 return const_cast<const char*>( | |
127 StringUtils::Utf8ToConsoleString( | |
128 const_cast<char*>(utf8), len, result_len)); | |
104 } | 129 } |
105 | 130 |
106 const char* StringUtils::ConsoleStringToUtf8(const char* str) { | 131 const char* StringUtils::ConsoleStringToUtf8( |
107 return const_cast<const char*>(ConsoleStringToUtf8(const_cast<char*>(str))); | 132 const char* str, intptr_t len, intptr_t* result_len) { |
133 return const_cast<const char*>( | |
134 StringUtils::ConsoleStringToUtf8( | |
135 const_cast<char*>(str), len, result_len)); | |
108 } | 136 } |
109 | 137 |
110 const char* StringUtils::WideToUtf8(const wchar_t* wide) { | 138 const char* StringUtilsWin::WideToUtf8( |
111 return const_cast<const char*>(WideToUtf8(const_cast<wchar_t*>(wide))); | 139 const wchar_t* wide, intptr_t len, intptr_t* result_len) { |
140 return const_cast<const char*>( | |
141 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); | |
112 } | 142 } |
113 | 143 |
114 const wchar_t* StringUtils::Utf8ToWide(const char* utf8) { | 144 const wchar_t* StringUtilsWin::Utf8ToWide( |
115 return const_cast<const wchar_t*>(Utf8ToWide(const_cast<char*>(utf8))); | 145 const char* utf8, intptr_t len, intptr_t* result_len) { |
146 return const_cast<const wchar_t*>( | |
147 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); | |
116 } | 148 } |
117 | 149 |
118 wchar_t** ShellUtils::GetUnicodeArgv(int* argc) { | 150 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
119 wchar_t* command_line = GetCommandLineW(); | 151 wchar_t* command_line = GetCommandLineW(); |
120 return CommandLineToArgvW(command_line, argc); | 152 int unicode_argc; |
121 } | 153 wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc); |
122 | 154 if (unicode_argv == NULL) return false; |
123 void ShellUtils::FreeUnicodeArgv(wchar_t** argv) { | 155 // The argc passed to main should have the same argc as we get here. |
124 LocalFree(argv); | 156 ASSERT(argc == unicode_argc); |
157 if (argc < unicode_argc) { | |
158 unicode_argc = argc; | |
159 } | |
160 for (int i = 0; i < unicode_argc; i++) { | |
161 wchar_t* arg = unicode_argv[i]; | |
162 argv[i] = StringUtilsWin::WideToUtf8(arg); | |
163 } | |
164 LocalFree(unicode_argv); | |
165 return true; | |
125 } | 166 } |
126 | 167 |
127 int64_t TimerUtils::GetCurrentTimeMilliseconds() { | 168 int64_t TimerUtils::GetCurrentTimeMilliseconds() { |
128 return GetCurrentTimeMicros() / 1000; | 169 return GetCurrentTimeMicros() / 1000; |
129 } | 170 } |
130 | 171 |
131 int64_t TimerUtils::GetCurrentTimeMicros() { | 172 int64_t TimerUtils::GetCurrentTimeMicros() { |
132 static const int64_t kTimeEpoc = 116444736000000000LL; | 173 static const int64_t kTimeEpoc = 116444736000000000LL; |
133 static const int64_t kTimeScaler = 10; // 100 ns to us. | 174 static const int64_t kTimeScaler = 10; // 100 ns to us. |
134 | 175 |
(...skipping 13 matching lines...) Expand all Loading... | |
148 } | 189 } |
149 | 190 |
150 void TimerUtils::Sleep(int64_t millis) { | 191 void TimerUtils::Sleep(int64_t millis) { |
151 ::Sleep(millis); | 192 ::Sleep(millis); |
152 } | 193 } |
153 | 194 |
154 } // namespace bin | 195 } // namespace bin |
155 } // namespace dart | 196 } // namespace dart |
156 | 197 |
157 #endif // defined(TARGET_OS_WINDOWS) | 198 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |