-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.java
170 lines (147 loc) · 4.67 KB
/
IniParser.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
/*
Description : IniParser.java contains ini parser code.
Creation Date : October 08, 2011
Authors : Navjot Singh
Project Manager : Navjot Singh
*/
import java.io.*;
class IniParser {
public static void write(String iniFile, String section, String element, String value) {
String curKey = "";
String curVal = "";
String checkSection = "";
boolean sectionStarted = false;
FileReader fr;
BufferedReader in;
BufferedWriter out;
OutputStreamWriter Sr;
try {
File file = new File("temp.ini");
OutputStream f = new FileOutputStream(file, false);
fr = new FileReader(iniFile);
in = new BufferedReader(fr);
Sr = new OutputStreamWriter(f);
out = new BufferedWriter(Sr);
String line;
int length = 0, offset =0;
while ((line = in.readLine()) != null) {
if(line.startsWith(";")) {
line+= "\r\n";
length = line.length();
Sr.flush();
Sr.write(line, 0, length);
continue;
}
else if(line.startsWith("[")) {
int a = line.indexOf("]");
checkSection = line.substring(1,a);
line += "\r\n";
length = line.length();
Sr.flush();
Sr.write(line, 0, length);
}
else {
line += "\r\n";
int i = line.indexOf("=");
if(i<0) continue;
curKey = line.substring(0, i).trim();
curVal = line.substring(i+1);
if(curKey.equals(element)) {
line+= "\r\n";
Sr.write(line, 0, i+1);
curVal = value;
curVal+= "\r\n";
Sr.write(curVal, 0, curVal.length());
Sr.flush();
}
else {
line+= "\r\n";
Sr.write(line, 0, line.length());
Sr.flush();
}
}
}
f.close();
fr.close();
in.close();
Sr.close();
out.close();
File f2 = new File(iniFile);
if (!f2.exists()) throw new IllegalArgumentException("Delete: no such file or directory: " + iniFile);
if (!f2.canWrite()) throw new IllegalArgumentException("Delete: write protected: "+ iniFile);
if (!f2.delete()) throw new IOException("Unable to delete file "+ f2.getName());
new File("temp.ini").renameTo(new File ("settings.ini"));
}
catch(Exception e) { System.out.println(e); }
}
public static String parse(String iniFile, String section, String element) {
String curKey = "";
String curVal = "";
boolean sectionStarted = false;
/*ensure ini file*/
if(!verifyIni()) {
System.out.println("Invalid ini file: " + iniFile);
return curVal;
}
FileReader fr = null;
BufferedReader in = null;
try {
fr = new FileReader(iniFile);
in = new BufferedReader(fr);
String line;
while ((line = in.readLine()) != null) {
/*remove blank spaces at start and end of line*/
line=line.trim();
/*ignore comments*/
if(line.startsWith(";")) continue;
/*hunt section*/
if(!sectionStarted) {
if(line.startsWith("[" + section + "]")) sectionStarted = true;
continue;
}
/*section ended*/
if(line.startsWith("[")) {
fr.close();
in.close();
return curVal;
}
//System.out.println(line);
/*fetch current element*/
int i = line.indexOf("=");
/*ignore lines where '=' is not found*/
if(i<0) continue;
curKey = line.substring(0, i).trim();
curVal = line.substring(i+1).trim();
if(curKey.equals(element)) {
fr.close();
in.close();
return curVal;
}
}//while
}
catch(Exception e) {
e.printStackTrace();
System.out.println("ERROR:: Failed to parse '" + element + "' from " + iniFile);
}
try {
fr.close();
in.close();
}catch(Exception e1){}
return "";
}
private static boolean verifyIni() {
/*TODO add code to verify validity of given ini file*/
return true;
}
public static void main(String args[]) {
/*if(args.length !=1) {
System.out.println("Usage: \njava IniParser <ini-file-path>");
return;
}
*/
//IniParser.write("settings.ini", "MIN_SYS_TRAY","1");
// IniParser.write("settings.ini", "general", "MIN_SYS_TRAY","100");
// IniParser.write("settings.ini", "themes", "SEL_THEME","Brown");
IniParser.write("settings.ini", "content", "STRG_DIR", "WEAVECLOUD/");
}
}//IniParser