|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Linq; |
3 | | -using System.Runtime.CompilerServices; |
4 | 3 |
|
5 | 4 | using Rampastring.Tools; |
6 | 5 |
|
7 | 6 | namespace ClientCore.Extensions |
8 | 7 | { |
9 | 8 | public static class IniFileExtensions |
10 | 9 | { |
11 | | - // Clone() method is not officially available now. https://github.com/Rampastring/Rampastring.Tools/issues/12 |
12 | | - public static IniFile Clone(this IniFile oldIniFile) |
| 10 | + extension(IniFile iniFile) |
13 | 11 | { |
14 | | - var newIni = new IniFile(); |
15 | | - foreach (string sectionName in oldIniFile.GetSections()) |
| 12 | + // Clone() method is not officially available now. https://github.com/Rampastring/Rampastring.Tools/issues/12 |
| 13 | + public IniFile Clone() |
16 | 14 | { |
17 | | - IniSection oldSection = oldIniFile.GetSection(sectionName); |
18 | | - newIni.AddSection(oldSection.Clone()); |
19 | | - } |
20 | | - |
21 | | - return newIni; |
22 | | - } |
| 15 | + var newIni = new IniFile(); |
| 16 | + foreach (string sectionName in iniFile.GetSections()) |
| 17 | + { |
| 18 | + IniSection oldSection = iniFile.GetSection(sectionName); |
| 19 | + newIni.AddSection(oldSection.Clone()); |
| 20 | + } |
23 | 21 |
|
24 | | - public static IniSection Clone(this IniSection oldSection) |
25 | | - { |
26 | | - IniSection newSection = new(oldSection.SectionName); |
| 22 | + return newIni; |
| 23 | + } |
27 | 24 |
|
28 | | - foreach ((var key, var value) in oldSection.Keys) |
| 25 | + public IniSection GetOrAddSection(string sectionName) |
29 | 26 | { |
30 | | - newSection.AddKey(key, value); |
| 27 | + var section = iniFile.GetSection(sectionName); |
| 28 | + if (section != null) |
| 29 | + return section; |
| 30 | + |
| 31 | + section = new IniSection(sectionName); |
| 32 | + iniFile.AddSection(section); |
| 33 | + return section; |
31 | 34 | } |
32 | 35 |
|
33 | | - return newSection; |
| 36 | + public string[] GetStringListValue(string section, string key, string defaultValue, char[] separators = null) |
| 37 | + => (iniFile.GetSection(section)?.GetStringValue(key, defaultValue) ?? defaultValue) |
| 38 | + .SplitWithCleanup(); |
| 39 | + |
34 | 40 | } |
35 | 41 |
|
36 | | - public static IniSection GetOrAddSection(this IniFile iniFile, string sectionName) |
| 42 | + extension(IniSection iniSection) |
37 | 43 | { |
38 | | - var section = iniFile.GetSection(sectionName); |
39 | | - if (section != null) |
40 | | - return section; |
| 44 | + public IniSection Clone() |
| 45 | + { |
| 46 | + IniSection newSection = new(iniSection.SectionName); |
41 | 47 |
|
42 | | - section = new IniSection(sectionName); |
43 | | - iniFile.AddSection(section); |
44 | | - return section; |
45 | | - } |
| 48 | + foreach ((var key, var value) in iniSection.Keys) |
| 49 | + { |
| 50 | + newSection.AddKey(key, value); |
| 51 | + } |
46 | 52 |
|
47 | | - public static void RemoveAllKeys(this IniSection iniSection) |
48 | | - { |
49 | | - var keys = new List<KeyValuePair<string, string>>(iniSection.Keys); |
50 | | - foreach (KeyValuePair<string, string> iniSectionKey in keys) |
51 | | - iniSection.RemoveKey(iniSectionKey.Key); |
52 | | - } |
| 53 | + return newSection; |
| 54 | + } |
53 | 55 |
|
54 | | - public static string[] GetStringListValue(this IniFile iniFile, string section, string key, string defaultValue, char[] separators = null) |
55 | | - => (iniFile.GetSection(section)?.GetStringValue(key, defaultValue) ?? defaultValue) |
56 | | - .SplitWithCleanup(); |
| 56 | + public void RemoveAllKeys() |
| 57 | + { |
| 58 | + var keys = new List<KeyValuePair<string, string>>(iniSection.Keys); |
| 59 | + foreach (KeyValuePair<string, string> iniSectionKey in keys) |
| 60 | + iniSection.RemoveKey(iniSectionKey.Key); |
| 61 | + } |
| 62 | + } |
57 | 63 | } |
58 | 64 | } |
0 commit comments