OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 class _Platform { |
| 8 external static int _numberOfProcessors(); |
| 9 external static String _pathSeparator(); |
| 10 external static String _operatingSystem(); |
| 11 external static _localHostname(); |
| 12 external static _executable(); |
| 13 external static _resolvedExecutable(); |
| 14 /** |
| 15 * Retrieve the entries of the process environment. |
| 16 * |
| 17 * The result is an [Iterable] of strings, where each string represents |
| 18 * an environment entry. |
| 19 * |
| 20 * Environment entries should be strings containing |
| 21 * a non-empty name and a value separated by a '=' character. |
| 22 * The name does not contain a '=' character, |
| 23 * so the name is everything up to the first '=' character. |
| 24 * Values are everything after the first '=' charcacter. |
| 25 * A value may contain further '=' characters, and it may be empty. |
| 26 * |
| 27 * Returns an [OSError] if retrieving the environment fails. |
| 28 */ |
| 29 external static _environment(); |
| 30 external static List<String> _executableArguments(); |
| 31 external static String _packageRoot(); |
| 32 external static String _packageConfig(); |
| 33 external static String _version(); |
| 34 |
| 35 static String executable = _executable(); |
| 36 static String resolvedExecutable = _resolvedExecutable(); |
| 37 static String packageRoot = _packageRoot(); |
| 38 static String packageConfig = _packageConfig(); |
| 39 |
| 40 // Cache the OS environemnt. This can be an OSError instance if |
| 41 // retrieving the environment failed. |
| 42 static var _environmentCache; |
| 43 |
| 44 static int get numberOfProcessors => _numberOfProcessors(); |
| 45 static String get pathSeparator => _pathSeparator(); |
| 46 static String get operatingSystem => _operatingSystem(); |
| 47 static Uri script; |
| 48 |
| 49 static String get localHostname { |
| 50 var result = _localHostname(); |
| 51 if (result is OSError) { |
| 52 throw result; |
| 53 } else { |
| 54 return result; |
| 55 } |
| 56 } |
| 57 |
| 58 static List<String> get executableArguments => _executableArguments(); |
| 59 |
| 60 static Map<String, String> get environment { |
| 61 if (_environmentCache == null) { |
| 62 var env = _environment(); |
| 63 if (env is !OSError) { |
| 64 var isWindows = operatingSystem == 'windows'; |
| 65 var result = isWindows ? new _CaseInsensitiveStringMap() : new Map(); |
| 66 for (var str in env) { |
| 67 // The Strings returned by [_environment()] are expected to be |
| 68 // valid environment entries, but exceptions have been seen |
| 69 // (e.g., an entry of just '=' has been seen on OS/X). |
| 70 // Invalid entries (lines without a '=' or with an empty name) |
| 71 // are discarded. |
| 72 var equalsIndex = str.indexOf('='); |
| 73 if (equalsIndex > 0) { |
| 74 result[str.substring(0, equalsIndex)] = |
| 75 str.substring(equalsIndex + 1); |
| 76 } |
| 77 } |
| 78 _environmentCache = new UnmodifiableMapView<String, String>(result); |
| 79 } else { |
| 80 _environmentCache = env; |
| 81 } |
| 82 } |
| 83 |
| 84 if (_environmentCache is OSError) { |
| 85 throw _environmentCache; |
| 86 } else { |
| 87 return _environmentCache; |
| 88 } |
| 89 } |
| 90 |
| 91 static String get version => _version(); |
| 92 } |
| 93 |
| 94 // Environment variables are case-insensitive on Windows. In order |
| 95 // to reflect that we use a case-insensitive string map on Windows. |
| 96 class _CaseInsensitiveStringMap<V> implements Map<String, V> { |
| 97 final Map<String, V> _map = new Map<String, V>(); |
| 98 |
| 99 bool containsKey(Object key) => |
| 100 key is String && _map.containsKey(key.toUpperCase()); |
| 101 bool containsValue(Object value) => _map.containsValue(value); |
| 102 V operator [](Object key) => key is String ? _map[key.toUpperCase()] : null; |
| 103 void operator []=(String key, V value) { |
| 104 _map[key.toUpperCase()] = value; |
| 105 } |
| 106 V putIfAbsent(String key, V ifAbsent()) { |
| 107 return _map.putIfAbsent(key.toUpperCase(), ifAbsent); |
| 108 } |
| 109 void addAll(Map other) { |
| 110 other.forEach((key, value) => this[key.toUpperCase()] = value); |
| 111 } |
| 112 V remove(Object key) => key is String ? _map.remove(key.toUpperCase()) : null; |
| 113 void clear() { _map.clear(); } |
| 114 void forEach(void f(String key, V value)) { _map.forEach(f); } |
| 115 Iterable<String> get keys => _map.keys; |
| 116 Iterable<V> get values => _map.values; |
| 117 int get length => _map.length; |
| 118 bool get isEmpty => _map.isEmpty; |
| 119 bool get isNotEmpty => _map.isNotEmpty; |
| 120 String toString() => _map.toString(); |
| 121 } |
OLD | NEW |