-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_cin.js
executable file
·52 lines (49 loc) · 1.23 KB
/
convert_cin.js
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
#!/usr/bin/env node
if (process.argv.length < 3) {
console.log('Usage: node '+process.argv[1]+' table_name')
process.exit(1);
}
var name = process.argv[2];
var fs = require('fs');
var options = {'encoding': 'utf8'};
fs.readFile(name + '.cin', options, function (err, data) {
if (err) throw err;
lines = data.split('\n');
var inside = false;
var tbl = {}
var inv = {}
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var len = line.length;
for (var j = 0; j < line.length; j++) {
if (line[j] === '#') {
len = j;
break;
}
}
line = line.substring(0, len);
line = line.trim();
if (line.length === 0) {
continue;
}
if (line === '%chardef begin') {
inside = true;
} else if (line === '%chardef end') {
inside = false;
} else if (!inside) {
continue;
} else {
var parts = line.split(/\s+/);
if (!(parts[0] in tbl)) {
tbl[parts[0]] = [];
}
tbl[parts[0]].push(parts[1]);
if (!(parts[1] in inv)) {
inv[parts[1]] = [];
}
inv[parts[1]].push(parts[0]);
}
}
fs.writeFileSync(name + '.json', JSON.stringify(tbl));
fs.writeFileSync(name + '_inv.json', JSON.stringify(inv));
});