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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package org.altusmetrum.altoslib_11;
import java.io.*;
import java.util.*;
import java.text.*;
public class AltosHashSet extends Hashtable<String,String> {
static private int get(StringReader reader) throws IOException {
return reader.read();
}
static public String get_token(StringReader reader) throws IOException {
int c = get(reader);
if (c == -1)
return null;
ArrayList<Integer> chars = new ArrayList<Integer>();
for (;;) {
chars.add(c);
c = get(reader);
if (c == -1 || c == ';')
break;
if (c == '\\')
c = get(reader);
}
int[] ch = new int[chars.size()];
for (int i = 0; i < ch.length; i++)
ch[i] = chars.get(i);
return new String(ch, 0, ch.length);
}
static private void put(StringWriter writer, int c) throws IOException {
writer.write(c);
}
static public void put_token(StringWriter writer, String token) throws IOException {
for (int i = 0; i < token.length(); i++) {
int c = token.codePointAt(i);
switch (c) {
case ';':
case '\\':
put(writer, '\\');
}
put(writer, c);
}
put(writer, ';');
}
public String toString() {
try {
StringWriter writer = new StringWriter();
for (String key : keySet()) {
String value = get(key);
put_token(writer, key);
put_token(writer, value);
}
return writer.toString();
} catch (IOException ie) {
return null;
}
}
public void putBoolean(String key, boolean value) {
put(key, value ? "t" : "f");
}
public boolean getBoolean(String key, boolean def) {
String value = get(key);
if (value == null)
return def;
if (value.equals("t"))
return true;
if (value.equals("f"))
return false;
return def;
}
public void putInt(String key, int value) {
put(key, Integer.toString(value));
}
public int getInt(String key, int def) {
String value = get(key);
if (value == null)
return def;
try {
return AltosParse.parse_int(value);
} catch (ParseException pe) {
return def;
}
}
public void putIntArray(String key, int value[]) {
if (value == null)
return;
StringWriter writer = new StringWriter();
try {
for (int i = 0; i < value.length; i++)
put_token(writer, Integer.toString(value[i]));
put(key, writer.toString());
} catch (IOException ie) {
}
}
public int[] getIntArray(String key, int[] def) {
String value = get(key);
if (value == null)
return def;
try {
StringReader reader = new StringReader(value);
ArrayList<Integer> array = new ArrayList<Integer>();
String elt;
while ((elt = get_token(reader)) != null)
array.add(AltosParse.parse_int(elt));
int[] ret = new int[array.size()];
for (int i = 0; i < ret.length; i++)
ret[i] = array.get(i);
return ret;
} catch (ParseException pe) {
return def;
} catch (IOException ie) {
return def;
}
}
public void putLong(String key, long value) {
put(key, Long.toString(value));
}
public long getLong(String key, long def) {
String value = get(key);
if (value == null)
return def;
try {
return AltosParse.parse_long(value);
} catch (ParseException pe) {
return def;
}
}
public void putDouble(String key, double value) {
put(key, AltosParse.format_double_net(value));
}
public double getDouble(String key, double def) {
String value = get(key);
if (value == null)
return def;
try {
return AltosParse.parse_double_net(value);
} catch (ParseException pe) {
return def;
}
}
public void putDoubleArray(String key, double value[]) {
if (value == null)
return;
StringWriter writer = new StringWriter();
try {
for (int i = 0; i < value.length; i++)
put_token(writer, AltosParse.format_double_net(value[i]));
put(key, writer.toString());
} catch (IOException ie) {
}
}
public double[] getDoubleArray(String key, double[] def) {
String value = get(key);
if (value == null)
return def;
try {
StringReader reader = new StringReader(value);
ArrayList<Double> array = new ArrayList<Double>();
String elt;
while ((elt = get_token(reader)) != null)
array.add(AltosParse.parse_double_net(elt));
double[] ret = new double[array.size()];
for (int i = 0; i < ret.length; i++)
ret[i] = array.get(i);
return ret;
} catch (ParseException pe) {
return def;
} catch (IOException ie) {
return def;
}
}
public String getString(String key, String def) {
String value = get(key);
if (value == null)
return def;
return value;
}
public void putString(String key, String value) {
if (value != null)
put(key, value);
}
public AltosHashSet getHash(String key) {
String value = get(key);
if (value == null)
return null;
try {
return new AltosHashSet(value);
} catch (IOException ie) {
return null;
}
}
public void putHash(String key, AltosHashSet h) {
put(key, h.toString());
}
public void putHashable(String key, AltosHashable h) {
if (h == null)
return;
put(key, h.hashSet().toString());
}
private AltosHashSet (String string) throws IOException {
StringReader reader = new StringReader(string);
String key, value;
for (;;) {
key = get_token(reader);
value = get_token(reader);
if (key == null || value == null)
break;
put(key, value);
}
}
public AltosHashSet() {
}
static public AltosHashSet fromString(String string) {
try {
return new AltosHashSet(string);
} catch (IOException ie) {
return null;
}
}
static public AltosHashSet[] array(String string) {
if (string == null)
return null;
try {
StringReader reader = new StringReader(string);
ArrayList<AltosHashSet> array = new ArrayList<AltosHashSet>();
String element;
while ((element = get_token(reader)) != null)
array.add(new AltosHashSet(element));
return array.toArray(new AltosHashSet[0]);
} catch (IOException ie) {
return null;
}
}
static public String toString(AltosHashSet[] sets) {
if (sets == null)
return null;
try {
StringWriter writer = new StringWriter();
for (AltosHashSet h : sets) {
String element = h.toString();
put_token(writer, element);
}
return writer.toString();
} catch (IOException ie) {
return null;
}
}
}
|