-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.java
executable file
·283 lines (228 loc) · 6.89 KB
/
Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.
package nachos.machine;
import java.util.HashMap;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.StreamTokenizer;
/**
* Provides routines to access the Nachos configuration.
*/
public final class Config {
/**
* Load configuration information from the specified file. Must be called
* before the Nachos security manager is installed.
*
* @param fileName the name of the file containing the
* configuration to use.
*/
public static void load(String fileName) {
System.out.print(" config");
Lib.assertTrue(!loaded);
loaded = true;
configFile = fileName;
try {
config = new HashMap<String, String>();
File file = new File(configFile);
Reader reader = new FileReader(file);
StreamTokenizer s = new StreamTokenizer(reader);
s.resetSyntax();
s.whitespaceChars(0x00, 0x20);
s.wordChars(0x21, 0xFF);
s.eolIsSignificant(true);
s.commentChar('#');
s.quoteChar('"');
int line = 1;
s.nextToken();
while (true) {
if (s.ttype == StreamTokenizer.TT_EOF)
break;
if (s.ttype == StreamTokenizer.TT_EOL) {
line++;
s.nextToken();
continue;
}
if (s.ttype != StreamTokenizer.TT_WORD)
loadError(line);
String key = s.sval;
if (s.nextToken() != StreamTokenizer.TT_WORD ||
!s.sval.equals("="))
loadError(line);
if (s.nextToken() != StreamTokenizer.TT_WORD && s.ttype != '"')
loadError(line);
String value = s.sval;
// ignore everything after first string
while (s.nextToken() != StreamTokenizer.TT_EOL &&
s.ttype != StreamTokenizer.TT_EOF);
if (config.get(key) != null)
loadError(line);
config.put(key, value);
line++;
}
}
catch (Throwable e) {
System.err.println("Error loading " + configFile);
System.exit(1);
}
}
private static void loadError(int line) {
System.err.println("Error in " + configFile + " line " + line);
System.exit(1);
}
private static void configError(String message) {
System.err.println("");
System.err.println("Error in " + configFile + ": " + message);
System.exit(1);
}
/**
* Get the value of a key in <tt>nachos.conf</tt>.
*
* @param key the key to look up.
* @return the value of the specified key, or <tt>null</tt> if it is not
* present.
*/
public static String getString(String key) {
return (String) config.get(key);
}
/**
* Get the value of a key in <tt>nachos.conf</tt>, returning the specified
* default if the key does not exist.
*
* @param key the key to look up.
* @param defaultValue the value to return if the key does not exist.
* @return the value of the specified key, or <tt>defaultValue</tt> if it
* is not present.
*/
public static String getString(String key, String defaultValue) {
String result = getString(key);
if (result == null)
return defaultValue;
return result;
}
private static Integer requestInteger(String key) {
try {
String value = getString(key);
if (value == null)
return null;
return new Integer(value);
}
catch (NumberFormatException e) {
configError(key + " should be an integer");
Lib.assertNotReached();
return null;
}
}
/**
* Get the value of an integer key in <tt>nachos.conf</tt>.
*
* @param key the key to look up.
* @return the value of the specified key.
*/
public static int getInteger(String key) {
Integer result = requestInteger(key);
if (result == null)
configError("missing int " + key);
return result.intValue();
}
/**
* Get the value of an integer key in <tt>nachos.conf</tt>, returning the
* specified default if the key does not exist.
*
* @param key the key to look up.
* @param defaultValue the value to return if the key does not exist.
* @return the value of the specified key, or <tt>defaultValue</tt> if the
* key does not exist.
*/
public static int getInteger(String key, int defaultValue) {
Integer result = requestInteger(key);
if (result == null)
return defaultValue;
return result.intValue();
}
private static Double requestDouble(String key) {
try {
String value = getString(key);
if (value == null)
return null;
return new Double(value);
}
catch (NumberFormatException e) {
configError(key + " should be a double");
Lib.assertNotReached();
return null;
}
}
/**
* Get the value of a double key in <tt>nachos.conf</tt>.
*
* @param key the key to look up.
* @return the value of the specified key.
*/
public static double getDouble(String key) {
Double result = requestDouble(key);
if (result == null)
configError("missing double " + key);
return result.doubleValue();
}
/**
* Get the value of a double key in <tt>nachos.conf</tt>, returning the
* specified default if the key does not exist.
*
* @param key the key to look up.
* @param defaultValue the value to return if the key does not exist.
* @return the value of the specified key, or <tt>defaultValue</tt> if the
* key does not exist.
*/
public static double getDouble(String key, double defaultValue) {
Double result = requestDouble(key);
if (result == null)
return defaultValue;
return result.doubleValue();
}
private static Boolean requestBoolean(String key) {
String value = getString(key);
if (value == null)
return null;
if (value.equals("1") || value.toLowerCase().equals("true")) {
return Boolean.TRUE;
}
else if (value.equals("0") || value.toLowerCase().equals("false")) {
return Boolean.FALSE;
}
else {
configError(key + " should be a boolean");
Lib.assertNotReached();
return null;
}
}
/**
* Get the value of a boolean key in <tt>nachos.conf</tt>.
*
* @param key the key to look up.
* @return the value of the specified key.
*/
public static boolean getBoolean(String key) {
Boolean result = requestBoolean(key);
if (result == null)
configError("missing boolean " + key);
return result.booleanValue();
}
/**
* Get the value of a boolean key in <tt>nachos.conf</tt>, returning the
* specified default if the key does not exist.
*
* @param key the key to look up.
* @param defaultValue the value to return if the key does not exist.
* @return the value of the specified key, or <tt>defaultValue</tt> if the
* key does not exist.
*/
public static boolean getBoolean(String key, boolean defaultValue) {
Boolean result = requestBoolean(key);
if (result == null)
return defaultValue;
return result.booleanValue();
}
private static boolean loaded = false;
private static String configFile;
private static HashMap<String, String> config;
}