/
ConfigurationLoader.java
72 lines (65 loc) · 2.16 KB
/
ConfigurationLoader.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
package com.farrellf.TelemetryGUI;
import java.io.File;
import java.util.Scanner;
/**
* Reads the configuration file and creates TelemetryGroups and TelemetryGroupItems as needed.
*
* @author Farrell Farahbod
* @version 1.0
*/
public class ConfigurationLoader {
File f;
Scanner s;
TelemetryGUI gui;
public ConfigurationLoader(TelemetryGUI gui) {
this.gui = gui;
// open the config file
try {
f = new File("config.txt");
s = new Scanner(f);
} catch(Exception e) {
System.err.println("Unable to open the configuration file at " + f.getAbsolutePath());
}
// parse the config file
while(s.hasNext()) {
String line = s.nextLine();
try{
if(line.length() == 0) {
// ignore empty lines
continue;
} else if(line.startsWith("#")) {
// ignore comment lines
continue;
} else if(line.startsWith("Group:")) {
// process groups
line = line.substring(6);
String[] chunks = line.split(",");
String groupName = chunks[0].trim();
int x = Integer.parseInt(chunks[1].trim());
int y = Integer.parseInt(chunks[2].trim());
gui.addGroup(groupName, x, y);
System.out.println("Adding Group: " + groupName + ", " + x + ", " + y);
} else if(line.startsWith("Item:")) {
// process items
line = line.substring(5);
String[] chunks = line.split(",");
String groupName = chunks[0].trim();
String itemName = chunks[1].trim();
String dbName = chunks[2].trim();
int min = Integer.parseInt(chunks[3].trim());
int max = Integer.parseInt(chunks[4].trim());
double factor = Double.parseDouble(chunks[5].trim());
String df = chunks[6].trim();
String suffix = chunks[7].trim();
int defaultValue = Integer.parseInt(chunks[8].trim());
gui.addGroupItem(groupName, itemName, dbName, min, max, factor, df, suffix, defaultValue);
System.out.println("Adding Item: " + groupName + ", " + itemName + ", " + dbName + ", " + min + ", " + max + ", " + factor + ", " + df + ", " + suffix + ", " + defaultValue);
} else {
// invalid line
}
} catch(Exception e) {
System.err.println("Error parsing configuration file line: " + line);
}
}
}
}