| 1 |
/******************************************************************** |
| 2 |
This file is part of Ironseed. |
| 3 |
|
| 4 |
Ironseed is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
Ironseed is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with Ironseed. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
********************************************************************/ |
| 17 |
|
| 18 |
/********************************************* |
| 19 |
Data Generator: Conversation logs generator |
| 20 |
|
| 21 |
Copyright: |
| 22 |
1994 Channel 7, Destiny: Virtual |
| 23 |
2020 Matija Nalis <[email protected]> |
| 24 |
**********************************************/ |
| 25 |
|
| 26 |
import std.stdio; |
| 27 |
import std.ascii; |
| 28 |
import std.regex; |
| 29 |
import std.conv; |
| 30 |
import std.string; |
| 31 |
import std.algorithm; |
| 32 |
import data; |
| 33 |
|
| 34 |
align(1): |
| 35 |
|
| 36 |
struct Converse { |
| 37 |
int linenum; |
| 38 |
short event; |
| 39 |
short runevent; |
| 40 |
short rcode; |
| 41 |
short index; |
| 42 |
char []keyword; |
| 43 |
} |
| 44 |
|
| 45 |
struct Response { |
| 46 |
int linenum; |
| 47 |
short index; |
| 48 |
char []response; |
| 49 |
} |
| 50 |
|
| 51 |
Converse []conv; |
| 52 |
Response []resp; |
| 53 |
|
| 54 |
char []inputfile; |
| 55 |
|
| 56 |
int [char []]keywordlines; |
| 57 |
int [char []]keywordused; |
| 58 |
char[][][int] responsekeywords; |
| 59 |
|
| 60 |
int [char []]ignorewords; |
| 61 |
int [char []]rootwords; |
| 62 |
|
| 63 |
int lastauto = 1000; |
| 64 |
int currentauto = 1000; |
| 65 |
|
| 66 |
void addignore(char []words) { |
| 67 |
foreach(char []s; std.string.split(words)) { |
| 68 |
//printf("ignore:%s\n", s.toStringz); |
| 69 |
ignorewords[to!string(s)] = 0; |
| 70 |
} |
| 71 |
} |
| 72 |
void addignoremaybe(char []words) { |
| 73 |
foreach(char []s; std.string.split(words)) { |
| 74 |
if(s.length && s[0] == '@') { |
| 75 |
//printf("ignore:%s\n", s.toStringz); |
| 76 |
ignorewords[to!string(s[1..$])] = 0; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void addroot(char []words) { |
| 82 |
foreach(char []s; std.string.split(toUpper(words))) { |
| 83 |
//printf("root:%s\n", s.toStringz); |
| 84 |
rootwords[to!string(s)] = 0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void addwordline(int line, char []words) { |
| 89 |
foreach(char []s; std.string.split(toUpper(words))) { |
| 90 |
//printf("line:(%d)%s\n", line, s.toStringz); |
| 91 |
keywordlines[to!string(s)] = line; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void parsefile(char []file) { |
| 96 |
auto fh = File(file, "r"); |
| 97 |
inputfile = file; |
| 98 |
//auto convreg = regex("^(-?\\d+)\\s+.*"); |
| 99 |
auto convreg = regex("^(@)?(-?\\d+)\\s+(-?\\d+)\\s+(-?\\d+)\\s+(-?\\d+)\\s+(\\S.*)", "g"); |
| 100 |
auto respreg = regex("^(-?\\d+)\\s+(\\S.*)$", "g"); |
| 101 |
auto stopreg = regex("^-500\\s*$", "g"); |
| 102 |
auto emptyreg = regex("^\\s*$","g"); |
| 103 |
auto ignorereg = regex("^@(.*)$","g"); |
| 104 |
auto rootreg = regex("^@\\s*\\^\\s*$", "g"); |
| 105 |
Converse c; |
| 106 |
Response r; |
| 107 |
int num = 0; |
| 108 |
foreach(line; fh.byLine) { |
| 109 |
num++; |
| 110 |
line = detab(line); |
| 111 |
//printf("%s\n", line.toStringz); |
| 112 |
auto convreg_match=match(line, convreg); |
| 113 |
if(convreg_match) { |
| 114 |
//printf("conv: %s,%s,%s,%s,%s,%s\n", convreg_match.captures[1].toStringz, convreg_match.captures[2].toStringz, convreg_match.captures[3].toStringz, convreg_match.captures[4].toStringz, convreg_match.captures[5].toStringz, convreg_match.captures[6].toStringz); |
| 115 |
c.linenum = num; |
| 116 |
c.event = to!short(convreg_match.captures[2]); |
| 117 |
c.runevent = to!short(convreg_match.captures[3]); |
| 118 |
c.rcode = to!short(convreg_match.captures[4]); |
| 119 |
c.index = to!short(convreg_match.captures[5]); |
| 120 |
if(c.index < 0) { |
| 121 |
if(lastauto == currentauto) { |
| 122 |
lastauto++; |
| 123 |
} |
| 124 |
c.index = to!short(lastauto); |
| 125 |
} |
| 126 |
c.keyword = cast(char[])(convreg_match.captures[6].dup.toUpper); |
| 127 |
addignoremaybe(c.keyword); |
| 128 |
c.keyword = replace(c.keyword, "@", ""); |
| 129 |
addwordline(num, c.keyword); |
| 130 |
if(convreg_match.captures[1] == "@") { |
| 131 |
addroot(c.keyword); |
| 132 |
} |
| 133 |
conv ~= c; |
| 134 |
} else if(auto respreg_match=match(line, respreg)) { |
| 135 |
//printf("resp: %s,%s\n", respreg_match.captures[1].toStringz, respreg_match.captures[2].toStringz); |
| 136 |
r.linenum = num; |
| 137 |
r.index = to!short(respreg_match.captures[1]); |
| 138 |
if(r.index < 0) { |
| 139 |
if(lastauto != currentauto) { |
| 140 |
currentauto++; |
| 141 |
} |
| 142 |
r.index = to!short(currentauto); |
| 143 |
} |
| 144 |
r.response = " " ~ respreg_match.captures[2]; |
| 145 |
resp ~= r; |
| 146 |
} else if(auto stopreg_match=match(line, stopreg)) { |
| 147 |
//printf("stop: %s\n", stopreg_match.captures[0].toStringz); |
| 148 |
} else if (match(line, emptyreg)) { |
| 149 |
/*do nothing*/ |
| 150 |
} else if (match(line, rootreg)) { |
| 151 |
addroot(c.keyword); |
| 152 |
} else if (auto ignorereg_match=match(line, ignorereg)) { |
| 153 |
addignore(ignorereg_match.captures[1].dup); |
| 154 |
} else { |
| 155 |
printf("%s(%d): bad line: %s\n", inputfile.toStringz, num, line.toStringz); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
fh.close(); |
| 160 |
} |
| 161 |
|
| 162 |
char []matchkeyword(char []instr, char [][]keywords) { |
| 163 |
char []s = toUpper(instr); |
| 164 |
foreach(char []m; keywords) { |
| 165 |
if(m == s) { |
| 166 |
return instr; |
| 167 |
} |
| 168 |
} |
| 169 |
if(s in keywordused) { |
| 170 |
keywordused[to!string(s)] = 1; |
| 171 |
return "^" ~ instr ~ "^"; |
| 172 |
} |
| 173 |
return instr; |
| 174 |
} |
| 175 |
|
| 176 |
char []dokeyword(char []instr, char [][]keywords) { |
| 177 |
char []outstr = cast(char[])""; |
| 178 |
char []s = cast(char[])""; |
| 179 |
int suppress = 0; |
| 180 |
foreach(size_t i, char c; instr) { |
| 181 |
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '\'') { |
| 182 |
s ~= c; |
| 183 |
} else { |
| 184 |
if(s.length) { |
| 185 |
if(suppress) { |
| 186 |
outstr ~= s; |
| 187 |
suppress = 0; |
| 188 |
} else { |
| 189 |
outstr ~= matchkeyword(s, keywords); |
| 190 |
} |
| 191 |
s = cast(char[])""; |
| 192 |
} |
| 193 |
if(c == '_') { |
| 194 |
suppress = 1; |
| 195 |
} else { |
| 196 |
outstr ~= c; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
if(s.length) { |
| 201 |
if(suppress) { |
| 202 |
outstr ~= s; |
| 203 |
suppress = 0; |
| 204 |
} else { |
| 205 |
outstr ~= matchkeyword(s, keywords); |
| 206 |
} |
| 207 |
s = cast(char[])""; |
| 208 |
} |
| 209 |
return outstr; |
| 210 |
} |
| 211 |
|
| 212 |
void processconv() { |
| 213 |
char [][]kw; |
| 214 |
foreach(Converse c; conv) { |
| 215 |
kw = std.string.split(c.keyword); |
| 216 |
responsekeywords[c.index] ~= kw; |
| 217 |
foreach(char []w; kw) { |
| 218 |
keywordused[to!string(w)] = 0; |
| 219 |
} |
| 220 |
} |
| 221 |
foreach(size_t i, Response r; resp) { |
| 222 |
//strip out old keyword highlights |
| 223 |
r.response = join(std.string.split(r.response, "^"), ""); |
| 224 |
if(r.index in responsekeywords) { |
| 225 |
r.response = dokeyword(r.response, responsekeywords[r.index]); |
| 226 |
} else { |
| 227 |
printf("There is no matching key word for response index: %d\n", r.index); |
| 228 |
} |
| 229 |
resp[i] = r; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
void checkall() { |
| 234 |
foreach(kw; keywordused.keys.sort) { |
| 235 |
if(keywordused[kw] == 0 && !(kw in rootwords) && !(kw in ignorewords)) { |
| 236 |
printf("%s(%d):'%s' not used.\n", inputfile.toStringz, keywordlines[kw], kw.toStringz); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void dumpall() { |
| 242 |
foreach(Converse c; conv) { |
| 243 |
printf("%d, %d, %d, %d, %s\n", c.event, c.runevent, c.rcode, c.index, c.keyword.toStringz); |
| 244 |
} |
| 245 |
foreach(Response r; resp) { |
| 246 |
printf("%d, %s, %lu\n", r.index, r.response.toStringz, r.response.length); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void writefiles(char []file) { |
| 251 |
auto fhind = File(file ~ ".ind", "wb"); |
| 252 |
auto fhdat = File(file ~ ".dta", "wb"); |
| 253 |
ConverseRecord cr; |
| 254 |
ResponseRecord rr; |
| 255 |
char []s; |
| 256 |
cr.keyword[0..$] = 1; |
| 257 |
rr.response[0..$] = 1; |
| 258 |
foreach(Converse c; conv) { |
| 259 |
cr.event = c.event; |
| 260 |
cr.runevent = c.runevent; |
| 261 |
cr.rcode = c.rcode; |
| 262 |
cr.index = c.index; |
| 263 |
s = encodestring(" " ~ c.keyword ~ " "); |
| 264 |
if(s.length > cr.keyword.length) { |
| 265 |
printf("%s(%d): keyword too long, truncated: %s\n", inputfile.toStringz, c.linenum, c.keyword.toStringz); |
| 266 |
s.length = cr.keyword.length; |
| 267 |
} |
| 268 |
cr.keywordlength = to!ubyte(s.length); |
| 269 |
cr.keyword[0..s.length] = s[0..$]; |
| 270 |
fhind.rawWrite((&cr)[0..1]); |
| 271 |
} |
| 272 |
foreach(Response r; resp) { |
| 273 |
rr.index = r.index; |
| 274 |
s = encodestring(r.response); |
| 275 |
if(s.length > rr.response.length) { |
| 276 |
printf("%s(%d): response too long, truncated: %s\n", inputfile.toStringz, r.linenum, r.response.toStringz); |
| 277 |
s.length = rr.response.length; |
| 278 |
} |
| 279 |
rr.responselength = to!ubyte(s.length); |
| 280 |
rr.response[0..s.length] = s[0..$]; |
| 281 |
fhdat.rawWrite((&rr)[0..1]); |
| 282 |
} |
| 283 |
fhind.close(); |
| 284 |
fhdat.close(); |
| 285 |
} |
| 286 |
|
| 287 |
int main(char [][]arg) { |
| 288 |
parsefile(arg[1]); |
| 289 |
//dumpall(); |
| 290 |
processconv(); |
| 291 |
//dumpall(); |
| 292 |
checkall(); |
| 293 |
writefiles(arg[2]); |
| 294 |
return 0; |
| 295 |
} |