Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Added basic ini file parser
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Jan 29, 2012
1 parent 81c8642 commit d9530b5
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Kudu.Core.Test/IniFileFacts.cs
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"]);
}
}
}
}
1 change: 1 addition & 0 deletions Kudu.Core.Test/Kudu.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="CommandExecutorTest.cs" />
<Compile Include="FileSystemHelpersTest.cs" />
<Compile Include="GitRepositoryRepositoryTest.cs" />
<Compile Include="IniFileFacts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
101 changes: 101 additions & 0 deletions Kudu.Core/Infrastructure/IniFile.cs
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();
}
}

}
}
}
}
1 change: 1 addition & 0 deletions Kudu.Core/Kudu.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Deployment\MsBuildSiteBuilder.cs" />
<Compile Include="Deployment\ProgressReporter.cs" />
<Compile Include="Infrastructure\DisposableAction.cs" />
<Compile Include="Infrastructure\IniFile.cs" />
<Compile Include="Infrastructure\TaskExtensions.cs" />
<Compile Include="Infrastructure\VsHelper.cs" />
<Compile Include="Performance\IProfilerFactory.cs" />
Expand Down

0 comments on commit d9530b5

Please sign in to comment.