| 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: Computer 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.regex; |
| 28 |
import std.conv; |
| 29 |
import std.string; |
| 30 |
import std.range; |
| 31 |
import data; |
| 32 |
|
| 33 |
char []inputfile; |
| 34 |
|
| 35 |
struct Log { |
| 36 |
int titleline; |
| 37 |
int id; |
| 38 |
char []title; |
| 39 |
char [][]head; |
| 40 |
char [][]tail; |
| 41 |
char [][]output; |
| 42 |
} |
| 43 |
|
| 44 |
Log []loglist; |
| 45 |
|
| 46 |
void parsefile(char []file) { |
| 47 |
auto fh = File(file, "r"); |
| 48 |
inputfile = file; |
| 49 |
|
| 50 |
auto titlereg = regex("^@(-?\\d+)\\s+(.+)$", "g"); |
| 51 |
auto sepreg = regex("##\\s*", "g"); |
| 52 |
|
| 53 |
Log log; |
| 54 |
|
| 55 |
//printf ("Parsing file %s\n", inputfile.toStringz); |
| 56 |
int started = 0; |
| 57 |
int head; |
| 58 |
int num = 0; |
| 59 |
foreach(line; fh.byLine) { |
| 60 |
num++; |
| 61 |
line = detab(line.dup); |
| 62 |
//printf (" parsing line: %s\n", line.toStringz); |
| 63 |
auto title_match = match(line, titlereg); |
| 64 |
if(title_match) { |
| 65 |
if(started) { |
| 66 |
loglist ~= log; |
| 67 |
} else { |
| 68 |
started = 1; |
| 69 |
} |
| 70 |
head = 1; |
| 71 |
log.id = to!short(title_match.captures[1]); |
| 72 |
log.titleline = num; |
| 73 |
log.title = title_match.captures[2].dup; |
| 74 |
log.head = [];//.length = 0; |
| 75 |
log.tail = [];//.length = 0; |
| 76 |
//printf (" matched title: id=%d, line=%d, title=%s\n", log.id, log.titleline, log.title.toStringz); |
| 77 |
} else if(match(line,sepreg)) { |
| 78 |
head = 0; |
| 79 |
//printf (" matched separator: %s\n", line.toStringz); |
| 80 |
} else { |
| 81 |
//printf (" continuation line: %s\n", line.toStringz); |
| 82 |
if(started) { |
| 83 |
if(head) { |
| 84 |
log.head ~= line.dup; |
| 85 |
} else { |
| 86 |
log.tail ~= line.dup; |
| 87 |
} |
| 88 |
} else { |
| 89 |
printf("%s(%d): text before first title!: %s\n", inputfile.toStringz, num, line.toStringz); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
if(started) { |
| 94 |
loglist ~= log; |
| 95 |
} else { |
| 96 |
printf("%s(%d): No log entries!\n", inputfile.toStringz, num); |
| 97 |
} |
| 98 |
//printf ("Done parsing file.\n\n"); |
| 99 |
} |
| 100 |
|
| 101 |
char [][]wraplines(char [][]text, int width) { |
| 102 |
char [][]output; |
| 103 |
int i, j; |
| 104 |
foreach(char []line; text) { |
| 105 |
while(line.length > width) { |
| 106 |
if(line[width] == ' ') { |
| 107 |
for(i = width; i < line.length && line[i] == ' '; i++) { |
| 108 |
/*do nothing*/ |
| 109 |
} |
| 110 |
i--; //adjust i so it points to the last space character. |
| 111 |
for(j = width; j > 0 && line[j] == ' '; j--) { |
| 112 |
/*do nothing*/ |
| 113 |
} |
| 114 |
} else { |
| 115 |
for(i = width; i > 0 && line[i] != ' '; i--) { |
| 116 |
/*do nothing*/ |
| 117 |
} |
| 118 |
for(j = i; j > 0 && line[j] == ' '; j--) { |
| 119 |
/*do nothing*/ |
| 120 |
} |
| 121 |
if(j == 0) { |
| 122 |
j = width; |
| 123 |
i = width - 1; |
| 124 |
} |
| 125 |
} |
| 126 |
output ~= line[0..j + 1]; |
| 127 |
line = line[i + 1..$]; |
| 128 |
} |
| 129 |
output ~= line; |
| 130 |
} |
| 131 |
return output; |
| 132 |
} |
| 133 |
|
| 134 |
char [][]trimouterblanks(char [][]input) { |
| 135 |
char [][]output = input; |
| 136 |
while(output.length && strip(output[0]).length == 0) { |
| 137 |
output = output[1..$]; |
| 138 |
} |
| 139 |
while(output.length && strip(output[$ - 1]).length == 0) { |
| 140 |
output = output[0..$ - 1]; |
| 141 |
} |
| 142 |
return output; |
| 143 |
} |
| 144 |
|
| 145 |
void processlogs() { |
| 146 |
char [][]output; |
| 147 |
//printf ("Processing logs\n"); |
| 148 |
foreach(size_t i, Log log; loglist) { |
| 149 |
//printf("%d:%d:[%s]\n", log.id, log.title.length, log.title.toStringz); |
| 150 |
log.head = wraplines(log.head, 49); |
| 151 |
//printf(".\n"); |
| 152 |
log.tail = wraplines(log.tail, 49); |
| 153 |
//printf(".\n"); |
| 154 |
log.head = trimouterblanks(log.head); |
| 155 |
//printf(".\n"); |
| 156 |
log.tail = trimouterblanks(log.tail); |
| 157 |
//printf(".\n"); |
| 158 |
if(log.head.length + log.tail.length > 25) { |
| 159 |
printf("%s(%d): Text is too long for the log!\n", inputfile.toStringz, log.titleline); |
| 160 |
output = (log.head ~ log.tail)[0..25]; |
| 161 |
} else { |
| 162 |
output.length = 25 - (log.head.length + log.tail.length); |
| 163 |
//printf (" head=>%s<\n output[%d]=%s\n tail=>%s<\n", to!string(log.head).toStringz, output.length, to!string(output).toStringz, to!string(log.tail).toStringz); |
| 164 |
output[0..$] = cast(char[])""; |
| 165 |
output = log.head ~ output ~ log.tail; |
| 166 |
//printf("X\n"); |
| 167 |
} |
| 168 |
int j; |
| 169 |
for(j = 0; j < output.length; j++) { |
| 170 |
//printf("1> %d:[%s]\n", output[j].length, output[j].toStringz); |
| 171 |
output[j] ~= " ".replicate(49 - output[j].length); |
| 172 |
//printf("2> %d:[%s]\n", output[j].length, output[j].toStringz); |
| 173 |
//printf("-\n"); |
| 174 |
} |
| 175 |
log.output = output.dup; |
| 176 |
//printf(".\n"); |
| 177 |
log.title = log.title ~ " ".replicate(49 - log.title.length); |
| 178 |
//printf(".\n"); |
| 179 |
loglist[i] = log; |
| 180 |
//printf("\n"); |
| 181 |
} |
| 182 |
//printf ("Logs processed\n"); |
| 183 |
} |
| 184 |
|
| 185 |
void writefiles(char []titlefile, char []logfile) { |
| 186 |
//printf ("\nWriting files: titles=%s and logs=%s\n", titlefile.toStringz, logfile.toStringz); |
| 187 |
auto fhtitles = File(titlefile, "wb"); |
| 188 |
auto fhlogs = File(logfile, "wb"); |
| 189 |
TitleRecord tr; |
| 190 |
LogRecord lr; |
| 191 |
char []s; |
| 192 |
int i; |
| 193 |
foreach(Log log; loglist) { |
| 194 |
tr.id = to!short(log.id); |
| 195 |
tr.text(encodestring(log.title)); |
| 196 |
for(i = 0; i < 25; i++) { |
| 197 |
//printf("1> %d:[%s]\n", log.output[i].length, log.output[i].toStringz); |
| 198 |
s = encodestring(log.output[i]); |
| 199 |
//printf("2> %d:[%s]\n", s.length, s.toStringz); |
| 200 |
lr.text[i](s); |
| 201 |
//printf("3> %d:[%s]\n", lr.text[i].length, (cast(char [])lr.text[i]).toStringz); |
| 202 |
} |
| 203 |
fhtitles.rawWrite((&tr)[0..1]); |
| 204 |
//printf ("title_%04d %d:[%s]\n\n", tr.id, tr.text.data.length, tr.text.data.toStringz); |
| 205 |
fhlogs.rawWrite((&lr)[0..1]); |
| 206 |
} |
| 207 |
//printf ("Done writing files.\n"); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
int main(char [][]arg) { |
| 212 |
parsefile(arg[1]); |
| 213 |
processlogs(); |
| 214 |
writefiles(arg[2], arg[3]); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|