Skip to content

Commit 5fe271d

Browse files
committed
Trying reading dot file
1 parent f70c577 commit 5fe271d

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

tools/dot_parser.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66

77
//import { parse } from 'ts-graphviz/ast';
88
const ast = require('ts-graphviz/ast');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
const base_path = __dirname || process.cwd();
13+
const target_path = path.resolve(base_path + '\\..\\submodules\\SS13-Codebases\\src\\builds_all.dot');
14+
15+
16+
// https://stackoverflow.com/a/6833016/8175291
17+
async function readLines(input, func) {
18+
var remaining = '';
19+
20+
input.on('data', function(data) {
21+
remaining += data;
22+
var index = remaining.indexOf('\n');
23+
while (index > -1) {
24+
var line = remaining.substring(0, index);
25+
remaining = remaining.substring(index + 1);
26+
func(line);
27+
index = remaining.indexOf('\n');
28+
}
29+
});
30+
31+
input.on('end', function() {
32+
if (remaining.length > 0) {
33+
func(remaining);
34+
}
35+
});
36+
}
37+
38+
function func(data) {
39+
console.log('Line: ' + data);
40+
}
41+
942

1043
// https://github.com/ts-graphviz/ts-graphviz#ts-graphvizast-module-
1144
function parse_example(_in) {
@@ -24,7 +57,32 @@ async function main() {
2457
]
2558
}
2659
`;
27-
console.log('parse:', parse_example(example));
60+
//console.log('parse:', parse_example(example));
61+
62+
63+
//var input = fs.createReadStream(target_path);
64+
//await readLines(input, func);
65+
66+
let file_content_raw = fs.readFileSync(target_path, 'utf8'); //.replaceAll('\n', '\\n')
67+
let file_content_arr = file_content_raw.split(/\r?\n|\r|\n/g); // https://stackoverflow.com/a/21712066/8175291
68+
console.log('file_content_arr.length:', file_content_arr.length);
69+
for (var i = 0; i < file_content_arr.length; i++) {
70+
// https://stackoverflow.com/a/1981837/8175291
71+
if (file_content_arr[i].includes('\\n')) {
72+
let _content = file_content_arr[i];
73+
console.log('> (\\n):', i, _content.trim().replaceAll(/ +/g, ' '));
74+
file_content_arr[i] = file_content_arr[i].replaceAll('\\n', '_n');
75+
}
76+
else if (file_content_arr[i].includes('\\')) {
77+
let _content = file_content_arr[i];
78+
console.log('> (\\):', i, _content.trim().replaceAll(/ +/g, ' '));
79+
file_content_arr[i] = file_content_arr[i].replaceAll('\\', '_');
80+
}
81+
}
82+
file_content_raw = file_content_arr.join('\n');
83+
84+
console.log('readFileAsync:', file_content_raw);
85+
console.log('parse:', parse_example(file_content_raw));
2886

2987
console.log('Bye');
3088
}

0 commit comments

Comments
 (0)