Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: runtime/bin/platform_win.cc

Issue 11529008: Fix extraction of environment variables on Windows. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/io/windows_environment_script.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
38 char** Platform::Environment(intptr_t* count) { 47 char** Platform::Environment(intptr_t* count) {
39 char* strings = GetEnvironmentStrings(); 48 wchar_t* strings = GetEnvironmentStringsW();
40 if (strings == NULL) return NULL; 49 if (strings == NULL) return NULL;
41 char* tmp = strings; 50 wchar_t* tmp = strings;
42 intptr_t i = 0; 51 intptr_t i = 0;
43 while (*tmp != '\0') { 52 while (*tmp != '\0') {
44 i++; 53 i++;
45 tmp += (strlen(tmp) + 1); 54 tmp += (wcslen(tmp) + 1);
46 } 55 }
47 *count = i; 56 *count = i;
48 char** result = new char*[i]; 57 char** result = new char*[i];
49 tmp = strings; 58 tmp = strings;
50 for (intptr_t current = 0; current < i; current++) { 59 for (intptr_t current = 0; current < i; current++) {
51 result[current] = StringUtils::SystemStringToUtf8(tmp); 60 result[current] = WideToUtf8(tmp);
52 tmp += (strlen(tmp) + 1); 61 tmp += (wcslen(tmp) + 1);
53 } 62 }
54 FreeEnvironmentStrings(strings); 63 FreeEnvironmentStringsW(strings);
55 return result; 64 return result;
56 } 65 }
57 66
67
58 void Platform::FreeEnvironment(char** env, int count) { 68 void Platform::FreeEnvironment(char** env, int count) {
59 for (int i = 0; i < count; i++) free(env[i]); 69 for (int i = 0; i < count; i++) free(env[i]);
60 delete[] env; 70 delete[] env;
61 } 71 }
62 72
63 73
64 char* Platform::StrError(int error_code) { 74 char* Platform::StrError(int error_code) {
65 static const int kBufferSize = 1024; 75 static const int kBufferSize = 1024;
66 char* error = static_cast<char*>(malloc(kBufferSize)); 76 char* error = static_cast<char*>(malloc(kBufferSize));
67 DWORD message_size = 77 DWORD message_size =
(...skipping 14 matching lines...) Expand all
82 // null termination. 92 // null termination.
83 if (message_size > 2 && 93 if (message_size > 2 &&
84 error[message_size - 2] == '\r' && 94 error[message_size - 2] == '\r' &&
85 error[message_size - 1] == '\n') { 95 error[message_size - 1] == '\n') {
86 error[message_size - 2] = '\0'; 96 error[message_size - 2] = '\0';
87 } else { 97 } else {
88 error[kBufferSize - 1] = '\0'; 98 error[kBufferSize - 1] = '\0';
89 } 99 }
90 return error; 100 return error;
91 } 101 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/windows_environment_script.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698