-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineDb.cs
104 lines (82 loc) · 2.44 KB
/
LineDb.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextRepls
{
class LineDb : IEnumerable<KeyValuePair<string, string>>, IEnumerator<KeyValuePair<string, string>>
{
private Dictionary<string, string> _db = new Dictionary<string, string>();
public LineDb(string ctx)
{
ParseLines(ctx);
}
public LineDb(string filename, Encoding enc)
{
string s = File.ReadAllText(filename, enc);
ParseLines(s);
}
private void ParseLines(string ctx)
{
_db.Clear();
var ss = ctx.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var l in ss)
{
var div = l.IndexOf('〓');
if (div <= 0)
continue;
var name = l.Substring(0, div);
if (name[0] == '#' || name.StartsWith("//"))
continue;
var value = l.Substring(div + 1);
_db.Add(name.Trim(), value.Trim());
}
}
public string Get(string name)
{
if (!_db.TryGetValue(name, out string value))
return name;
return value;
}
public bool Try(string name, out string value)
{
return _db.TryGetValue(name, out value);
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return _db.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _db.GetEnumerator();
}
public void Dispose()
{
}
public bool MoveNext()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
public int Count { get { return _db.Count; } }
public KeyValuePair<string, string> Current => throw new NotImplementedException();
object IEnumerator.Current => throw new NotImplementedException();
public string this[string index]
{
get
{
return Get(index);
}
}
public Dictionary<string, string> CloneDb()
{
return new Dictionary<string, string>(_db);
}
}
}