-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBOalertData.js
110 lines (105 loc) · 3.21 KB
/
BBOalertData.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
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
/**
* @ignore
*/
class BBOalertData {
constructor() {
// Set cursor at the beginning of the table
this.alertTableCursor = 0;
this.matchOption = true;
this.aliasList = [];
this.trimOn = true;
}
setData(text) {
alertData = text;
if (alertData.endsWith("\n")) alertData = alertData.slice(0, -1);
alertTable = alertData.split("\n");
alertData = alertTable.join("\n");
}
appendData(text) {
alertData = alertData + "\n" + text;
// Strip trailing linefeed if any
if (alertData.endsWith("\n")) alertData = alertData.slice(0, -1);
alertTable = alertData.split("\n");
alertData = alertTable.join("\n");
}
resetScan() {
// Set cursor at the beginning of the table
this.alertTableCursor = 0;
this.matchOption = true;
this.aliasList = [];
this.trimOn = true;
}
addAlias(r) {
this.aliasList.push(r);
}
replaceAliases(t) {
var oldText = '';
var newText = t;
var i = 0;
while (oldText != newText) {
if (i++ > 10) return newText;
oldText = newText;
newText = this.replaceAlias(oldText);
}
return newText;
}
replaceAlias(t) {
var oldStr = '';
var newStr = '';
for (var i = 0; i < this.aliasList.length; i++) {
var r = this.aliasList[i].split(',');
if (r.length < 3) continue;
if (t.indexOf(r[1]) != -1) {
oldStr = r[1];
newStr = r[2];
}
}
if (newStr == '') return t;
return t.replace(oldStr, newStr);
}
getNextLine() {
if (this.alertTableCursor >= alertTable.length) return null;
var txt = alertTable[this.alertTableCursor];
if (this.trimOn) txt = txt.trim();
// Concatenate records ending with backslash
while (txt.endsWith('\\')) {
this.alertTableCursor++;
if (this.trimOn) {
txt = txt.slice(0, txt.length - 1) + alertTable[this.alertTableCursor].trim();
} else {
txt = txt.slice(0, txt.length - 1) + alertTable[this.alertTableCursor];
}
if (this.alertTableCursor >= alertTable.length) return txt;
}
this.alertTableCursor++;
return txt;
}
getNextRecord() {
var txt, rec, keyword;
while ((txt = this.getNextLine()) != null) {
if (txt == "") continue;
rec = txt.split(",");
keyword = elimineSpaces(rec[0].trim());
if (keyword == 'Option') {
if (rec.length < 2) {
this.matchOption = true;
} else {
this.matchOption = checkOption(rec);
}
} else if (keyword == 'Alias') {
if (this.matchOption) this.addAlias(txt);
} else {
if (this.matchOption) return txt;
}
}
return null;
}
}
function testBBOalertData() {
var scan = new BBOalertData();
var txt;
scan.resetScan();
while ((txt = scan.getNextRecord()) != null) {
console.log(txt);
}
}