/
Program.cs
276 lines (264 loc) · 13.3 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
namespace Kvitto
{
class Program
{
enum rxTypes
{
undefined,
tl,
mottagare,
tag1,
tag2,
tagslag,
korplan,
anm
};
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
if (args.Length < 2)
{
Console.WriteLine("Usage: Kvitto <katalog> <json-file>");
System.Environment.Exit(0);
}
int rxState = (int)rxTypes.undefined;
string jsonKvitto = "{\"tagPlaner\": [";
foreach (string filename in System.IO.Directory.EnumerateFiles(args[0]))
{
bool firstFrom = true;
Console.WriteLine("Filnamn: " + filename);
Console.WriteLine();
StreamReader sr = new StreamReader(filename);
string kvStr = sr.ReadToEnd();
string[] kvStrs = kvStr.Split(new char[] { '\n', '\r', '\t' });
Queue<string> kvQStrs = new Queue<string>();
foreach (string tmp in kvStrs)
{
if (!tmp.Equals(""))
kvQStrs.Enqueue(tmp);
}
string[] kvittoStrs = kvQStrs.ToArray();
// loop over From:/Från: until kvitoStr at end of string
foreach (string line in kvittoStrs)
{
int numDiv = 0;
IParsedPlan pPlan = new ParsedPlan();
IRunPlan rPlan = null;
IKvitto kvitto = new Kvitto();
Regex rxTL = new Regex(@"TL -- (?<tl>[0-9a-zA-ZåäöÅÄÖ:\. ]+) -- Trafikverket - NR (?<ordernr>[0-9]+) -- (?<datum>[0-9a-zA-ZåäöÅÄÖ:\. ]+) --");
Regex rxMottagare = new Regex(@"^(?<mottagare>MOTTAGARE:)");
Regex rxTag = new Regex(@"^TÅG (?<tagnr>[0-9]+) (?<typ>(Ska Gå|SKA INSTÄLLAS))");
Regex rxTagslag = new Regex(@"^TÅGSLAG (?<tagslag>[A-Z]+)");
Regex rxKorplan = new Regex(@"^(?<korplan>Körplan)");
Regex rxAnm = new Regex(@"^(?<end>ANM:)");
Regex rxNext = new Regex(@"^Från:");
while (true)
{
MatchCollection mc = rxTL.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.tl;
}
mc = rxMottagare.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.mottagare;
}
mc = rxTag.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.tag1;
}
mc = rxTagslag.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.tagslag;
}
mc = rxKorplan.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.korplan;
}
mc = rxAnm.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.anm;
}
mc = rxNext.Matches(line);
if (mc.Count > 0)
{
Console.WriteLine(line);
rxState = (int)rxTypes.undefined;
if (!firstFrom)
break; // start new Kvitto meddelande
firstFrom = false;
}
if (rxState == (int)rxTypes.tl)
{
MatchCollection tlMc = rxTL.Matches(line);
if (tlMc.Count > 0)
{
string tl = tlMc[0].Groups["tl"].Value;
string ordernr = tlMc[0].Groups["ordernr"].Value;
string datum = tlMc[0].Groups["datum"].Value;
kvitto.AddTL(tl, ordernr, datum);
}
}
else if (rxState == (int)rxTypes.mottagare)
{
Regex rxMottagarLista = new Regex(@"(?<mottagare>[0-9a-zA-ZåäöÅÄÖ ]+)[,]*");
MatchCollection mMc = rxMottagarLista.Matches(line);
if (mMc.Count > 0)
{
foreach (Match m in mMc)
{
if (!m.Groups["mottagare"].Value.Trim().Equals("MOTTAGARE"))
{
kvitto.AddMottagare(m.Groups["mottagare"].Value.Trim());
Console.Write(m.Groups["mottagare"].Value + "; ");
}
}
}
Console.WriteLine();
}
else if (rxState == (int)rxTypes.tag1)
{
MatchCollection tagMc = rxTag.Matches(line);
if (tagMc.Count > 0)
{
string tagnr = tagMc[0].Groups["tagnr"].Value.Trim();
string typ = tagMc[0].Groups["typ"].Value.Trim();
kvitto.AddTag(tagnr, typ);
rxState = (int)rxTypes.tag2;
}
Console.WriteLine(line);
}
else if (rxState == (int)rxTypes.tag2)
{
Regex rxTagInfo = new Regex(@"(?<fromtpl>[a-zA-ZåäöÅÄÖ ]+)\-(?<totpl>[a-zA-ZåäöÅÄÖ ]+)[ ]+(?<from>[0-9]+)\-(?<to>[0-9]+): (?<gangdagar>[A-Z\-]+)");
MatchCollection tInfoMc = rxTagInfo.Matches(line);
if (tInfoMc.Count > 0)
{
Kvitto.ITagInfo tagInfo = new Kvitto.Taginfo();
tagInfo.fromtpl = tInfoMc[0].Groups["fromtpl"].Value.Trim();
tagInfo.totpl = tInfoMc[0].Groups["totpl"].Value.Trim();
tagInfo.from = tInfoMc[0].Groups["from"].Value.Trim();
tagInfo.to = tInfoMc[0].Groups["to"].Value.Trim();
tagInfo.gangdagar = tInfoMc[0].Groups["gangdagar"].Value.Trim();
kvitto.AddTagInfo(tagInfo);
}
}
else if (rxState == (int)rxTypes.tagslag)
{
MatchCollection tagslagMc = rxTagslag.Matches(line);
if (tagslagMc.Count > 0)
{
kvitto.AddTagslag(tagslagMc[0].Groups["tagslag"].Value.Trim());
}
Console.WriteLine(line);
}
else if (rxState == (int)rxTypes.korplan)
{
Regex rxDiv = new Regex(@"(?<div>[\-]{4})");
Regex rxAct = new Regex(@"(Traf.utb|Obevakad|Tkl ger körtillstånd|Går från [a-zA-ZåäöÅÄÖ]+|K-Möter [0-9]+|K-Möter Ej [0-9]+|\([0-9]+\-[0-9]+:|På\/Av|Fjärrbevakad)");
Regex rxHpl = new Regex(@"(?<hpl>[a-zA-ZåäöÅÄÖ]+)");
Regex rxTid = new Regex(@"^(?<tid>[0-9]{4})");
MatchCollection kpMc = rxDiv.Matches(line);
if (kpMc.Count > 0) numDiv++;
if (numDiv == 2)
{
bool act = false;
kpMc = rxAct.Matches(line);
if (kpMc.Count > 0)
{
pPlan.Add("info", line);
act = true;
}
kpMc = rxHpl.Matches(line);
if (kpMc.Count > 0 && !act)
{
pPlan.Add("hpl", line);
}
kpMc = rxTid.Matches(line);
if (kpMc.Count > 0)
{
pPlan.Add("tid", line);
}
}
else if (numDiv == 3)
{
rPlan = new RunPlan(pPlan);
kvitto.AddRunPlan(rPlan);
RunPlan.IRun[] runs = rPlan.GetArray();
foreach (RunPlan.IRun run in runs)
{
Console.Write("Hpl: " + run.hpl + ", ");
foreach (string tid in run.getTimes())
Console.Write(tid + ", ");
foreach (string info in run.getInfos())
{
Console.Write(info + ", ");
Regex rxKMoter = new Regex(@"K-Möter (?<kmoter>[0-9]+)");
MatchCollection kmMc = rxKMoter.Matches(info);
if (kmMc.Count > 0)
{
Console.Write("K Möter tågnr: " + kmMc[0].Groups["kmoter"].Value + ", ");
}
else
{
Console.WriteLine("Omatchat: " + info);
}
}
Console.WriteLine();
}
}
else if (numDiv > 3)
{
Console.WriteLine("------------------------------------------------------------------------------");
}
}
else if (rxState == (int)rxTypes.anm)
{
Regex rxANM = new Regex(@"TÅG[ ]+(?<tagnr>[0-9]+)[ ]+(K\-Möter|K\-Möter Även)[ ]+(?<kmoternr>[0-9]+)[ ]+i[ ]+(?<hpl>[a-zA-ZåäöÅÄÖ]+)[ ]+(?<from>[0-9]+)(\-(?<to>[0-9]+)\:[ ]+(?<dag>[A-ZÅÄÖ]+)){0,1}");
MatchCollection anmMc = rxANM.Matches(line);
if (anmMc.Count > 0)
{
if (anmMc[0].Groups["to"].Value != null)
kvitto.AddAnm(anmMc[0].Groups["tagnr"].Value, anmMc[0].Groups["kmoternr"].Value, anmMc[0].Groups["hpl"].Value,
anmMc[0].Groups["from"].Value, anmMc[0].Groups["to"].Value, anmMc[0].Groups["dag"].Value);
else
kvitto.AddAnm(anmMc[0].Groups["tagnr"].Value, anmMc[0].Groups["kmoternr"].Value, anmMc[0].Groups["hpl"].Value,
anmMc[0].Groups["from"].Value);
Console.WriteLine(line);
}
//end_state = false;
}
}
jsonKvitto += kvitto.toJson() + ",";
Console.WriteLine(kvitto.toJson());
Console.WriteLine();
Console.WriteLine("##############################################################################################");
Console.WriteLine();
}
}
jsonKvitto = jsonKvitto.TrimEnd(new char[] { ',' });
jsonKvitto += "]}";
System.IO.StreamWriter sw = new StreamWriter(args[1]);
sw.Write(jsonKvitto);
sw.Close();
}
}
}