This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections.Generic; | ||
using Kudu.Core.Infrastructure; | ||
using Xunit; | ||
using IniLookup = System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>; | ||
|
||
namespace Kudu.Core.Test | ||
{ | ||
public class IniFileFacts | ||
{ | ||
public class ParseValues | ||
{ | ||
[Fact] | ||
public void EmptySectionIgnored() | ||
{ | ||
IniLookup lookup; | ||
|
||
IniFile.ParseValues(new[] { "[]", "a=b" }, out lookup); | ||
|
||
Assert.Equal(0, lookup.Count); | ||
} | ||
|
||
[Fact] | ||
public void SectionWithEmptyKeySkipped() | ||
{ | ||
IniLookup lookup; | ||
|
||
IniFile.ParseValues(new[] { "[section]", "=b" }, out lookup); | ||
|
||
Assert.Equal(1, lookup.Count); | ||
Dictionary<string, string> values; | ||
Assert.True(lookup.TryGetValue("section", out values)); | ||
Assert.Equal(0, values.Count); | ||
} | ||
|
||
[Fact] | ||
public void ParsesSectionsIntoKeyValuePairs() | ||
{ | ||
IniLookup lookup; | ||
|
||
IniFile.ParseValues(new[] { "[section]", "", " ", "\t", "key=\tvalue", "[s2]", "x= y" }, out lookup); | ||
|
||
Assert.Equal(2, lookup.Count); | ||
Dictionary<string, string> s1; | ||
Assert.True(lookup.TryGetValue("section", out s1)); | ||
Assert.Equal(1, s1.Count); | ||
Assert.Equal("value", s1["key"]); | ||
|
||
Dictionary<string, string> s2; | ||
Assert.True(lookup.TryGetValue("s2", out s2)); | ||
Assert.Equal(1, s2.Count); | ||
Assert.Equal("y", s2["x"]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using IniLookup = System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>; | ||
|
||
namespace Kudu.Core.Infrastructure | ||
{ | ||
public class IniFile | ||
{ | ||
private readonly string _path; | ||
private IniLookup _sectionLookup; | ||
|
||
public IniFile(string path) | ||
{ | ||
_path = path; | ||
} | ||
|
||
public string GetSectionValue(string section, string key) | ||
{ | ||
ParseIniFile(); | ||
|
||
Dictionary<string, string> valueLookup; | ||
string value; | ||
if (_sectionLookup.TryGetValue(section, out valueLookup) && | ||
valueLookup.TryGetValue(key, out value)) | ||
{ | ||
return value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void ParseIniFile() | ||
{ | ||
if (_sectionLookup != null) | ||
{ | ||
return; | ||
} | ||
|
||
var files = File.ReadAllLines(_path); | ||
ParseValues(files, out _sectionLookup); | ||
} | ||
|
||
internal static void ParseValues(IList<string> lines, out IniLookup iniFile) | ||
{ | ||
iniFile = new IniLookup(StringComparer.OrdinalIgnoreCase); | ||
|
||
Dictionary<string, string> currentSection = null; | ||
foreach (var line in lines) | ||
{ | ||
if (String.IsNullOrWhiteSpace(line)) | ||
{ | ||
continue; | ||
} | ||
|
||
var value = line.Trim(); | ||
if (value.Length > 2 && | ||
value[0] == '[' && | ||
value[value.Length - 1] == ']') | ||
{ | ||
// Get the section name | ||
string sectionValue = value.Substring(1, value.Length - 2).Trim(); | ||
|
||
if (!String.IsNullOrEmpty(sectionValue)) | ||
{ | ||
// Create a new sectio | ||
currentSection = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
|
||
// Create a new section | ||
iniFile[sectionValue] = currentSection; | ||
} | ||
} | ||
else if (currentSection == null) | ||
{ | ||
// If there's no section then ignore the values | ||
continue; | ||
} | ||
else | ||
{ | ||
var kvp = value.Split('='); | ||
if (kvp.Length == 2) | ||
{ | ||
var key = kvp[0]; | ||
var keyValue = kvp[1]; | ||
|
||
if (String.IsNullOrEmpty(key)) | ||
{ | ||
// Skip empty keys | ||
continue; | ||
} | ||
|
||
// Add it to the list of keys | ||
currentSection[key.Trim()] = keyValue.Trim(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters