758943e32a
Map<String, String> env = new Platform().environment(); Unfortunately, this is a bit hard to test at this point. I will move on to implement support for providing an environment to processes that are started. At that point we can test it properly. Another follow-up change will be to remove the instantiation for Platform. I would like this to be just Map<String, String> env = Platform.environment(); R=sgjesse@google.com BUG=dartbug.com/752 TEST= Review URL: https://chromiumcodereview.appspot.com//10112002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6646 260f80e4-7a28-3924-810f-c04153c831b5
85 lines
2.2 KiB
C++
85 lines
2.2 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "bin/platform.h"
|
|
#include "bin/socket.h"
|
|
|
|
|
|
bool Platform::Initialize() {
|
|
// Nothing to do on Windows.
|
|
return true;
|
|
}
|
|
|
|
|
|
int Platform::NumberOfProcessors() {
|
|
SYSTEM_INFO info;
|
|
GetSystemInfo(&info);
|
|
return info.dwNumberOfProcessors;
|
|
}
|
|
|
|
|
|
const char* Platform::OperatingSystem() {
|
|
return "windows";
|
|
}
|
|
|
|
|
|
bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) {
|
|
static bool socket_initialized = false;
|
|
if (!socket_initialized) {
|
|
// Initialize Socket for gethostname.
|
|
if (!Socket::Initialize()) return false;
|
|
socket_initialized = true;
|
|
}
|
|
return gethostname(buffer, buffer_length) == 0;
|
|
}
|
|
|
|
|
|
char** Platform::Environment(intptr_t* count) {
|
|
char* strings = GetEnvironmentStringsA();
|
|
if (strings == NULL) return NULL;
|
|
char* tmp = strings;
|
|
intptr_t i = 0;
|
|
while (*tmp != '\0') {
|
|
i++;
|
|
tmp += (strlen(tmp) + 1);
|
|
}
|
|
*count = i;
|
|
char** result = new char*[i];
|
|
for (intptr_t current = 0; current < i; current++) {
|
|
result[current] = strings;
|
|
strings += (strlen(strings) + 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
char* Platform::StrError(int error_code) {
|
|
static const int kBufferSize = 1024;
|
|
char* error = static_cast<char*>(malloc(kBufferSize));
|
|
DWORD message_size =
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
NULL,
|
|
error_code,
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
error,
|
|
kBufferSize,
|
|
NULL);
|
|
if (message_size == 0) {
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
|
fprintf(stderr, "FormatMessage failed %d\n", GetLastError());
|
|
}
|
|
snprintf(error, kBufferSize, "OS Error %d", error_code);
|
|
}
|
|
// Strip out \r\n at the end of the generated message and ensure
|
|
// null termination.
|
|
if (message_size > 2 &&
|
|
error[message_size - 2] == '\r' &&
|
|
error[message_size - 1] == '\n') {
|
|
error[message_size - 2] = '\0';
|
|
} else {
|
|
error[kBufferSize - 1] = '\0';
|
|
}
|
|
return error;
|
|
}
|