Skip to content

Commit

Permalink
Added Overload of Functions to allow for not found defaults (#13)
Browse files Browse the repository at this point in the history
Added Overload of Functions to allow for not found defaults. If you can find a Section Key pair with a value then return not found defaults passed into function.
  • Loading branch information
davidbti authored Apr 30, 2020
1 parent c1d884e commit db90f01
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 46 deletions.
65 changes: 37 additions & 28 deletions Ini.Net.sln
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Windows Desktop
VisualStudioVersion = 12.0.30723.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ini.Net", "src\Ini.Net\Ini.Net.csproj", "{109977DF-DDCB-4CB8-B658-51DF6DA2842C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ini.Net.Tests", "test\Ini.Net.Tests\Ini.Net.Tests.csproj", "{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Release|Any CPU.Build.0 = Release|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ini.Net", "src\Ini.Net\Ini.Net.csproj", "{109977DF-DDCB-4CB8-B658-51DF6DA2842C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ini.Net.Tests", "test\Ini.Net.Tests\Ini.Net.Tests.csproj", "{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{2D7766BC-9287-4DB6-BE11-8E19C4F72416}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{109977DF-DDCB-4CB8-B658-51DF6DA2842C}.Release|Any CPU.Build.0 = Release|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B99D6813-2B82-4EB3-B043-BFCDDCBB745B}.Release|Any CPU.Build.0 = Release|Any CPU
{2D7766BC-9287-4DB6-BE11-8E19C4F72416}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D7766BC-9287-4DB6-BE11-8E19C4F72416}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D7766BC-9287-4DB6-BE11-8E19C4F72416}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D7766BC-9287-4DB6-BE11-8E19C4F72416}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EAE21322-BA5A-4FF3-B63B-30FA5D546792}
EndGlobalSection
EndGlobal
113 changes: 99 additions & 14 deletions src/Ini.Net/IniFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text.RegularExpressions;


namespace Ini.Net
{
Expand Down Expand Up @@ -37,9 +39,9 @@ public class IniFile
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);

private const int SIZE = 255;
private const string DATETIME_MASK = "yyyy/MM/dd HH:mm:ss";
private const string DATE_MASK = "yyyy/MM/dd";
private const string DATETIME_MASK = "MM/dd/yyyy hh:mm:ss tt";
private const string DATE_MASK = "MM/dd/yyyy";

public string FileName { get; private set; }

public IniFile(string fileName)
Expand All @@ -56,7 +58,11 @@ private void SetFileName(string fileName)
string basePath = System.IO.Directory.GetCurrentDirectory();
this.FileName = Path.Combine(basePath, this.FileName);
}
}
}
private bool IsNumeric(string input)
{
return Regex.IsMatch(input, @"^\d+$");
}

public void DeleteKey(string section, string key)
{
Expand All @@ -73,18 +79,50 @@ public string ReadString(string section, string key)
var temp = new StringBuilder(SIZE);
GetPrivateProfileString(section, key, null, temp, SIZE, this.FileName);
return temp.ToString();
}

public string ReadString(string section, string key, string notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadString(section, key);
}

public bool ReadBoolean(string section, string key)
{
string value = ReadString(section, key);
return value.ToUpper().Equals("TRUE");
string value = ReadString(section, key);
bool rtn = (IsNumeric(value)) ? Convert.ToBoolean(Convert.ToInt32(value)) : value.ToUpper().Equals("TRUE");
return rtn;
}

public bool ReadBoolean(string section, string key, bool notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadBoolean(section, key);
}

public decimal ReadDecimal(string section, string key)
{
if (!KeyExists(section, key))
{
return new decimal(-1);
}
string value = ReadString(section, key);
return decimal.Parse(value.Trim(), System.Globalization.CultureInfo.InvariantCulture);
}

public decimal ReadDecimal(string section, string key, decimal notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadDecimal(section, key);
}

public double ReadDouble(string section, string key)
Expand All @@ -93,38 +131,84 @@ public double ReadDouble(string section, string key)
return double.Parse(value.Trim(), System.Globalization.CultureInfo.InvariantCulture);
}

public double ReadDouble(string section, string key, double notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadDouble(section, key);
}

public float ReadFloat(string section, string key)
{
string value = ReadString(section, key);
return float.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
}

public float ReadFloat(string section, string key, float notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadFloat(section, key);
}

public int ReadInteger(string section, string key)
{
string value = ReadString(section, key);
return Convert.ToInt32(value.Trim());
}

