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 "bin/platform.h" | 5 #include "bin/platform.h" |
6 #include "bin/log.h" | 6 #include "bin/log.h" |
7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
8 | 8 |
9 bool Platform::Initialize() { | 9 bool Platform::Initialize() { |
10 // Nothing to do on Windows. | 10 // Nothing to do on Windows. |
(...skipping 17 matching lines...) Expand all Loading... |
28 static bool socket_initialized = false; | 28 static bool socket_initialized = false; |
29 if (!socket_initialized) { | 29 if (!socket_initialized) { |
30 // Initialize Socket for gethostname. | 30 // Initialize Socket for gethostname. |
31 if (!Socket::Initialize()) return false; | 31 if (!Socket::Initialize()) return false; |
32 socket_initialized = true; | 32 socket_initialized = true; |
33 } | 33 } |
34 return gethostname(buffer, buffer_length) == 0; | 34 return gethostname(buffer, buffer_length) == 0; |
35 } | 35 } |
36 | 36 |
37 | 37 |
38 static char* WideToUtf8(wchar_t* wide) { | |
39 int len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL); | |
40 char* utf8 = reinterpret_cast<char*>(malloc(len + 1)); | |
41 WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, len, NULL, NULL); | |
42 utf8[len] = '\0'; | |
43 return utf8; | |
44 } | |
45 | |
46 | |
47 char** Platform::Environment(intptr_t* count) { | 38 char** Platform::Environment(intptr_t* count) { |
48 wchar_t* strings = GetEnvironmentStringsW(); | 39 wchar_t* strings = GetEnvironmentStringsW(); |
49 if (strings == NULL) return NULL; | 40 if (strings == NULL) return NULL; |
50 wchar_t* tmp = strings; | 41 wchar_t* tmp = strings; |
51 intptr_t i = 0; | 42 intptr_t i = 0; |
52 while (*tmp != '\0') { | 43 while (*tmp != '\0') { |
53 i++; | 44 i++; |
54 tmp += (wcslen(tmp) + 1); | 45 tmp += (wcslen(tmp) + 1); |
55 } | 46 } |
56 *count = i; | 47 *count = i; |
57 char** result = new char*[i]; | 48 char** result = new char*[i]; |
58 tmp = strings; | 49 tmp = strings; |
59 for (intptr_t current = 0; current < i; current++) { | 50 for (intptr_t current = 0; current < i; current++) { |
60 result[current] = WideToUtf8(tmp); | 51 result[current] = StringUtils::WideToUtf8(tmp); |
61 tmp += (wcslen(tmp) + 1); | 52 tmp += (wcslen(tmp) + 1); |
62 } | 53 } |
63 FreeEnvironmentStringsW(strings); | 54 FreeEnvironmentStringsW(strings); |
64 return result; | 55 return result; |
65 } | 56 } |
66 | 57 |
67 | 58 |
68 void Platform::FreeEnvironment(char** env, int count) { | 59 void Platform::FreeEnvironment(char** env, int count) { |
69 for (int i = 0; i < count; i++) free(env[i]); | 60 for (int i = 0; i < count; i++) free(env[i]); |
70 delete[] env; | 61 delete[] env; |
71 } | 62 } |
72 | |
73 | |
74 char* Platform::StrError(int error_code) { | |
75 static const int kBufferSize = 1024; | |
76 char* error = static_cast<char*>(malloc(kBufferSize)); | |
77 DWORD message_size = | |
78 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
79 NULL, | |
80 error_code, | |
81 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
82 error, | |
83 kBufferSize, | |
84 NULL); | |
85 if (message_size == 0) { | |
86 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | |
87 Log::PrintErr("FormatMessage failed %d\n", GetLastError()); | |
88 } | |
89 snprintf(error, kBufferSize, "OS Error %d", error_code); | |
90 } | |
91 // Strip out \r\n at the end of the generated message and ensure | |
92 // null termination. | |
93 if (message_size > 2 && | |
94 error[message_size - 2] == '\r' && | |
95 error[message_size - 1] == '\n') { | |
96 error[message_size - 2] = '\0'; | |
97 } else { | |
98 error[kBufferSize - 1] = '\0'; | |
99 } | |
100 return error; | |
101 } | |
OLD | NEW |