1
2
3
4
5
6 using System.Collections;
7
8 namespace System
9 {
10 private char** environment = null;
11
12 public class Environment
13 {
14 static Environment() : instance(new Environment())
15 {
16 }
17 public Environment() : variables()
18 {
19 if (environment != null)
20 {
21 const char** envp = environment;
22 while (*envp != null)
23 {
24 string envVar = *envp;
25 long eqPos = envVar.Find('=');
26 if (eqPos != -1)
27 {
28 Set(envVar.Substring(0, eqPos), envVar.Substring(eqPos + 1));
29 }
30 ++envp;
31 }
32 }
33 }
34 public static nothrow Environment& Instance()
35 {
36 return *instance;
37 }
38 public nothrow bool Has(const string& envVarName) const
39 {
40 return variables.CFind(envVarName) != variables.CEnd();
41 }
42 public nothrow string Get(const string& envVarName) const
43 {
44 Map<string, string>.ConstIterator it = variables.CFind(envVarName);
45 if (it != variables.CEnd())
46 {
47 return it->second;
48 }
49 else
50 {
51 return string();
52 }
53 }
54 public void Set(const string& envVarName, const string& envVarValue)
55 {
56 if (envVarName.Find('=') != -1)
57 {
58 throw SystemError(EPARAM, "environment variable name cannot contain '=' character");
59 }
60 variables[envVarName] = envVarValue;
61 }
62 public nothrow void Remove(const string& envVarName)
63 {
64 variables.Remove(envVarName);
65 }
66 public nothrow void Clear()
67 {
68 variables.Clear();
69 }
70 public void CopyTo(Environment& that)
71 {
72 for (const Pair<string, string>& v : variables)
73 {
74 that.Set(v.first, v.second);
75 }
76 }
77 public nothrow long Size() const
78 {
79 long size = 0;
80 for (const Pair<string, string>& v : variables)
81 {
82 size = size + v.first.Length() + 1 + v.second.Length() + 1 + 16;
83 }
84 return size;
85 }
86 public nothrow const Map<string, string>& Variables() const
87 {
88 return variables;
89 }
90 private static UniquePtr<Environment> instance;
91 private Map<string, string> variables;
92 }
93
94 public nothrow bool HasEnv(const string& envVarName)
95 {
96 return Environment.Instance().Has(envVarName);
97 }
98
99 public nothrow string GetEnv(const string& envVarName)
100 {
101 return Environment.Instance().Get(envVarName);
102 }
103
104 public void SetEnv(const string& envVarName, const string& envVarValue)
105 {
106 Environment.Instance().Set(envVarName, envVarValue);
107 }
108
109 public nothrow void RemoveEnv(const string& envVarName)
110 {
111 Environment.Instance().Remove(envVarName);
112 }
113
114 public nothrow void ClearEnvironment()
115 {
116 Environment.Instance().Clear();
117 }
118
119 public cdecl nothrow void StartupSetupEnvironment(const char** envp)
120 {
121 environment = envp;
122 }
123 }