altoslib: Store saved state in version-independent format
[fw/altos] / altoslib / AltosHashSet.java
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_11;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosHashSet extends Hashtable<String,String> {
25         static private int get(StringReader reader) throws IOException {
26                 return reader.read();
27         }
28
29         static public String get_token(StringReader reader) throws IOException {
30                 int     c = get(reader);
31
32                 if (c == -1)
33                         return null;
34
35                 ArrayList<Integer>      chars = new ArrayList<Integer>();
36
37                 for (;;) {
38                         chars.add(c);
39                         c = get(reader);
40                         if (c == -1 || c == ';')
41                                 break;
42                         if (c == '\\')
43                                 c = get(reader);
44                 }
45                 int[] ch = new int[chars.size()];
46                 for (int i = 0; i < ch.length; i++)
47                         ch[i] = chars.get(i);
48                 return new String(ch, 0, ch.length);
49         }
50
51         static private void put(StringWriter writer, int c) throws IOException {
52                 writer.write(c);
53         }
54
55         static public void put_token(StringWriter writer, String token) throws IOException {
56                 for (int i = 0; i < token.length(); i++) {
57                         int c = token.codePointAt(i);
58
59                         switch (c) {
60                         case ';':
61                         case '\\':
62                                 put(writer, '\\');
63                         }
64                         put(writer, c);
65                 }
66                 put(writer, ';');
67         }
68
69         public String toString() {
70                 try {
71                         StringWriter writer = new StringWriter();
72
73                         for (String key : keySet()) {
74                                 String value = get(key);
75                                 put_token(writer, key);
76                                 put_token(writer, value);
77                         }
78                         return writer.toString();
79                 } catch (IOException ie) {
80                         return null;
81                 }
82         }
83
84         public void putBoolean(String key, boolean value) {
85                 put(key, value ? "t" : "f");
86         }
87
88         public boolean getBoolean(String key, boolean def) {
89                 String  value = get(key);
90
91                 if (value == null)
92                         return def;
93                 if (value.equals("t"))
94                         return true;
95                 if (value.equals("f"))
96                         return false;
97                 return def;
98         }
99
100         public void putInt(String key, int value) {
101                 put(key, Integer.toString(value));
102         }
103
104         public int getInt(String key, int def) {
105                 String  value = get(key);
106
107                 if (value == null)
108                         return def;
109                 try {
110                         return AltosParse.parse_int(value);
111                 } catch (ParseException pe) {
112                         return def;
113                 }
114         }
115
116         public void putIntArray(String key, int value[]) {
117                 if (value == null)
118                         return;
119
120                 StringWriter    writer = new StringWriter();
121
122                 try {
123                         for (int i = 0; i < value.length; i++)
124                                 put_token(writer, Integer.toString(value[i]));
125                         put(key, writer.toString());
126                 } catch (IOException ie) {
127                 }
128         }
129
130         public int[] getIntArray(String key, int[] def) {
131                 String          value = get(key);
132
133                 if (value == null)
134                         return def;
135                 try {
136                         StringReader            reader = new StringReader(value);
137                         ArrayList<Integer>      array = new ArrayList<Integer>();
138                         String                  elt;
139
140                         while ((elt = get_token(reader)) != null)
141                                 array.add(AltosParse.parse_int(elt));
142                         int[] ret = new int[array.size()];
143                         for (int i = 0; i < ret.length; i++)
144                                 ret[i] = array.get(i);
145                         return ret;
146                 } catch (ParseException pe) {
147                         return def;
148                 } catch (IOException ie) {
149                         return def;
150                 }
151         }
152
153         public void putLong(String key, long value) {
154                 put(key, Long.toString(value));
155         }
156
157         public long getLong(String key, long def) {
158                 String  value = get(key);
159
160                 if (value == null)
161                         return def;
162                 try {
163                         return AltosParse.parse_long(value);
164                 } catch (ParseException pe) {
165                         return def;
166                 }
167         }
168
169         public void putDouble(String key, double value) {
170                 put(key, AltosParse.format_double_net(value));
171         }
172
173         public double getDouble(String key, double def) {
174                 String  value = get(key);
175
176                 if (value == null)
177                         return def;
178                 try {
179                         return AltosParse.parse_double_net(value);
180                 } catch (ParseException pe) {
181                         return def;
182                 }
183         }
184
185         public void putDoubleArray(String key, double value[]) {
186                 if (value == null)
187                         return;
188
189                 StringWriter    writer = new StringWriter();
190
191                 try {
192                         for (int i = 0; i < value.length; i++)
193                                 put_token(writer, AltosParse.format_double_net(value[i]));
194                         put(key, writer.toString());
195                 } catch (IOException ie) {
196                 }
197         }
198
199         public double[] getDoubleArray(String key, double[] def) {
200                 String          value = get(key);
201
202                 if (value == null)
203                         return def;
204                 try {
205                         StringReader            reader = new StringReader(value);
206                         ArrayList<Double>       array = new ArrayList<Double>();
207                         String                  elt;
208
209                         while ((elt = get_token(reader)) != null)
210                                 array.add(AltosParse.parse_double_net(elt));
211                         double[] ret = new double[array.size()];
212                         for (int i = 0; i < ret.length; i++)
213                                 ret[i] = array.get(i);
214                         return ret;
215                 } catch (ParseException pe) {
216                         return def;
217                 } catch (IOException ie) {
218                         return def;
219                 }
220         }
221
222         public String getString(String key, String def) {
223                 String  value = get(key);
224
225                 if (value == null)
226                         return def;
227                 return value;
228         }
229
230         public void putString(String key, String value) {
231                 if (value != null)
232                     put(key, value);
233         }
234
235         public AltosHashSet getHash(String key) {
236                 String  value = get(key);
237
238                 if (value == null)
239                         return null;
240                 try {
241                         return new AltosHashSet(value);
242                 } catch (IOException ie) {
243                         return null;
244                 }
245         }
246
247         public void putHash(String key, AltosHashSet h) {
248                 put(key, h.toString());
249         }
250
251         public void putHashable(String key, AltosHashable h) {
252                 if (h == null)
253                         return;
254
255                 put(key, h.hashSet().toString());
256         }
257
258         private AltosHashSet (String string) throws IOException {
259                 StringReader reader = new StringReader(string);
260                 String  key, value;
261
262                 for (;;) {
263                         key = get_token(reader);
264                         value = get_token(reader);
265                         if (key == null || value == null)
266                                 break;
267                         put(key, value);
268                 }
269         }
270
271         public AltosHashSet() {
272         }
273
274         static public AltosHashSet fromString(String string) {
275                 try {
276                         return new AltosHashSet(string);
277                 } catch (IOException ie) {
278                         return null;
279                 }
280         }
281
282         static public AltosHashSet[] array(String string) {
283
284                 if (string == null)
285                         return null;
286
287                 try {
288                         StringReader            reader = new StringReader(string);
289                         ArrayList<AltosHashSet> array = new ArrayList<AltosHashSet>();
290                         String                  element;
291
292                         while ((element = get_token(reader)) != null)
293                                 array.add(new AltosHashSet(element));
294                         return array.toArray(new AltosHashSet[0]);
295                 } catch (IOException ie) {
296                         return null;
297                 }
298         }
299
300         static public String toString(AltosHashSet[] sets) {
301                 if (sets == null)
302                         return null;
303
304                 try {
305                         StringWriter            writer = new StringWriter();
306
307                         for (AltosHashSet h : sets) {
308                                 String          element = h.toString();
309                                 put_token(writer, element);
310                         }
311                         return writer.toString();
312                 } catch (IOException ie) {
313                         return null;
314                 }
315         }
316 }