-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmudReader.cs
More file actions
442 lines (348 loc) · 9.55 KB
/
mudReader.cs
File metadata and controls
442 lines (348 loc) · 9.55 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace logedit
{
/// <summary>
///
/// </summary>
public class mudReader
{
// private variables
string priv_ansi;
string priv_rtf;
string priv_text;
string priv_html;
int priv_progress;
int priv_tags;
string priv_filename;
bool priv_working;
string priv_initialpath;
public string initialpath
{
get
{
return priv_initialpath;
}
set
{
priv_initialpath=value;
}
}
public bool working
{
get
{
return priv_working;
}
set
{
priv_working = value;
}
}
public string filename
{
get
{
return priv_filename;
}
set
{
priv_filename = value;
}
}
// private richTextBox to hold the rtf text
//private System.Windows.Forms.RichTextBox rtfBox;
EventArgs e = new EventArgs();
public Hashtable ca;
// = new Hashtable();
public ArrayList posA;
public Hashtable tagA;
public Hashtable replaceA;
public Hashtable colorA;
public Hashtable undefT;
private ArrayList removeNr;
// = new ArrayList();
public mudReader ()
{
priv_working=false;
priv_ansi="";
priv_filename="";
priv_rtf="";
priv_text="";
priv_html="";
priv_progress = 0;
priv_initialpath="C:";
colorA = new Hashtable();
//rtfBox = new RichTextBox();
colorA["\x1B"+"\x5B"+ "30"+"\x6D"]="\\cf1 ";
colorA["\x1B"+"\x5B"+"0;30"+"\x6D"]="\\cf1 ";
colorA["\x1B"+"\x5B"+"1;30"+"\x6D"]="\\cf2 ";
colorA["\x1B"+"\x5B"+ "31"+"\x6D"]="\\cf3 ";
colorA["\x1B"+"\x5B"+"0;31"+"\x6D"]="\\cf3 ";
colorA["\x1B"+"\x5B"+"1;31"+"\x6D"]="\\cf4 ";
colorA["\x1B"+"\x5B"+ "32"+"\x6D"]="\\cf5 ";
colorA["\x1B"+"\x5B"+"0;32"+"\x6D"]="\\cf5 ";
colorA["\x1B"+"\x5B"+"1;32"+"\x6D"]="\\cf6 ";
colorA["\x1B"+"\x5B"+ "33"+"\x6D"]="\\cf7 ";
colorA["\x1B"+"\x5B"+"0;33"+"\x6D"]="\\cf7 ";
colorA["\x1B"+"\x5B"+"1;33"+"\x6D"]="\\cf8 ";
colorA["\x1B"+"\x5B"+ "34"+"\x6D"]="\\cf9 ";
colorA["\x1B"+"\x5B"+"0;34"+"\x6D"]="\\cf9 ";
colorA["\x1B"+"\x5B"+"1;34"+"\x6D"]="\\cf10 ";
colorA["\x1B"+"\x5B"+ "35"+"\x6D"]="\\cf11 ";
colorA["\x1B"+"\x5B"+"0;35"+"\x6D"]="\\cf11 ";
colorA["\x1B"+"\x5B"+"1;35"+"\x6D"]="\\cf12 ";
colorA["\x1B"+"\x5B"+ "36"+"\x6D"]="\\cf13 ";
colorA["\x1B"+"\x5B"+"0;36"+"\x6D"]="\\cf13 ";
colorA["\x1B"+"\x5B"+"1;36"+"\x6D"]="\\cf14 ";
colorA["\x1B"+"\x5B"+ "37"+"\x6D"]="\\cf15 ";
colorA["\x1B"+"\x5B"+"0;37"+"\x6D"]="\\cf15 ";
colorA["\x1B"+"\x5B"+"1;37"+"\x6D"]="\\cf16 ";
// ANSI: [32mHp: 744(744) [0mGp: 460(460) Xp: 11869989 Socp: 1000
// VT100: Hp: 744(744) [37mGp: 460(460) Xp: 11869989 Socp: 1000[0m
//colorA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf5 "; // VIRKER VT100
colorA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf15 "; // VIRKER ANSI
replaceA = colorA;
removeNr = new ArrayList();
//fix backgrounds
for(int l=38;l<50;l++)
{
removeNr.Add(l);
}
//fix blink,reverse
for(int l=2;l<30;l++)
{
removeNr.Add(l);
}
removeNr.Add(0);
}
// properties
// private methods
public void remove(string inString)
{
//rtfBox.Rtf=priv_rtf;
/*
string searchString = inString;
Regex r = new Regex(searchString);
MatchCollection m = r.Matches(rtfBox.Text);
for (int i=m.Count-1;i>=0;i--)
{
rtfBox.SelectionStart=m[i].Index;
rtfBox.SelectionLength=m[i].Length;
rtfBox.SelectedText="";
}
priv_rtf=rtfBox.Rtf;
*/
}
public void replace(string inString,string newString)
{
/*
rtfBox.Rtf=priv_rtf;
string searchString = inString;
Regex r = new Regex(searchString);
MatchCollection m = r.Matches(rtfBox.Text);
for (int i=m.Count-1;i>=0;i--)
{
rtfBox.SelectionStart=m[i].Index;
rtfBox.SelectionLength=m[i].Length;
rtfBox.SelectedText=newString;
}
priv_rtf=rtfBox.Rtf;
*/
}
private void storeTags()
{
ca = new Hashtable();
posA = new ArrayList();
tagA = new Hashtable();
undefT = new Hashtable();
ArrayList tundefT = new ArrayList();
// colorA = new Hashtable();
//posA = null;
string ts = null;
string[] ta = null;
ts = priv_ansi;
string[] sp = ts.Split('\x1B');
priv_tags = sp.Length;
// START REGEX approach
Regex reg = new Regex("(\x1B\\[)"+
@"(?<tag>(\d|;)+)m");
//Match m = reg.Match(richTextBox1.Text);
MatchCollection m = reg.Matches(priv_ansi);
ts=reg.Replace(priv_ansi,"");
for (int i=0;i<m.Count;i++)
{
undefT[m[i].ToString()]=true;
//Form1.textBox1.Text += m[i]+"\r\n";
}
ICollection rKeys = replaceA.Keys;
foreach(string key in rKeys)
{
undefT.Remove(key);
}
//undefT.Remove(replaceA[]);
// END REGEX approach
// loop the undefT
ICollection keys = undefT.Keys;
foreach(string key in keys)
{
string newKey = key.Substring(0,2);
string tk = key.Substring(2,key.Length-3);
string[] tkA = tk.Split(';');
ArrayList tkA2 = new ArrayList();
foreach (string nrs in tkA)
{
tkA2.Add((int) Int16.Parse(nrs));
}
for(int nr=0;nr<tkA2.Count;nr++)
{
// check for big numbers etc
while((int) tkA2[nr]>=50)
{
tkA2[nr]=(int) tkA2[nr]-20;
if((int)tkA2[0]!=1)
{
tkA2.Insert(0,(int) 1);
}
}
}
// remove stupid 0
tkA2.Remove(0);
// remove blink
tkA2.Remove(5);
// remove backgrounds
for(int nr=38;nr<50;nr++)
{
tkA2.Remove(nr);
}
for(int nr=0;nr<tkA2.Count;nr++)
{
newKey += tkA2[nr].ToString()+";";
}
newKey=newKey.Substring(0,newKey.Length-1)+"m";
if(replaceA[newKey]==null)
{
replaceA[key]="\\cf15"; // undefinded!!
}
else
{
replaceA[key]=replaceA[newKey];
tundefT.Add(key);
}
}
foreach(string key in tundefT)
{
undefT.Remove(key);
}
ICollection mKey = colorA.Keys;
foreach(string key in mKey)
{
undefT.Remove(key);
}
priv_text=ts;
//return priv_text;
}
private void ansiToRtf()
{
string ts = "";
ts = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Courier New;}}\n";
ts += "{\\colortbl ;"+
"\\red100\\green100\\blue100;"+ // black 0;30 \cf1
"\\red100\\green100\\blue100;"+ // black bold 1;30 \cf2
"\\red128\\green0\\blue0;"+ // red 0;31 \cf3
"\\red255\\green0\\blue0;"+ // red bold 1;31 \cf4
"\\red0\\green128\\blue0;"+ // green 0;32 \cf5
"\\red0\\green255\\blue0;"+ // green bold 1;32 \cf6
"\\red136\\green136\\blue0;"+ // yellow 0;33 \cf7
"\\red255\\green255\\blue0;"+ // yellow bold 1;33 \cf8
"\\red0\\green0\\blue128;"+ // blue 0;34 \cf9
"\\red0\\green0\\blue255;"+ // blue bold 1;34 \cf10
"\\red136\\green0\\blue136;"+ // magenta 0;35 \cf11
"\\red255\\green0\\blue255;"+ // magenta bold 1;35 \cf12
"\\red0\\green136\\blue136;"+ // cyan 0;36 \cf13
"\\red0\\green255\\blue255;"+ // cyan bold 1;36 \cf14
"\\red192\\green192\\blue192;"+ // white 0;37 \cf15
"\\red255\\green255\\blue255;"+ // white bold 1;37 \cf16
"}\n";
ts += "\\viewkind4\\uc1\\pard\\cf15\\f0\\fs18 ";
string tempS = priv_ansi;
// count number of green vs number of gray tags
//colorA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf5 "; // VIRKER VT100
//colorA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf15 "; // VIRKER ANSI
Regex tr = new Regex("\\x1B"+"\\x5B"+"32"+"\\x6D");
MatchCollection m = tr.Matches(tempS);
int count32 = m.Count;
Debug.WriteLine("Count 32m: "+m.Count);
tr = new Regex("\\x1B"+"\\x5B"+"37"+"\\x6D");
m = tr.Matches(tempS);
int count37 = m.Count;
Debug.WriteLine("Count 37m: "+m.Count);
// check if VT100
string defcolor = "37";
if(count37>count32)
{
Debug.WriteLine("Assuming VT100 protocol has been used.");
replaceA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf5 "; // VIRKER VT100
colorA["\x1B"+"\x5B"+"0"+"\x6D"]="\\cf5 "; // VIRKER VT100
defcolor = "32";
}
else
{
Debug.WriteLine("Assuming ANSI protocol has been used.");
}
// insert GRAY color in beginning of each line to support ansi terminal
tempS = tempS.Replace("\n","\\par\n"+"\x1B"+"\x5B"+defcolor+"m");
ICollection keys = replaceA.Keys;
foreach(string key in keys)
{
//tempS = tempS+="Replaced\n";
tempS = tempS.Replace(key,(string) colorA[key]);
}
ts += tempS;
ts += "}";
priv_rtf=ts;
//rtfBox.Rtf=priv_rtf;
//return ts;
}
// public methods
public int Progress
{
get
{
return priv_progress;
}
}
public int getProgress()
{
return priv_progress;
}
public string getText()
{
//priv_text=rtfBox.Text;
return priv_text;
}
public void setRtf(string inString)
{
priv_rtf=inString;
//rtfBox.Rtf=priv_rtf;
}
public string getRtf()
{
return priv_rtf;
}
public string getAnsi()
{
return priv_ansi;
}
public void setAnsi(string inString)
{
priv_ansi = "\x1B"+"\x5B"+"0m"+inString;
//priv_ansi = inString;
storeTags();
ansiToRtf();
}
}
}