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) { |
| 85 free(wide); |
| 86 return NULL; |
| 87 } |
| 88 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); |
| 89 free(wide); |
| 90 if (result_len != NULL) { |
| 91 *result_len = system_len; |
| 92 } |
81 return ansi; | 93 return ansi; |
82 } | 94 } |
83 | 95 |
84 char* StringUtils::WideToUtf8(wchar_t* wide) { | 96 char* StringUtilsWin::WideToUtf8(wchar_t* wide, |
85 int len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL); | 97 intptr_t len, |
86 char* utf8 = reinterpret_cast<char*>(malloc(len + 1)); | 98 intptr_t* result_len) { |
87 WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, len, NULL, NULL); | 99 // If len is -1 then WideCharToMultiByte will include the terminating |
88 utf8[len] = '\0'; | 100 // NUL byte in the length. |
| 101 int utf8_len = WideCharToMultiByte( |
| 102 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); |
| 103 char* utf8 = reinterpret_cast<char*>(malloc(utf8_len)); |
| 104 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); |
| 105 if (result_len != NULL) { |
| 106 *result_len = utf8_len; |
| 107 } |
89 return utf8; | 108 return utf8; |
90 } | 109 } |
91 | 110 |
92 | 111 |
93 wchar_t* StringUtils::Utf8ToWide(char* utf8) { | 112 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, |
94 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); | 113 intptr_t len, |
95 wchar_t* unicode = | 114 intptr_t* result_len) { |
96 reinterpret_cast<wchar_t*>(malloc((len + 1) * sizeof(wchar_t))); | 115 // If len is -1 then MultiByteToWideChar will include the terminating |
97 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, unicode, len); | 116 // NUL byte in the length. |
98 unicode[len] = '\0'; | 117 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); |
99 return unicode; | 118 wchar_t* wide = |
| 119 reinterpret_cast<wchar_t*>(malloc((wide_len) * sizeof(wchar_t))); |
| 120 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); |
| 121 if (result_len != NULL) { |
| 122 *result_len = wide_len; |
| 123 } |
| 124 return wide; |
100 } | 125 } |
101 | 126 |
102 const char* StringUtils::Utf8ToConsoleString(const char* utf8) { | 127 const char* StringUtils::Utf8ToConsoleString( |
103 return const_cast<const char*>(Utf8ToConsoleString(const_cast<char*>(utf8))); | 128 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 129 return const_cast<const char*>( |
| 130 StringUtils::Utf8ToConsoleString( |
| 131 const_cast<char*>(utf8), len, result_len)); |
104 } | 132 } |
105 | 133 |
106 const char* StringUtils::ConsoleStringToUtf8(const char* str) { | 134 const char* StringUtils::ConsoleStringToUtf8( |
107 return const_cast<const char*>(ConsoleStringToUtf8(const_cast<char*>(str))); | 135 const char* str, intptr_t len, intptr_t* result_len) { |
| 136 return const_cast<const char*>( |
| 137 StringUtils::ConsoleStringToUtf8( |
| 138 const_cast<char*>(str), len, result_len)); |
108 } | 139 } |
109 | 140 |
110 const char* StringUtils::WideToUtf8(const wchar_t* wide) { | 141 const char* StringUtilsWin::WideToUtf8( |
111 return const_cast<const char*>(WideToUtf8(const_cast<wchar_t*>(wide))); | 142 const wchar_t* wide, intptr_t len, intptr_t* result_len) { |
| 143 return const_cast<const char*>( |
| 144 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); |
112 } | 145 } |
113 | 146 |
114 const wchar_t* StringUtils::Utf8ToWide(const char* utf8) { | 147 const wchar_t* StringUtilsWin::Utf8ToWide( |
115 return const_cast<const wchar_t*>(Utf8ToWide(const_cast<char*>(utf8))); | 148 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 149 return const_cast<const wchar_t*>( |
| 150 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); |
116 } | 151 } |
117 | 152 |
118 wchar_t** ShellUtils::GetUnicodeArgv(int* argc) { | 153 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
119 wchar_t* command_line = GetCommandLineW(); | 154 wchar_t* command_line = GetCommandLineW(); |
120 return CommandLineToArgvW(command_line, argc); | 155 int unicode_argc; |
121 } | 156 wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc); |
122 | 157 if (unicode_argv == NULL) return false; |
123 void ShellUtils::FreeUnicodeArgv(wchar_t** argv) { | 158 // The argc passed to main should have the same argc as we get here. |
124 LocalFree(argv); | 159 ASSERT(argc == unicode_argc); |
| 160 if (argc < unicode_argc) { |
| 161 unicode_argc = argc; |
| 162 } |
| 163 for (int i = 0; i < unicode_argc; i++) { |
| 164 wchar_t* arg = unicode_argv[i]; |
| 165 argv[i] = StringUtilsWin::WideToUtf8(arg); |
| 166 } |
| 167 LocalFree(unicode_argv); |
| 168 return true; |
125 } | 169 } |
126 | 170 |
127 int64_t TimerUtils::GetCurrentTimeMilliseconds() { | 171 int64_t TimerUtils::GetCurrentTimeMilliseconds() { |
128 return GetCurrentTimeMicros() / 1000; | 172 return GetCurrentTimeMicros() / 1000; |
129 } | 173 } |
130 | 174 |
131 int64_t TimerUtils::GetCurrentTimeMicros() { | 175 int64_t TimerUtils::GetCurrentTimeMicros() { |
132 static const int64_t kTimeEpoc = 116444736000000000LL; | 176 static const int64_t kTimeEpoc = 116444736000000000LL; |
133 static const int64_t kTimeScaler = 10; // 100 ns to us. | 177 static const int64_t kTimeScaler = 10; // 100 ns to us. |
134 | 178 |
(...skipping 13 matching lines...) Expand all Loading... |
148 } | 192 } |
149 | 193 |
150 void TimerUtils::Sleep(int64_t millis) { | 194 void TimerUtils::Sleep(int64_t millis) { |
151 ::Sleep(millis); | 195 ::Sleep(millis); |
152 } | 196 } |
153 | 197 |
154 } // namespace bin | 198 } // namespace bin |
155 } // namespace dart | 199 } // namespace dart |
156 | 200 |
157 #endif // defined(TARGET_OS_WINDOWS) | 201 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |