diff options
Diffstat (limited to 'OpenGridServices/OpenGrid.Framework.Data')
-rw-r--r-- | OpenGridServices/OpenGrid.Framework.Data/IniConfig.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/OpenGridServices/OpenGrid.Framework.Data/IniConfig.cs b/OpenGridServices/OpenGrid.Framework.Data/IniConfig.cs new file mode 100644 index 0000000..58597d2 --- /dev/null +++ b/OpenGridServices/OpenGrid.Framework.Data/IniConfig.cs | |||
@@ -0,0 +1,73 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.IO; | ||
5 | using System.Text.RegularExpressions; | ||
6 | |||
7 | /* | ||
8 | Taken from public code listing at by Alex Pinsker | ||
9 | http://alexpinsker.blogspot.com/2005/12/reading-ini-file-from-c_113432097333021549.html | ||
10 | */ | ||
11 | |||
12 | namespace OpenGrid.Framework.Data | ||
13 | { | ||
14 | /// <summary> | ||
15 | /// Parse settings from ini-like files | ||
16 | /// </summary> | ||
17 | public class IniFile | ||
18 | { | ||
19 | static IniFile() | ||
20 | { | ||
21 | _iniKeyValuePatternRegex = new Regex( | ||
22 | @"((\s)*(?<Key>([^\=^\s^\n]+))[\s^\n]* | ||
23 | # key part (surrounding whitespace stripped) | ||
24 | \= | ||
25 | (\s)*(?<Value>([^\n^\s]+(\n){0,1}))) | ||
26 | # value part (surrounding whitespace stripped) | ||
27 | ", | ||
28 | RegexOptions.IgnorePatternWhitespace | | ||
29 | RegexOptions.Compiled | | ||
30 | RegexOptions.CultureInvariant); | ||
31 | } | ||
32 | static private Regex _iniKeyValuePatternRegex; | ||
33 | |||
34 | public IniFile(string iniFileName) | ||
35 | { | ||
36 | _iniFileName = iniFileName; | ||
37 | } | ||
38 | |||
39 | public string ParseFileReadValue(string key) | ||
40 | { | ||
41 | using (StreamReader reader = | ||
42 | new StreamReader(_iniFileName)) | ||
43 | { | ||
44 | do | ||
45 | { | ||
46 | string line = reader.ReadLine(); | ||
47 | Match match = | ||
48 | _iniKeyValuePatternRegex.Match(line); | ||
49 | if (match.Success) | ||
50 | { | ||
51 | string currentKey = | ||
52 | match.Groups["Key"].Value as string; | ||
53 | if (currentKey != null && | ||
54 | currentKey.Trim().CompareTo(key) == 0) | ||
55 | { | ||
56 | string value = | ||
57 | match.Groups["Value"].Value as string; | ||
58 | return value; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | } | ||
63 | while (reader.Peek() != -1); | ||
64 | } | ||
65 | return null; | ||
66 | } | ||
67 | |||
68 | public string IniFileName | ||
69 | { | ||
70 | get { return _iniFileName; } | ||
71 | } private string _iniFileName; | ||
72 | } | ||
73 | } | ||