root / HAgent / Horizon(TCP, HTTP) / Horizon / Ini.cs
이력 | 보기 | 이력해설 | 다운로드 (5.96 KB)
| 1 | 40 | HKM | using System; |
|---|---|---|---|
| 2 | using System.Collections.Generic; |
||
| 3 | using System.IO; |
||
| 4 | using System.Linq; |
||
| 5 | using System.Text; |
||
| 6 | |||
| 7 | public class Ini |
||
| 8 | {
|
||
| 9 | Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase); |
||
| 10 | string file; |
||
| 11 | |||
| 12 | /// <summary> |
||
| 13 | /// Initialize an INI file |
||
| 14 | /// Load it if it exists |
||
| 15 | /// </summary> |
||
| 16 | /// <param name="file">Full path where the INI file has to be read from or written to</param> |
||
| 17 | public Ini(string file) |
||
| 18 | {
|
||
| 19 | this.file = file; |
||
| 20 | |||
| 21 | if (!File.Exists(file)) |
||
| 22 | return; |
||
| 23 | |||
| 24 | Load(); |
||
| 25 | } |
||
| 26 | |||
| 27 | /// <summary> |
||
| 28 | /// Load the INI file content |
||
| 29 | /// </summary> |
||
| 30 | public void Load() |
||
| 31 | {
|
||
| 32 | var txt = File.ReadAllText(file); |
||
| 33 | |||
| 34 | Dictionary<string, string> currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); |
||
| 35 | |||
| 36 | ini[""] = currentSection; |
||
| 37 | |||
| 38 | foreach (var l in txt.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)
|
||
| 39 | .Select((t, i) => new |
||
| 40 | {
|
||
| 41 | idx = i, |
||
| 42 | text = t.Trim() |
||
| 43 | })) |
||
| 44 | // .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith(";")))
|
||
| 45 | {
|
||
| 46 | var line = l.text; |
||
| 47 | |||
| 48 | if (line.StartsWith(";") || string.IsNullOrWhiteSpace(line))
|
||
| 49 | {
|
||
| 50 | currentSection.Add(";" + l.idx.ToString(), line);
|
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | if (line.StartsWith("[") && line.EndsWith("]"))
|
||
| 55 | {
|
||
| 56 | currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); |
||
| 57 | ini[line.Substring(1, line.Length - 2)] = currentSection; |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | |||
| 61 | var idx = line.IndexOf("=");
|
||
| 62 | if (idx == -1) |
||
| 63 | currentSection[line] = ""; |
||
| 64 | else |
||
| 65 | currentSection[line.Substring(0, idx)] = line.Substring(idx + 1); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /// <summary> |
||
| 70 | /// Get a parameter value at the root level |
||
| 71 | /// </summary> |
||
| 72 | /// <param name="key">parameter key</param> |
||
| 73 | /// <returns></returns> |
||
| 74 | public string GetValue(string key) |
||
| 75 | {
|
||
| 76 | return GetValue(key, "", ""); |
||
| 77 | } |
||
| 78 | |||
| 79 | /// <summary> |
||
| 80 | /// Get a parameter value in the section |
||
| 81 | /// </summary> |
||
| 82 | /// <param name="key">parameter key</param> |
||
| 83 | /// <param name="section">section</param> |
||
| 84 | /// <returns></returns> |
||
| 85 | public string GetValue(string key, string section) |
||
| 86 | {
|
||
| 87 | return GetValue(key, section, ""); |
||
| 88 | } |
||
| 89 | |||
| 90 | /// <summary> |
||
| 91 | /// Returns a parameter value in the section, with a default value if not found |
||
| 92 | /// </summary> |
||
| 93 | /// <param name="key">parameter key</param> |
||
| 94 | /// <param name="section">section</param> |
||
| 95 | /// <param name="default">default value</param> |
||
| 96 | /// <returns></returns> |
||
| 97 | public string GetValue(string key, string section, string @default) |
||
| 98 | {
|
||
| 99 | if (!ini.ContainsKey(section)) |
||
| 100 | return @default; |
||
| 101 | |||
| 102 | if (!ini[section].ContainsKey(key)) |
||
| 103 | return @default; |
||
| 104 | |||
| 105 | return ini[section][key]; |
||
| 106 | } |
||
| 107 | |||
| 108 | /// <summary> |
||
| 109 | /// Save the INI file |
||
| 110 | /// </summary> |
||
| 111 | public void Save() |
||
| 112 | {
|
||
| 113 | var sb = new StringBuilder(); |
||
| 114 | foreach (var section in ini) |
||
| 115 | {
|
||
| 116 | if (section.Key != "") |
||
| 117 | {
|
||
| 118 | sb.AppendFormat("[{0}]", section.Key);
|
||
| 119 | sb.AppendLine(); |
||
| 120 | } |
||
| 121 | |||
| 122 | foreach (var keyValue in section.Value) |
||
| 123 | {
|
||
| 124 | if (keyValue.Key.StartsWith(";"))
|
||
| 125 | {
|
||
| 126 | sb.Append(keyValue.Value); |
||
| 127 | sb.AppendLine(); |
||
| 128 | } |
||
| 129 | else |
||
| 130 | {
|
||
| 131 | sb.AppendFormat("{0}={1}", keyValue.Key, keyValue.Value);
|
||
| 132 | sb.AppendLine(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if (!endWithCRLF(sb)) |
||
| 137 | sb.AppendLine(); |
||
| 138 | } |
||
| 139 | |||
| 140 | File.WriteAllText(file, sb.ToString()); |
||
| 141 | } |
||
| 142 | |||
| 143 | bool endWithCRLF(StringBuilder sb) |
||
| 144 | {
|
||
| 145 | if (sb.Length < 4) |
||
| 146 | return sb[sb.Length - 2] == '\r' && |
||
| 147 | sb[sb.Length - 1] == '\n'; |
||
| 148 | else |
||
| 149 | return sb[sb.Length - 4] == '\r' && |
||
| 150 | sb[sb.Length - 3] == '\n' && |
||
| 151 | sb[sb.Length - 2] == '\r' && |
||
| 152 | sb[sb.Length - 1] == '\n'; |
||
| 153 | } |
||
| 154 | |||
| 155 | /// <summary> |
||
| 156 | /// Write a parameter value at the root level |
||
| 157 | /// </summary> |
||
| 158 | /// <param name="key">parameter key</param> |
||
| 159 | /// <param name="value">parameter value</param> |
||
| 160 | public void WriteValue(string key, string value) |
||
| 161 | {
|
||
| 162 | WriteValue(key, "", value); |
||
| 163 | } |
||
| 164 | |||
| 165 | /// <summary> |
||
| 166 | /// Write a parameter value in a section |
||
| 167 | /// </summary> |
||
| 168 | /// <param name="key">parameter key</param> |
||
| 169 | /// <param name="section">section</param> |
||
| 170 | /// <param name="value">parameter value</param> |
||
| 171 | public void WriteValue(string key, string section, string value) |
||
| 172 | {
|
||
| 173 | Dictionary<string, string> currentSection; |
||
| 174 | if (!ini.ContainsKey(section)) |
||
| 175 | {
|
||
| 176 | currentSection = new Dictionary<string, string>(); |
||
| 177 | ini.Add(section, currentSection); |
||
| 178 | } |
||
| 179 | else |
||
| 180 | currentSection = ini[section]; |
||
| 181 | |||
| 182 | currentSection[key] = value; |
||
| 183 | } |
||
| 184 | |||
| 185 | /// <summary> |
||
| 186 | /// Get all the keys names in a section |
||
| 187 | /// </summary> |
||
| 188 | /// <param name="section">section</param> |
||
| 189 | /// <returns></returns> |
||
| 190 | public string[] GetKeys(string section) |
||
| 191 | {
|
||
| 192 | if (!ini.ContainsKey(section)) |
||
| 193 | return new string[0]; |
||
| 194 | |||
| 195 | return ini[section].Keys.ToArray(); |
||
| 196 | } |
||
| 197 | |||
| 198 | /// <summary> |
||
| 199 | /// Get all the section names of the INI file |
||
| 200 | /// </summary> |
||
| 201 | /// <returns></returns> |
||
| 202 | public string[] GetSections() |
||
| 203 | {
|
||
| 204 | return ini.Keys.Where(t => t != "").ToArray(); |
||
| 205 | } |
||
| 206 | } |