-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAskBigString.cs
172 lines (149 loc) · 5.67 KB
/
AskBigString.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddressLibraryManager
{
public partial class AskBigString : Form
{
public AskBigString()
{
InitializeComponent();
}
internal enum BigStringTypes : int
{
OffsetMap,
NameMap,
}
internal BigStringTypes BigStringType;
internal object BigStringResult;
internal object GetBigString(ref string error)
{
SortedDictionary<ulong, uint> offsets = new SortedDictionary<ulong, uint>();
SortedDictionary<ulong, string> names = new SortedDictionary<ulong, string>();
string text = this.textBox1.Text;
var spl = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (int i = 0; i < spl.Length; i++)
{
var l = spl[i].Trim();
if (l.Length == 0)
continue;
int ix = l.IndexOf(' ');
if(ix < 0)
{
error = "Invalid format on line " + (i + 1) + ": " + spl[i];
return null;
}
ulong id;
if(!ulong.TryParse(l.Substring(0, ix), System.Globalization.NumberStyles.None, null, out id))
{
error = "Invalid format on line " + (i + 1) + ": " + spl[i];
return null;
}
string k = l.Substring(ix + 1).Trim();
switch (this.BigStringType)
{
case BigStringTypes.OffsetMap:
{
uint off;
if(!uint.TryParse(k, System.Globalization.NumberStyles.AllowHexSpecifier, null, out off))
{
error = "Invalid format on line " + (i + 1) + ": " + spl[i];
return null;
}
if(offsets.ContainsKey(id))
{
error = "Duplicate identifier on line " + (i + 1) + ": " + id;
return null;
}
offsets[id] = off;
}
break;
case BigStringTypes.NameMap:
{
if(k.Length == 0)
{
error = "Invalid format on line " + (i + 1) + ": " + spl[i];
return null;
}
if(names.ContainsKey(id))
{
error = "Duplicate identifier on line " + (i + 1) + ": " + id;
return null;
}
names[id] = k;
}
break;
default:
throw new NotImplementedException();
}
}
switch(this.BigStringType)
{
case BigStringTypes.NameMap: return names;
case BigStringTypes.OffsetMap: return offsets;
default:
throw new NotImplementedException();
}
}
internal void SetBigString(object value)
{
var str = new StringBuilder();
switch(this.BigStringType)
{
case BigStringTypes.OffsetMap:
{
SortedDictionary<ulong, uint> offsets = (SortedDictionary<ulong, uint>)value;
if(offsets != null)
{
foreach(var pair in offsets)
{
str.Append(pair.Key.ToString());
str.Append(' ');
str.Append(pair.Value.ToString("X"));
str.AppendLine();
}
}
}
break;
case BigStringTypes.NameMap:
{
SortedDictionary<ulong, string> names = (SortedDictionary<ulong, string>)value;
if (names != null)
{
foreach (var pair in names)
{
if (string.IsNullOrEmpty(pair.Value))
continue;
str.Append(pair.Key.ToString());
str.Append(' ');
str.Append(pair.Value);
str.AppendLine();
}
}
}
break;
default:
throw new NotImplementedException();
}
this.textBox1.Text = str.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
string error = null;
this.BigStringResult = this.GetBigString(ref error);
if(error != null)
{
MessageBox.Show(error, "Error", MessageBoxButtons.OK);
return;
}
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}