public int ReadInteger(string section, string key, int notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadInteger(section, key);
}

public DateTime ReadDateTime(string section, string key)
{
string value = ReadString(section, key);
return DateTime.ParseExact(value, DATETIME_MASK,
System.Globalization.CultureInfo.InvariantCulture);
return Convert.ToDateTime(value);
//return DateTime.ParseExact(value, DATETIME_MASK,
// System.Globalization.CultureInfo.InvariantCulture);
}

public DateTime ReadDateTime(string section, string key, DateTime notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadDateTime(section, key);
}

public DateTime ReadDate(string section, string key)
{
string value = ReadString(section, key);
return DateTime.ParseExact(value, DATE_MASK,
System.Globalization.CultureInfo.InvariantCulture);
string value = ReadString(section, key);
DateTime rtn = Convert.ToDateTime(value);
return Convert.ToDateTime(rtn.ToString(DATE_MASK));
}

public DateTime ReadDate(string section, string key, DateTime notfound)
{
if (!KeyExists(section, key))
{
return notfound;
}
return ReadDate(section, key);
}

public bool WriteString(string section, string key, string value)
{
long l = WritePrivateProfileString(section, key, value, this.FileName);
return l > 0;
}
}

public bool WriteBoolean(string section, string key, bool value)
{
string str = value.ToString().ToUpper();
Expand Down Expand Up @@ -187,5 +271,6 @@ public IDictionary<string, string> ReadSection(string section)
}
return result;
}
}
}

}
Binary file added src/Ini.Net/bin/Debug/Ini.Net.dll
Binary file not shown.
Binary file added src/Ini.Net/bin/Debug/Ini.Net.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eab9205dc0e7654e3a6f813edcfafc4967e4b51c
6 changes: 6 additions & 0 deletions src/Ini.Net/obj/Debug/Ini.Net.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
D:\projects\Ini.Net\src\Ini.Net\bin\Debug\Ini.Net.dll
D:\projects\Ini.Net\src\Ini.Net\bin\Debug\Ini.Net.pdb
D:\projects\Ini.Net\src\Ini.Net\obj\Debug\Ini.Net.csprojAssemblyReference.cache
D:\projects\Ini.Net\src\Ini.Net\obj\Debug\Ini.Net.csproj.CoreCompileInputs.cache
D:\projects\Ini.Net\src\Ini.Net\obj\Debug\Ini.Net.dll
D:\projects\Ini.Net\src\Ini.Net\obj\Debug\Ini.Net.pdb
Binary file not shown.
Binary file added src/Ini.Net/obj/Debug/Ini.Net.dll
Binary file not shown.
Binary file added src/Ini.Net/obj/Debug/Ini.Net.pdb
Binary file not shown.
8 changes: 4 additions & 4 deletions test/Ini.Net.Tests/IniFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public void TestReadString()
this.iniFile.WriteString(section, key, value);
string gotValue = this.iniFile.ReadString(section, key);
Assert.AreEqual(value, gotValue);
}
}

[TestMethod]
public void TestReadBoolean()
{
Expand Down Expand Up @@ -256,7 +256,7 @@ public void TestWriteDateTime()

StringBuilder sb = new StringBuilder();
sb.AppendLine("[breno]");
sb.AppendLine("somedatetime=1984/09/11 17:40:01");
sb.AppendLine("somedatetime=09/11/1984 05:40:01 PM");
string expectedText = sb.ToString();

Assert.AreEqual(expectedText, gotText);
Expand All @@ -271,7 +271,7 @@ public void TestWriteDate()

StringBuilder sb = new StringBuilder();
sb.AppendLine("[breno]");
sb.AppendLine("birthdate=1984/09/11");
sb.AppendLine("birthdate=09/11/1984");
string expectedText = sb.ToString();

Assert.AreEqual(expectedText, gotText);
Expand Down
Binary file added test/Ini.Net.Tests/bin/Debug/Ini.Net.Tests.dll
Binary file not shown.
Binary file added test/Ini.Net.Tests/bin/Debug/Ini.Net.Tests.pdb
Binary file not shown.
Binary file added test/Ini.Net.Tests/bin/Debug/Ini.Net.dll
Binary file not shown.
Binary file added test/Ini.Net.Tests/bin/Debug/Ini.Net.pdb
Binary file not shown.
Binary file not shown.

0 comments on commit db90f01

Please sign in to comment.