-
Notifications
You must be signed in to change notification settings - Fork 0
/
People.cs
125 lines (105 loc) · 3.71 KB
/
People.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace UnfuddleBackupParser
{
class People
{
struct Person
{
public string id;
public string userName;
public string firstName;
public string lastName;
}
SortedDictionary<int, Person> m_people;
Dictionary<string, string> m_nameMappings = new Dictionary<string, string>();
public People(XmlElement element)
{
m_people = new SortedDictionary<int, Person>();
parse(element);
}
public void UseNameMappings(string path)
{
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(path, Encoding.Unicode))
{
while (!sr.EndOfStream)
{
sb.Append(sr.ReadLine());
sb.Append(',');
}
}
string csv = sb.ToString();
string[] splitResult = csv.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
m_nameMappings.Clear();
for (int i = 0; i < splitResult.Length; )
{
string name = splitResult[i++].Trim();
string newName = splitResult[i++].Trim();
m_nameMappings.Add(name, newName);
}
}
public string GetPersonName(string id)
{
if (string.IsNullOrEmpty(id))
return null;
int nameId = Convert.ToInt32(id);
Person p = m_people[nameId];
string name = p.firstName + " " + p.lastName;
name = name.Trim();
if (m_nameMappings.ContainsKey(name))
return m_nameMappings[name];
return name;
}
public void Save(string path)
{
StringBuilder sb = new StringBuilder();
addPerson(sb, "id", "userName", "fullName");
foreach (KeyValuePair<int, Person> pair in m_people)
{
Person person = pair.Value;
addPerson(sb, person.id, person.userName, GetPersonName(person.id));
}
File.WriteAllText(path, sb.ToString(), Encoding.Unicode);
}
private void parse(XmlElement element)
{
foreach (XmlNode node in element.ChildNodes)
{
Person person = new Person();
foreach (XmlNode child in node.ChildNodes)
{
switch (child.Name)
{
case "id":
person.id = child.InnerText;
break;
case "username":
person.userName = child.InnerText;
break;
case "first-name":
person.firstName = child.InnerText;
break;
case "last-name":
person.lastName = child.InnerText;
break;
}
}
m_people.Add(Convert.ToInt32(person.id), person);
}
}
private void addPerson(StringBuilder sb, string id, string userName, string fullName)
{
sb.Append(id);
sb.Append(',');
sb.Append(userName);
sb.Append(',');
sb.Append(fullName);
sb.Append(',');
sb.Append('\n');
}
}
}