|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace GitCredentialManager |
| 8 | +{ |
| 9 | + public class IniFile |
| 10 | + { |
| 11 | + public IniFile() |
| 12 | + { |
| 13 | + Sections = new Dictionary<IniSectionName, IniSection>(); |
| 14 | + } |
| 15 | + |
| 16 | + public IDictionary<IniSectionName, IniSection> Sections { get; } |
| 17 | + |
| 18 | + public bool TryGetSection(string name, string subName, out IniSection section) |
| 19 | + { |
| 20 | + return Sections.TryGetValue(new IniSectionName(name, subName), out section); |
| 21 | + } |
| 22 | + |
| 23 | + public bool TryGetSection(string name, out IniSection section) |
| 24 | + { |
| 25 | + return Sections.TryGetValue(new IniSectionName(name), out section); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + [DebuggerDisplay("{DebuggerDisplay}")] |
| 30 | + public readonly struct IniSectionName : IEquatable<IniSectionName> |
| 31 | + { |
| 32 | + public IniSectionName(string name, string subName = null) |
| 33 | + { |
| 34 | + Name = name; |
| 35 | + SubName = string.IsNullOrEmpty(subName) ? null : subName; |
| 36 | + } |
| 37 | + |
| 38 | + public string Name { get; } |
| 39 | + |
| 40 | + public string SubName { get; } |
| 41 | + |
| 42 | + public bool Equals(IniSectionName other) |
| 43 | + { |
| 44 | + // Main section name is case-insensitive, but subsection name IS case-sensitive! |
| 45 | + return StringComparer.OrdinalIgnoreCase.Equals(Name, other.Name) && |
| 46 | + StringComparer.Ordinal.Equals(SubName, other.SubName); |
| 47 | + } |
| 48 | + |
| 49 | + public override bool Equals(object obj) |
| 50 | + { |
| 51 | + return obj is IniSectionName other && Equals(other); |
| 52 | + } |
| 53 | + |
| 54 | + public override int GetHashCode() |
| 55 | + { |
| 56 | + unchecked |
| 57 | + { |
| 58 | + return ((Name != null ? Name.ToLowerInvariant().GetHashCode() : 0) * 397) ^ |
| 59 | + (SubName != null ? SubName.GetHashCode() : 0); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private string DebuggerDisplay => SubName is null ? Name : $"{Name} \"{SubName}\""; |
| 64 | + } |
| 65 | + |
| 66 | + [DebuggerDisplay("{DebuggerDisplay}")] |
| 67 | + public class IniSection |
| 68 | + { |
| 69 | + public IniSection(IniSectionName name) |
| 70 | + { |
| 71 | + Name = name; |
| 72 | + Properties = new List<IniProperty>(); |
| 73 | + } |
| 74 | + |
| 75 | + public IniSectionName Name { get; } |
| 76 | + |
| 77 | + public IList<IniProperty> Properties { get; } |
| 78 | + |
| 79 | + public bool TryGetProperty(string name, out string value) |
| 80 | + { |
| 81 | + if (TryGetMultiProperty(name, out IEnumerable<string> values)) |
| 82 | + { |
| 83 | + value = values.Last(); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + value = null; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + public bool TryGetMultiProperty(string name, out IEnumerable<string> values) |
| 92 | + { |
| 93 | + IniProperty[] props = Properties |
| 94 | + .Where(x => StringComparer.OrdinalIgnoreCase.Equals(x.Name, name)) |
| 95 | + .ToArray(); |
| 96 | + |
| 97 | + if (props.Length == 0) |
| 98 | + { |
| 99 | + values = Array.Empty<string>(); |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + values = props.Select(x => x.Value); |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private string DebuggerDisplay => Name.SubName is null |
| 108 | + ? $"{Name.Name} [Properties: {Properties.Count}]" |
| 109 | + : $"{Name.Name} \"{Name.SubName}\" [Properties: {Properties.Count}]"; |
| 110 | + } |
| 111 | + |
| 112 | + [DebuggerDisplay("{DebuggerDisplay}")] |
| 113 | + public class IniProperty |
| 114 | + { |
| 115 | + public IniProperty(string name, string value) |
| 116 | + { |
| 117 | + Name = name; |
| 118 | + Value = value; |
| 119 | + } |
| 120 | + |
| 121 | + public string Name { get; } |
| 122 | + public string Value { get; } |
| 123 | + |
| 124 | + private string DebuggerDisplay => $"{Name}={Value}"; |
| 125 | + } |
| 126 | + |
| 127 | + public static class IniSerializer |
| 128 | + { |
| 129 | + private static readonly Regex SectionRegex = |
| 130 | + new Regex(@"^\[[^\S#]*(?'name'[^\s#\]]*?)(?:\s+""(?'sub'.+)"")?\s*\]", RegexOptions.Compiled); |
| 131 | + |
| 132 | + private static readonly Regex PropertyRegex = |
| 133 | + new Regex(@"^[^\S#]*?(?'name'[^\s#]+)\s*=(?'value'.*)?$", RegexOptions.Compiled); |
| 134 | + |
| 135 | + public static IniFile Deserialize(IFileSystem fs, string path) |
| 136 | + { |
| 137 | + IEnumerable<string> lines = fs.ReadAllLines(path).Select(x => x.Trim()); |
| 138 | + |
| 139 | + var iniFile = new IniFile(); |
| 140 | + IniSection section = null; |
| 141 | + |
| 142 | + foreach (string line in lines) |
| 143 | + { |
| 144 | + Match match = SectionRegex.Match(line); |
| 145 | + if (match.Success) |
| 146 | + { |
| 147 | + string mainName = match.Groups["name"].Value; |
| 148 | + string subName = match.Groups["sub"].Value; |
| 149 | + |
| 150 | + // Skip empty-named sections |
| 151 | + if (string.IsNullOrWhiteSpace(mainName)) |
| 152 | + { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + if (!iniFile.TryGetSection(mainName, subName, out section)) |
| 157 | + { |
| 158 | + var sectionName = new IniSectionName(mainName, subName); |
| 159 | + section = new IniSection(sectionName); |
| 160 | + iniFile.Sections[sectionName] = section; |
| 161 | + } |
| 162 | + |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + match = PropertyRegex.Match(line); |
| 167 | + if (match.Success) |
| 168 | + { |
| 169 | + if (section is null) |
| 170 | + { |
| 171 | + throw new Exception("Missing section header"); |
| 172 | + } |
| 173 | + |
| 174 | + string propName = match.Groups["name"].Value; |
| 175 | + string propValue = match.Groups["value"].Value.Trim(); |
| 176 | + |
| 177 | + // Trim trailing comments |
| 178 | + int firstDQuote = propValue.IndexOf('"'); |
| 179 | + int lastDQuote = propValue.LastIndexOf('"'); |
| 180 | + int commentIdx = propValue.LastIndexOf('#'); |
| 181 | + if (commentIdx > -1) |
| 182 | + { |
| 183 | + bool insideDQuotes = firstDQuote > -1 && lastDQuote > -1 && |
| 184 | + (firstDQuote < commentIdx && commentIdx < lastDQuote); |
| 185 | + |
| 186 | + if (!insideDQuotes) |
| 187 | + { |
| 188 | + propValue = propValue.Substring(0, commentIdx).Trim(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Trim book-ending double quotes: "foo" => foo |
| 193 | + if (propValue.Length > 1 && propValue[0] == '"' && |
| 194 | + propValue[propValue.Length - 1] == '"') |
| 195 | + { |
| 196 | + propValue = propValue.Substring(1, propValue.Length - 2); |
| 197 | + } |
| 198 | + |
| 199 | + var property = new IniProperty(propName, propValue); |
| 200 | + section.Properties.Add(property); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return iniFile; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments