This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MappingReader.cs
130 lines (117 loc) · 4.49 KB
/
MappingReader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ProvinceMapper
{
class MappingReader
{
public Dictionary<string, List<IMapping>> mappings = new Dictionary<string, List<IMapping>>();
public string srcTag;
public string destTag;
private readonly string path;
private List<string> initialLines;
public MappingReader(string _path, string _srcTag, string _destTag)
{
srcTag = _srcTag;
destTag = _destTag;
path = _path;
initialLines = new List<string>();
}
public MappingReader(string _path, string _srcTag, string _destTag, List<Province> srcProvs, List<Province> destProvs, StatusUpdate su)
{
srcTag = _srcTag;
destTag = _destTag;
path = _path;
initialLines = new List<string>();
List<IMapping> currentMappings = new List<IMapping>();
string mappingName = "";
int level = 0;
StreamReader sr = new StreamReader(path, Encoding.GetEncoding(1252));
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (line.Length > 0)
{
if (level == 0)
{
if (line.Count(x => x == '{') > 0)
{
if (mappingName != "")
{
mappings.Add(mappingName, currentMappings);
currentMappings = new List<IMapping>();
}
string[] strings = line.Split('=');
mappingName = strings[0];
mappingName = mappingName.Trim();
}
else
{
initialLines.Add(line);
}
}
else if (line.StartsWith("link"))
{
try
{
currentMappings.Add(new ProvinceMapping(line, srcTag, destTag, srcProvs, destProvs, mappingName));
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message, "Error in mapping file");
}
level += line.Count(x => x == '{') - line.Count(x => x == '}');
}
else if (line.StartsWith("#"))
{
try
{
currentMappings.Add(new CommentMapping(line));
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message, "Error in mapping file");
}
}
else if (line.StartsWith("mappings") || line.StartsWith("{") || line.StartsWith("}"))
{
// ignore these lines
}
else
{
System.Windows.Forms.MessageBox.Show(String.Format("Error parsing province mapping file: line beginning '{0}' appears to be invalid.", line.Split(' ')[0]));
}
level += line.Count(x => x == '{') - line.Count(x => x == '}');
}
su(100.0 * sr.BaseStream.Position / sr.BaseStream.Length);
}
mappings.Add(mappingName, currentMappings);
sr.Close();
}
public void Write()
{
Write(path);
}
public void Write(string path)
{
StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding(1252));
foreach (string line in initialLines)
{
sw.WriteLine(line);
}
foreach (KeyValuePair<string, List<IMapping>> oneMapping in mappings)
{
sw.Write(oneMapping.Key);
sw.WriteLine(" = {");
foreach (IMapping m in oneMapping.Value)
{
sw.WriteLine(m.ToOutputString(srcTag, destTag));
}
sw.WriteLine("}");
}
sw.Close();
}
}
}