summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/Tools/Props.java
blob: 7c7d063085e97ecf5d4b10463c71c0dd26933b96 (plain)
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
package Tools;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/*
 * Copyright 2009 Volker Oth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Property class to ease use of ini files to save/load properties.
 *
 * @author Volker Oth
 */
public class Props {

	private final static long serialVersionUID = 0x01;

	/** extended hash to store properties */
	private final Properties hash;
	/** header string */
	private String header;

	/**
	 * Constructor
	 */
	public Props() {
		hash = new Properties();
		header = new String();
	}

	/**
	 * Set the property file header
	 * @param h String containing Header information
	 */
	public void setHeader(final String h) {
		header = h;
	}

	/**
	 * Clear all properties
	 */
	public void clear() {
		hash.clear();
	}

	/**
	 * Remove key
	 * @param key Name of key
	 */
	public void remove(final String key) {
		hash.remove(key);
	}

	/**
	 * Set string property
	 * @param key Name of the key to set value for
	 * @param value Value to set
	 */
	public void set(final String key, final String value) {
		hash.setProperty(key, value);
	}

	/**
	 * Set integer property
	 * @param key Name of the key to set value for
	 * @param value Value to set
	 */
	public void set(final String key, final int value) {
		hash.setProperty(key, String.valueOf(value));
	}

	/**
	 * Set boolean property
	 * @param key Name of the key to set value for
	 * @param value Value to set
	 */
	public void set(final String key, final boolean value) {
		hash.setProperty(key, String.valueOf(value));
	}

	/**
	 * Set double property
	 * @param key Name of the key to set value for
	 * @param value Value to set
	 */
	public void set(final String key, final double value) {
		hash.setProperty(key, String.valueOf(value));
	}

	/**
	 * Get string property
	 * @param key Name of the key to get value for
	 * @param def Default value in case key is not found
	 * @return Value of key as String
	 */
	public String get(final String key, final String def) {
		String s = hash.getProperty(key,def);
		return removeComment(s);
	}

	/**
	 * Get integer property
	 * @param key Name of the key to get value for
	 * @param def Default value in case key is not found
	 * @return Value of key as int
	 */
	public int get(final String key, final int def) {
		String s = hash.getProperty(key);
		if (s == null)
			return def;
		s = removeComment(s);
		return parseString(s);
	}

	/**
	 * Get integer array property
	 * @param key Name of the key to get value for
	 * @param def Default value in case key is not found
	 * @return Value of key as array of int
	 */
	public int[] get(final String key, final int def[]) {
		String s = hash.getProperty(key);
		if (s == null)
			return def;
		s = removeComment(s);
		String members[] = s.split(",");
		// remove trailing and leading spaces
		for (int i=0; i<members.length; i++)
			members[i] = trim(members[i]);

		int ret[];
		ret = new int[members.length];
		for (int i=0; i<members.length; i++)
			ret[i] = parseString(members[i]);

		return ret;
	}

	/**
	 * Get string array property
	 * @param key Name of the key to get value for
	 * @param def Default value in case key is not found
	 * @return Value of key as array of string
	 */
	public String[] get(final String key, final String def[]) {
		String s = hash.getProperty(key);
		if (s == null)
			return def;
		s = removeComment(s);
		String members[] = s.split(",");
		// remove trailing and leading spaces
		for (int i=0; i<members.length; i++)
			members[i] = trim(members[i]);

		return members;
	}

	/**
	 * Get boolean property
	 * @param key Name of the key to get value for
	 * @param def Default value in case key is not found
	 * @return Value of key as boolean
	 */
	public boolean get(final String key, final boolean def) {
		String s = hash.getProperty(key);
		if (s == null)
			return def;
		s = removeComment(s);
		return Boolean.valueOf(s).booleanValue();
	}

	/**
	 * Get double property
	 * @param key Name of the key to get value for
	 * @param def default value in case key is not found
	 * @return value of key as double
	 */
	public double get(final String key, final double def) {
		String s = hash.getProperty(key);
		if (s == null)
			return def;
		s = removeComment(s);
		return Double.valueOf(s).doubleValue();
	}

	/**
	 * Save property file
	 * @param fname File name of property file
	 * @return True if ok, false if exception occured
	 */
	public boolean save(final String fname) {
		try {
			FileOutputStream f = new FileOutputStream(fname);
			hash.store(f, header);
			return true;
		} catch(FileNotFoundException e) {
			return false;
		} catch(IOException e) {
			return false;
		}
	}

	/**
	 * Load property file
	 * @param file File handle of property file
	 * @return True if OK, false if exception occurred
	 */
	public boolean load(final URL file) {
		try {
			InputStream f = file.openStream();
			hash.load(f);
			f.close();
			return true;
		} catch(FileNotFoundException e) {
			return false;
		} catch(IOException e) {
			return false;
		}
	}

	/**
	 * Load property file
	 * @param fname File name of property file
	 * @return True if OK, false if exception occurred
	 */
	public boolean load(final String fname) {
		try {
			FileInputStream f = new FileInputStream(fname);
			hash.load(f);
			f.close();
			return true;
		} catch(FileNotFoundException e) {
			return false;
		} catch(IOException e) {
			return false;
		}
	}

	/**
	 * Parse hex, binary or octal number
	 * @param s String that contains one number
	 * @return Integer value of string
	 */
	private static int parseString(final String s) {
		if (s==null || s.length()==0)
			return -1;
		if (s.charAt(0) == '0') {
			if (s.length() == 1)
				return 0;
			else if (s.length() > 2 && s.charAt(1) == 'x')
				return Integer.parseInt(s.substring(2),16); // hex
			else if (s.charAt(1) == 'b')  // binary
				return Integer.parseInt(s.substring(2),2);
			else return Integer.parseInt(s.substring(0),8); // octal
		}
		int retval;
		try {
			retval = Integer.parseInt(s);
		} catch (NumberFormatException ex) {
			retval = 0;
		}
		return retval;
	}

	/**
	 * Remove comment from line. Comment character is "#".
	 * Everything behind (including "#") will be removed
	 * @param s String to search for comment
	 * @return String without comment
	 */
	private String removeComment(final String s) {
		int pos = s.indexOf('#');
		if (pos != -1)
			return s.substring(0,pos);
		else
			return s;
	}

	/**
	 * Remove trailing and leading spaces from a string
	 * @param s String to process
	 * @return String without leading and trailing spaces
	 */
	private static String trim(final String s) {
		// search first character that is not a space
		int spos;
		for (spos=0; spos<s.length(); spos++)
			if (s.charAt(spos) != ' ')
				break;
		int epos;
		for (epos=s.length()-1; epos > spos; epos--)
			if (s.charAt(epos) != ' ')
				break;
		return s.substring(spos, epos+1);
	}
}