| OLD | NEW |
| 1 // Copyright (c) 2011, 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/socket.h" | 6 #include "bin/socket.h" |
| 7 | 7 |
| 8 | 8 |
| 9 bool Platform::Initialize() { | 9 bool Platform::Initialize() { |
| 10 // Nothing to do on Windows. | 10 // Nothing to do on Windows. |
| 11 return true; | 11 return true; |
| 12 } | 12 } |
| 13 | 13 |
| 14 | 14 |
| 15 int Platform::NumberOfProcessors() { | 15 int Platform::NumberOfProcessors() { |
| 16 SYSTEM_INFO info; | 16 SYSTEM_INFO info; |
| 17 GetSystemInfo(&info); | 17 GetSystemInfo(&info); |
| 18 return info.dwNumberOfProcessors; | 18 return info.dwNumberOfProcessors; |
| 19 } | 19 } |
| 20 | 20 |
| 21 | 21 |
| 22 const char* Platform::OperatingSystem() { | 22 const char* Platform::OperatingSystem() { |
| 23 return "windows"; | 23 return "windows"; |
| 24 } | 24 } |
| 25 | 25 |
| 26 | 26 |
| 27 bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) { | 27 bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) { |
| 28 static bool socketInitialized = false; | 28 static bool socket_initialized = false; |
| 29 if (!socketInitialized) { | 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 socketInitialized = 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 char** Platform::Environment(intptr_t* count) { |
| 39 char* strings = GetEnvironmentStringsA(); |
| 40 if (strings == NULL) return NULL; |
| 41 char* tmp = strings; |
| 42 intptr_t i = 0; |
| 43 while (*tmp != '\0') { |
| 44 i++; |
| 45 tmp += (strlen(tmp) + 1); |
| 46 } |
| 47 *count = i; |
| 48 char** result = new char*[i]; |
| 49 for (intptr_t current = 0; current < i; current++) { |
| 50 result[current] = strings; |
| 51 strings += (strlen(strings) + 1); |
| 52 } |
| 53 return result; |
| 54 } |
| 55 |
| 56 |
| 38 char* Platform::StrError(int error_code) { | 57 char* Platform::StrError(int error_code) { |
| 39 static const int kBufferSize = 1024; | 58 static const int kBufferSize = 1024; |
| 40 char* error = static_cast<char*>(malloc(kBufferSize)); | 59 char* error = static_cast<char*>(malloc(kBufferSize)); |
| 41 DWORD message_size = | 60 DWORD message_size = |
| 42 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 61 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 43 NULL, | 62 NULL, |
| 44 error_code, | 63 error_code, |
| 45 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | 64 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 46 error, | 65 error, |
| 47 kBufferSize, | 66 kBufferSize, |
| 48 NULL); | 67 NULL); |
| 49 if (message_size == 0) { | 68 if (message_size == 0) { |
| 50 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | 69 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 51 fprintf(stderr, "FormatMessage failed %d\n", GetLastError()); | 70 fprintf(stderr, "FormatMessage failed %d\n", GetLastError()); |
| 52 } | 71 } |
| 53 snprintf(error, kBufferSize, "OS Error %d", error_code); | 72 snprintf(error, kBufferSize, "OS Error %d", error_code); |
| 54 } | 73 } |
| 55 // Strip out \r\n at the end of the generated message and ensure | 74 // Strip out \r\n at the end of the generated message and ensure |
| 56 // null termination. | 75 // null termination. |
| 57 if (message_size > 2 && | 76 if (message_size > 2 && |
| 58 error[message_size - 2] == '\r' && | 77 error[message_size - 2] == '\r' && |
| 59 error[message_size - 1] == '\n') { | 78 error[message_size - 1] == '\n') { |
| 60 error[message_size - 2] = '\0'; | 79 error[message_size - 2] = '\0'; |
| 61 } else { | 80 } else { |
| 62 error[kBufferSize - 1] = '\0'; | 81 error[kBufferSize - 1] = '\0'; |
| 63 } | 82 } |
| 64 return error; | 83 return error; |
| 65 } | 84 } |
| OLD | NEW |