-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleINI.java
396 lines (350 loc) · 10.7 KB
/
SimpleINI.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright(C) 2023 DeviceBlack
*
* 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.
*/
package com.devicewhite;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* SimpleINI class for handling INI file operations.
*/
public class SimpleINI {
private HashMap<String, HashMap<String, String>> data = new HashMap<>();
private File ini;
/**
* Constructor to initialize SimpleINI with a specified file.
*
* @param file The INI file.
*/
public SimpleINI(File file) {
ini = file;
}
/**
* Load method reads and processes the content of the INI file.
*
* @return True if successful, false otherwise.
*/
public boolean load() {
BufferedReader buffer = null;
try {
buffer = new BufferedReader(new FileReader(ini));
HashMap<String, String> sectionData = new HashMap<>();
String currentSection = "none", line;
while((line = buffer.readLine()) != null) {
// Check if the line contains a section header
if(!line.startsWith("#")) {
if(line.matches("\\[\\w+\\]")) {
// Save the data for the current section and move to the new section
data.put(currentSection, sectionData);
currentSection = line.substring(1, line.length() - 1);
sectionData = new HashMap<>();
} else {
// Process key-value pairs within a section
String[] values = line.split(" = ", 2);
if(values.length == 2) {
sectionData.put(values[0], values[1]);
}
}
}
}
// Save the last section data
if(!sectionData.isEmpty()) {
data.put(currentSection, sectionData);
return true;
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(buffer != null) {
try {
buffer.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
data.clear();
return false;
}
/**
* Save method writes the data to the INI file.
*
* @param clear If true, clears the data after saving.
* @return True if successful, false otherwise.
*/
public boolean save(int saveType) {
BufferedWriter buffer = null;
try {
buffer = new BufferedWriter(new FileWriter(ini, false));
String text = "# Created by DeviceWhite\n";
// Save data for the 'none' section if it exists
if(data.containsKey("none")) {
text = text.concat(saveSection(buffer, "none"));
}
// Save data for other sections
for(String section : new HashSet<>(data.keySet())) {
if(section != "none") {
text = text.concat(String.format("\n[%s]", section));
text = text.concat(saveSection(buffer, section));
}
}
buffer.write(text);
buffer.flush();
return true;
} catch(Exception e) {
e.printStackTrace();
} finally {
if(buffer != null) {
try {
buffer.close();
} catch(Exception e) {
e.printStackTrace();
}
}
switch(saveType % 3) {
case SaveType.UNLOAD_CACHE:
data.clear();
break;
case SaveType.RELOAD_CACHE:
data.clear();
load();
break;
}
}
return false;
}
/**
* Save method writes the data to the INI file and clears the data.
*
* @return True if successful, false otherwise.
*/
public boolean save() {
return save(SaveType.KEEP_CACHE);
}
/**
* Helper method to save a section's data.
*
* @param buffer The BufferedWriter to write to.
* @param section The section to save.
* @throws Exception If an I/O error occurs.
*/
private String saveSection(BufferedWriter buffer, String section) throws Exception {
HashMap<String, String> sectionData = data.get(section);
Set<Map.Entry<String, String>> entrySet = sectionData.entrySet();
StringBuilder text = new StringBuilder("\n");
for(Map.Entry<String, String> entry : entrySet) {
text.append(String.format("%s = %s\n", entry.getKey(), entry.getValue()));
}
return text.toString();
}
/**
* Gets the value associated with the specified key in the given section.
*
* @param section The section in which to look for the key.
* @param key The key whose associated value is to be retrieved.
* @return The value to which the specified key is mapped, or null if the key is not present in the section.
*/
public String get(String section, String key) {
if(section == "none") return null;
else if(section == null) section = "none";
HashMap<String, String> sectionData = data.getOrDefault(section, new HashMap<>());
return sectionData.getOrDefault(key, null);
}
/**
* Gets the value associated with the specified key.
*
* @param key The key whose associated value is to be retrieved.
* @return The value to which the specified key is mapped, or null if the key is not present.
*/
public String get(String key) {
return get(null, key);
}
/**
* Gets the float value associated with the specified key in the given section.
*
* @param section The section in which to look for the key.
* @param key The key whose associated float value is to be retrieved.
* @return The float value to which the specified key is mapped.
*/
public float getFloat(String section, String key) {
return Float.parseFloat(get(section, key));
}
/**
* Gets the float value associated with the specified key.
*
* @param key The key whose associated float value is to be retrieved.
* @return The float value to which the specified key is mapped.
*/
public float getFloat(String key) {
return getFloat(null, key);
}
/**
* Gets the integer value associated with the specified key in the given section.
*
* @param section The section in which to look for the key.
* @param key The key whose associated integer value is to be retrieved.
* @return The integer value to which the specified key is mapped.
*/
public int getInt(String section, String key) {
return Integer.parseInt(get(section, key));
}
/**
* Gets the integer value associated with the specified key.
*
* @param key The key whose associated integer value is to be retrieved.
* @return The integer value to which the specified key is mapped.
*/
public int getInt(String key) {
return getInt(null, key);
}
/**
* Gets the boolean value associated with the specified key in the given section.
*
* @param section The section in which to look for the key.
* @param key The key whose associated boolean value is to be retrieved.
* @return The boolean value to which the specified key is mapped.
*/
public boolean getBool(String section, String key) {
return Boolean.parseBoolean(get(section, key));
}
/**
* Gets the boolean value associated with the specified key.
*
* @param key The key whose associated boolean value is to be retrieved.
* @return The boolean value to which the specified key is mapped.
*/
public boolean getBool(String key) {
return getBool(null, key);
}
/**
* Checks if a key exists in the specified section.
*
* @param section The section in which to check for the key.
* @param key The key to check for existence.
* @return True if the key exists in the section, false otherwise.
*/
public boolean exists(String section, String key) {
return(get(section, key) != null);
}
/**
* Checks if a key exists.
*
* @param key The key to check for existence.
* @return True if the key exists, false otherwise.
*/
public boolean exists(String key) {
return exists(null, key);
}
/**
* Sets the value associated with the specified key in the given section.
*
* @param section The section in which to set the key-value pair.
* @param key The key to set.
* @param value The value to associate with the key.
*/
public void set(String section, String key, String value) {
if(section == "none") return;
else if(section == null) section = "none";
HashMap<String, String> sectionData = data.getOrDefault(section, new HashMap<>());
if(sectionData.containsKey(key)) {
sectionData.replace(key, value);
} else {
sectionData.put(key, value);
}
if(!data.containsKey(section)) {
data.put(section, sectionData);
}
}
/**
* Sets the value associated with the specified key.
*
* @param key The key to set.
* @param value The value to associate with the key.
*/
public void set(String key, String value) {
set(null, key, value);
}
/**
* Sets the float value associated with the specified key in the given section.
*
* @param section The section in which to set the key-value pair.
* @param key The key to set.
* @param value The float value to associate with the key.
*/
public void setFloat(String section, String key, float value) {
set(section, key, Float.toString(value));
}
/**
* Sets the float value associated with the specified key.
*
* @param key The key to set.
* @param value The float value to associate with the key.
*/
public void setFloat(String key, float value) {
setFloat(null, key, value);
}
/**
* Sets the integer value associated with the specified key in the given section.
*
* @param section The section in which to set the key-value pair.
* @param key The key to set.
* @param value The integer value to associate with the key.
*/
public void setInt(String section, String key, int value) {
set(section, key, Integer.toString(value));
}
/**
* Sets the integer value associated with the specified key.
*
* @param key The key to set.
* @param value The integer value to associate with the key.
*/
public void setInt(String key, int value) {
setInt(null, key, value);
}
/**
* Sets the boolean value associated with the specified key in the given section.
*
* @param section The section in which to set the key-value pair.
* @param key The key to set.
* @param value The boolean value to associate with the key.
*/
public void setBool(String section, String key, boolean value) {
set(section, key, Boolean.toString(value));
}
/**
* Sets the boolean value associated with the specified key.
*
* @param key The key to set.
* @param value The boolean value to associate with the key.
*/
public void setBool(String key, boolean value) {
setBool(null, key, value);
}
}
/**
* Enumeration class for save types.
*/
class SaveType {
public static final int KEEP_CACHE = 0;
public static final int RELOAD_CACHE = 1;
public static final int UNLOAD_CACHE = 2;
}