-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParseIni.ts
207 lines (160 loc) · 5.21 KB
/
ParseIni.ts
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
export default class ParseIni {
public schema: any;
public ini: string = '';
constructor() {}
private cleanComments(line: string): string {
let cleanLine = line;
// Skip comments
if (cleanLine.indexOf('#') > -1)
cleanLine = cleanLine.substring(0, cleanLine.indexOf('#'));
if (cleanLine.indexOf(';') > -1)
cleanLine = cleanLine.substring(0, cleanLine.lastIndexOf(';'));
cleanLine = cleanLine.trim();
return cleanLine;
}
private headerCheck(line: string): boolean {
return line[0] === '[' && line.substring(line.length - 1) === ']';
}
private destructureValue(line: string): Array<string> {
let cleanLine = line;
let response = '';
if (cleanLine.indexOf('=') > -1) {
response = cleanLine.substring(cleanLine.indexOf('=') + 1);
cleanLine = cleanLine.substring(0, cleanLine.indexOf('='));
cleanLine = cleanLine.trim();
response = response.trim();
}
return [cleanLine, response];
}
public setOjb(obj: any) {
if (!this.ini && !(this.ini.length === 0))
return;
const objCheck = {...obj};
const lines: Array<string> = this.ini.split(/\n/i);
let lastHeader: string = 'unset';
let foundHeader: number = -1;
let foundMatch: boolean = false;
// Adding obj data to existing ini sheet
for (let a: number = 0; a < lines.length; a++) {
let cleanLine = this.cleanComments(lines[a]);
if (cleanLine.length > 0) {
// Determine if it's a header or setting
if (this.headerCheck(cleanLine)) {
if (lastHeader !== 'unset') {
delete objCheck[lastHeader];
}
lastHeader = cleanLine.replace(/(\[|\])/g, '');
if (!obj[lastHeader])
continue;
} else {
let response;
[cleanLine, response] = this.destructureValue(cleanLine);
if (!obj[lastHeader] || (obj[lastHeader] && !obj[lastHeader][cleanLine]))
continue;
if (obj[lastHeader][cleanLine]) {
lines[a] = `${cleanLine}=${obj[lastHeader][cleanLine]}`;
}
}
}
}
// Adding in data that isn't in there by default
for (let key in objCheck) {
if (objCheck.hasOwnProperty(key)) {
lines.push(`\n[${key}]`);
for (let subkey in objCheck[key]) {
if (objCheck[key].hasOwnProperty(subkey)) {
lines.push(`${subkey}=${objCheck[key][subkey]}`);
}
}
}
}
const newLines = lines.join('\n');
// Get new schema
this.parse(newLines);
}
public set(header: string, field: string, value: string): void {
if (!this.ini)
return;
const lines: Array<string> = this.ini.split(/\n/i);
let lastHeader: string = 'unset';
let foundHeader: number = -1;
let foundMatch:boolean = false;
for (let a: number = 0; a < lines.length; a++) {
let cleanLine = this.cleanComments(lines[a]);
if (cleanLine.length > 0) {
// Determine if it's a header or setting
if (this.headerCheck(cleanLine)) {
lastHeader = cleanLine.replace(/(\[|\])/g, '');
if (lastHeader === header)
foundHeader = a;
} else {
let response;
[cleanLine, response] = this.destructureValue(cleanLine);
if (lastHeader === header && field === cleanLine) {
lines[a] = `${field}=${value}`;
foundMatch = true;
break;
}
}
}
}
if (!foundMatch && foundHeader > -1)
lines.splice(foundHeader + 1, 0, `${field}=${value}`);
const newLines = lines.join('\n');
// Get new schema
this.parse(newLines);
}
public verifyData(): Boolean {
try {
if (this.schema && (this.schema['default'] || this.schema['DEFAULT'])) {
let data;
if (this.schema['default']) {
data = this.schema['default'];
} else {
data = this.schema['DEFAULT'];
}
if (!data.scheme || (data.scheme && !data.scheme.length)) {
return false;
}
if (!data.address || (data.address && !data.address.length)) {
return false;
}
if (!data.port || (data.port && !data.port.length)) {
return false;
}
if (!data.username || (data.username && !data.username.length)) {
return false;
}
if (!data.password || (data.password && !data.password.length)) {
return false;
}
return true;
}
return false;
} catch (e) {
return false;
}
}
public parse(ini: string): void {
const lines: Array<string> = ini.split(/\n/i);
let schema: any = {};
let lastHeader: string = 'unset';
lines.forEach(line => {
let cleanLine = this.cleanComments(line);
if (cleanLine.length > 0) {
// Determine if it's a header or setting
if (this.headerCheck(cleanLine))
lastHeader = cleanLine.replace(/(\[|\])/g, '');
else {
let response;
[cleanLine, response] = this.destructureValue(cleanLine);
if (!schema[lastHeader])
schema[lastHeader] = {};
schema[lastHeader][cleanLine] = response;
}
}
});
this.schema = schema;
this.ini = ini;
}
}