-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.java
More file actions
207 lines (182 loc) · 5.29 KB
/
Config.java
File metadata and controls
207 lines (182 loc) · 5.29 KB
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
import java.util.*;
import java.io.*;
import java.awt.event.*;
/** @author Gergely Kota
Config provides an interface for reading and writing from
the options.cfg file by keys and values.
*/
public class Config
{
private static final ArrayList listeners = new ArrayList();
private static HashMap strings;
private static HashMap descriptions;
/** INPUT is the File from which options are read */
public static final File INPUT = new File("data/options.cfg");
public static final File DESC = new File("data/options.help");
/** ESCAPE is the sequence that comments the line */
public static final String ESCAPE = "//";
/** ILLEGAL is the return for a non-existent int */
public static final int ILLEGAL = Integer.MIN_VALUE;
// read in key/value pairs on startup
static
{
strings = init(INPUT);
descriptions = init(DESC);
}
public static void addActionListener(ActionListener al)
{
listeners.add(al);
}
private static void notifyListeners(String source)
{
ActionEvent ae = new ActionEvent(source, 0, "write");
for(int i = 0; i < listeners.size(); i++)
((ActionListener)listeners.get(i)).actionPerformed(ae);
}
// init reads the config file and sets up all key/value pairs
// in a HashMap. NOTE: All keys are mapped to lowercase for
// case insensitivity
private static HashMap init(File f)
{
HashMap temp = new HashMap();
try
{
// create a reader
BufferedReader br = new BufferedReader(new FileReader(f));
while(br.ready())
{
// try each line of the config file
try
{
// read a line
// ignore if it starts with "//"
// pull key off the front
// the rest is the value
// add value hashed by key
String s = br.readLine().trim();
if(s.startsWith(ESCAPE))
continue;
StringTokenizer st = new StringTokenizer(s);
String key = st.nextToken().toLowerCase();
String value = "";
while(st.hasMoreTokens())
value += " " + st.nextToken();
if(key != null)
temp.put(key, value.trim());
}
// if there are any problems, ignore this line
catch(Exception e) {}
}
}
// failed reader results in no key/value pairs.
catch(Exception e) {}
return temp;
}
/** @param key the String by which the lookup occurs
@param value the String to associate with the key
@return true if the write succeeded
write allows a program to add or change information in the
config file. If the key is already used, the associated value
is replaced. If the key is new, the new key/value pair is added
to the end of the config file. In either case, the config file
is then reread to reflect the changes immediately.
*/
public static boolean write(String key, String value)
{
if(key == null || value == null)
return false;
key = key.trim();
if(key.equals("//"))
return false;
boolean worked = true;
boolean replaced = false;
StringBuffer sb = new StringBuffer();
try
{
BufferedReader br = new BufferedReader(new FileReader(INPUT));
while(br.ready())
{
// read one line at a time
// for each, check if it uses the passed key
// if so, replace its value, else keep it as is
String temp = br.readLine();
StringTokenizer st = new StringTokenizer(temp);
if(st.hasMoreTokens())
if(key.equals(st.nextToken()))
{
temp = key + " " + value;
replaced = true;
}
sb.append(temp).append("\n");
}
if(!replaced)
sb.append(key).append(" ").append(value).append("\n");
}
// make a note in case of any failure
catch(Exception e) {worked = false;}
// sb now holds the new contents of the file.
// if there was a failure, do not write the file (to avoid corruption or loss)
if(!worked)
return false;
try
{
// write the contents of the new file to the config file
// reread the contents of the file to reflect the changes
PrintWriter pw = new PrintWriter(new FileWriter(INPUT));
pw.print(sb.toString());
pw.close();
strings = init(INPUT);
notifyListeners(key);
return true;
}
catch(Exception e) {return false;}
}
/** @param key the String by which the lookup occurs
@return the int value for that key
returns the value associated with the argument key as an int
in case of failure, ILLEGAL is returned
*/
public static int getInt(String key)
{
try
{
return Integer.parseInt(read(key));
}
catch(Exception e) {return ILLEGAL;}
}
// no instances exist.
private Config() {}
/** @param the key to look up a value for
@return the value associated with that key
read a value associated with a key from the config file
*/
public static String read(String key)
{
if(strings == null)
return null;
return (String) strings.get(key.toLowerCase());
}
public static String readDescription(String key)
{
if(descriptions == null)
return null;
return (String) descriptions.get(key.toLowerCase());
}
public static String[] getKeys()
{
Object[] o = strings.keySet().toArray();
String[] s = new String[o.length];
for(int i = 0; i < o.length; i++)
s[i] = (String) o[i];
return s;
}
/* -------------------------------------------- */
/* -------------------------------------------- */
/* -------------------------------------------- */
public static void main(String[] ahrs)
{
Debug.println(read("edgesize"));
Debug.println(write("edgesize", "3"));
Debug.println(read("edgesize"));
}
}