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

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

Issue 2786183003: [dart:io] Reland: Adds Platform.localeName (Closed)
Patch Set: Created 3 years, 8 months 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
« no previous file with comments | « runtime/bin/platform_linux.cc ('k') | runtime/bin/platform_patch.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 "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_MACOS) 6 #if defined(HOST_OS_MACOS)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <CoreFoundation/CoreFoundation.h>
11
10 #if !HOST_OS_IOS 12 #if !HOST_OS_IOS
11 #include <crt_externs.h> // NOLINT 13 #include <crt_externs.h> // NOLINT
12 #endif // !HOST_OS_IOS 14 #endif // !HOST_OS_IOS
13 #include <mach-o/dyld.h> 15 #include <mach-o/dyld.h>
14 #include <signal.h> // NOLINT 16 #include <signal.h> // NOLINT
15 #include <string.h> // NOLINT 17 #include <string.h> // NOLINT
16 #include <sys/sysctl.h> // NOLINT 18 #include <sys/sysctl.h> // NOLINT
17 #include <sys/types.h> // NOLINT 19 #include <sys/types.h> // NOLINT
18 #include <unistd.h> // NOLINT 20 #include <unistd.h> // NOLINT
19 21
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 const char* Platform::LibraryPrefix() { 93 const char* Platform::LibraryPrefix() {
92 return "lib"; 94 return "lib";
93 } 95 }
94 96
95 97
96 const char* Platform::LibraryExtension() { 98 const char* Platform::LibraryExtension() {
97 return "dylib"; 99 return "dylib";
98 } 100 }
99 101
100 102
103 static const char* GetLocaleName() {
104 CFLocaleRef locale = CFLocaleCopyCurrent();
105 CFStringRef locale_string = CFLocaleGetIdentifier(locale);
106 CFIndex len = CFStringGetLength(locale_string);
107 CFIndex max_len =
108 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1;
109 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len));
110 ASSERT(result != NULL);
111 bool success =
112 CFStringGetCString(locale_string, result, max_len, kCFStringEncodingUTF8);
113 CFRelease(locale);
114 if (!success) {
115 return NULL;
116 }
117 return result;
118 }
119
120
121 static const char* GetPreferredLanguageName() {
122 CFArrayRef languages = CFLocaleCopyPreferredLanguages();
123 CFIndex languages_length = CFArrayGetCount(languages);
124 if (languages_length < 1) {
125 CFRelease(languages);
126 return NULL;
127 }
128 CFTypeRef item =
129 reinterpret_cast<CFTypeRef>(CFArrayGetValueAtIndex(languages, 0));
130 CFTypeID item_type = CFGetTypeID(item);
131 ASSERT(item_type == CFStringGetTypeID());
132 CFStringRef language = reinterpret_cast<CFStringRef>(item);
133 CFIndex len = CFStringGetLength(language);
134 CFIndex max_len =
135 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1;
136 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len));
137 ASSERT(result != NULL);
138 bool success =
139 CFStringGetCString(language, result, max_len, kCFStringEncodingUTF8);
140 CFRelease(languages);
141 if (!success) {
142 return NULL;
143 }
144 return result;
145 }
146
147
148 const char* Platform::LocaleName() {
149 // First see if there is a preferred language. If not, return the
150 // current locale name.
151 const char* preferred_langauge = GetPreferredLanguageName();
152 return (preferred_langauge != NULL) ? preferred_langauge : GetLocaleName();
153 }
154
155
101 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { 156 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) {
102 return gethostname(buffer, buffer_length) == 0; 157 return gethostname(buffer, buffer_length) == 0;
103 } 158 }
104 159
105 160
106 char** Platform::Environment(intptr_t* count) { 161 char** Platform::Environment(intptr_t* count) {
107 #if HOST_OS_IOS 162 #if HOST_OS_IOS
108 // TODO(zra,chinmaygarde): On iOS, environment variables are seldom used. Wire 163 // TODO(zra,chinmaygarde): On iOS, environment variables are seldom used. Wire
109 // this up if someone needs it. In the meantime, we return an empty array. 164 // this up if someone needs it. In the meantime, we return an empty array.
110 char** result; 165 char** result;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 210
156 211
157 void Platform::Exit(int exit_code) { 212 void Platform::Exit(int exit_code) {
158 exit(exit_code); 213 exit(exit_code);
159 } 214 }
160 215
161 } // namespace bin 216 } // namespace bin
162 } // namespace dart 217 } // namespace dart
163 218
164 #endif // defined(HOST_OS_MACOS) 219 #endif // defined(HOST_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/platform_linux.cc ('k') | runtime/bin/platform_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698