forked from askeladdk/aiedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.cs
83 lines (74 loc) · 1.95 KB
/
IniParser.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace AIEdit
{
using IniDictionary = Dictionary<string, OrderedDictionary>;
public class IniParser
{
/**
* New parser reads .ini into dictionary.
*/
public static IniDictionary ParseToDictionary(StreamReader stream, string filename, Logger logger)
{
IniDictionary ini = new IniDictionary();
OrderedDictionary section = null;
string key, val, line;
int index;
int linenr = 0;
string sectionKey = "";
while( (line = stream.ReadLine()) != null )
{
linenr++;
// strip off whitespaces and comments
line = line.TrimStart();
if( (index = line.IndexOf(';')) != -1 )
{
line = line.Substring(0, index).TrimEnd();
}
// skip empty lines
if(line.Length == 0)
{
continue;
}
// start of section
else if(line[0] == '[')
{
if( (index = line.IndexOf(']')) == -1 ) continue;
sectionKey = line.Substring(1, index - 1);
section = new OrderedDictionary();
if (!ini.ContainsKey(sectionKey))
{
ini.Add(sectionKey, section);
}
else
{
logger.Add("Duplicate section [" + sectionKey + "] in " + filename + "!");
}
}
// key=value pair
else if(section != null)
{
if ((index = line.IndexOf('=')) == -1) continue;
key = line.Substring(0, index).Trim();
val = line.Substring(index + 1).Trim();
if (section.Contains(key))
{
logger.Add("Duplicate tag/index [" + sectionKey + "] => " + key + " in " + filename + "!");
}
section[key] = val;
}
}
return ini;
}
public static IniDictionary ParseToDictionary(string path, Logger logger)
{
StreamReader reader = new StreamReader(path);
string[] fullname = path.Split('\\');
IniDictionary d = ParseToDictionary(reader, fullname[fullname.Length - 1], logger);
reader.Close();
return d;
}
}
}