Skip to content

Commit

Permalink
v1.0.2.3 Added support for defining placeholders in IniKey.Value and …
Browse files Browse the repository at this point in the history
…binding them with internal or external data source.
  • Loading branch information
MarioZ committed Feb 8, 2015
1 parent 07de997 commit 8defbe2
Show file tree
Hide file tree
Showing 23 changed files with 549 additions and 81 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.h diff=cpp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.vcxproj merge=union
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Rr]elease/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult/

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
61 changes: 59 additions & 2 deletions MadMilkman.Ini.Samples/MadMilkman.Ini.Samples.CPP/IniSamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ void Parse()
try { file->Load(contentStream); }
finally { delete contentStream; }

// Map "yes" value as "true" boolean.
// Map 'yes' value as 'true' boolean.
file->ValueMappings->Add("yes", true);
// Map "no" value as "false" boolean.
// Map 'no' value as 'false' boolean.
file->ValueMappings->Add("no", false);

IniSection^ playerSection = file->Sections["Player"];
Expand All @@ -223,6 +223,59 @@ void Parse()
playerSection->Keys["Game Time"]->TryParseValue(playerGameTime);
}

void BindInternal()
{
IniFile^ file = gcnew IniFile();
String^ content = "[Machine Settings]" + Environment::NewLine +
"Program Files = C:\\Program Files" + Environment::NewLine +
"[Application Settings]" + Environment::NewLine +
"Name = Example App" + Environment::NewLine +
"Version = 1.0" + Environment::NewLine +
"Full Name = @{Name} v@{Version}" + Environment::NewLine +
"Executable Path = @{Machine Settings|Program Files}\\@{Name}.exe";
Stream^ stream = gcnew MemoryStream(Encoding::ASCII->GetBytes(content));
try { file->Load(stream); }
finally { delete stream; }

// Bind placeholders with file's content, internal information.
file->ValueBinding->Bind();

// Retrieve application's full name, value is "Example App v1.0".
String^ appFullName = file->Sections["Application Settings"]->Keys["Full Name"]->Value;

// Retrieve application's executable path, value is "C:\\Program Files\\Example App.exe".
String^ appExePath = file->Sections["Application Settings"]->Keys["Executable Path"]->Value;
}

void BindExternal()
{
IniFile^ file = gcnew IniFile();
String^ content = "[User's Settings]" + Environment::NewLine +
"Nickname = @{User Alias}" + Environment::NewLine +
"Full Name = @{User Name} @{User Surname}" + Environment::NewLine +
"Profile Page = @{Homepage}/Profiles/@{User Alias}";
Stream^ stream = gcnew MemoryStream(Encoding::ASCII->GetBytes(content));
try { file->Load(stream); }
finally { delete stream; }

// Bind placeholders with user's data, external information.
Dictionary<String^, String^>^ userData = gcnew Dictionary<String^, String^>();
userData->Add("User Alias", "Johny");
userData->Add("User Name", "John");
userData->Add("User Surname", "Doe");
file->ValueBinding->Bind(userData);

// Bind 'Homepage' placeholder with 'www.example.com' value.
file->ValueBinding->Bind(
KeyValuePair<String^, String^>("Homepage", "www.example.com"));

// Retrieve user's full name, value is "John Doe".
String^ userFullName = file->Sections["User's Settings"]->Keys["Full Name"]->Value;

// Retrieve user's profile page, value is "www.example.com/Profiles/Johny".
String^ userProfilePage = file->Sections["User's Settings"]->Keys["Profile Page"]->Value;
}

void main()
{
HelloWorld();
Expand All @@ -240,4 +293,8 @@ void main()
Copy();

Parse();

BindInternal();

BindExternal();
}

This file was deleted.

71 changes: 64 additions & 7 deletions MadMilkman.Ini.Samples/MadMilkman.Ini.Samples.CS/IniSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,17 @@ private static void Parse()
{
IniFile file = new IniFile();
string content = "[Player]" + Environment.NewLine +
"Full Name = John Doe" + Environment.NewLine +
"Birthday = 12/31/1999" + Environment.NewLine +
"Married = Yes" + Environment.NewLine +
"Score = 9999999" + Environment.NewLine +
"Game Time = 00:59:59";
"Full Name = John Doe" + Environment.NewLine +
"Birthday = 12/31/1999" + Environment.NewLine +
"Married = Yes" + Environment.NewLine +
"Score = 9999999" + Environment.NewLine +
"Game Time = 00:59:59";
using (Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(content)))
file.Load(stream);

// Map "yes" value as "true" boolean.
// Map 'yes' value as 'true' boolean.
file.ValueMappings.Add("yes", true);
// Map "no" value as "false" boolean.
// Map 'no' value as 'false' boolean.
file.ValueMappings.Add("no", false);

IniSection playerSection = file.Sections["Player"];
Expand All @@ -222,6 +222,59 @@ private static void Parse()
playerSection.Keys["Game Time"].TryParseValue(out playerGameTime);
}

private static void BindInternal()
{
IniFile file = new IniFile();
string content = "[Machine Settings]" + Environment.NewLine +
"Program Files = C:\\Program Files" + Environment.NewLine +
"[Application Settings]" + Environment.NewLine +
"Name = Example App" + Environment.NewLine +
"Version = 1.0" + Environment.NewLine +
"Full Name = @{Name} v@{Version}" + Environment.NewLine +
"Executable Path = @{Machine Settings|Program Files}\\@{Name}.exe";
using (Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(content)))
file.Load(stream);

// Bind placeholders with file's content, internal information.
file.ValueBinding.Bind();

// Retrieve application's full name, value is "Example App v1.0".
string appFullName = file.Sections["Application Settings"].Keys["Full Name"].Value;

// Retrieve application's executable path, value is "C:\\Program Files\\Example App.exe".
string appExePath = file.Sections["Application Settings"].Keys["Executable Path"].Value;
}

private static void BindExternal()
{
IniFile file = new IniFile();
string content = "[User's Settings]" + Environment.NewLine +
"Nickname = @{User Alias}" + Environment.NewLine +
"Full Name = @{User Name} @{User Surname}" + Environment.NewLine +
"Profile Page = @{Homepage}/Profiles/@{User Alias}";
using (Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(content)))
file.Load(stream);

// Bind placeholders with user's data, external information.
file.ValueBinding.Bind(
new Dictionary<string, string>
{
{"User Alias", "Johny"},
{"User Name", "John"},
{"User Surname", "Doe"}
});

// Bind 'Homepage' placeholder with 'www.example.com' value.
file.ValueBinding.Bind(
new KeyValuePair<string, string>("Homepage", "www.example.com"));

// Retrieve user's full name, value is "John Doe".
string userFullName = file.Sections["User's Settings"].Keys["Full Name"].Value;

// Retrieve user's profile page, value is "www.example.com/Profiles/Johny".
string userProfilePage = file.Sections["User's Settings"].Keys["Profile Page"].Value;
}

static void Main()
{
HelloWorld();
Expand All @@ -239,6 +292,10 @@ static void Main()
Copy();

Parse();

BindInternal();

BindExternal();
}
}
}
61 changes: 59 additions & 2 deletions MadMilkman.Ini.Samples/MadMilkman.Ini.Samples.VB/IniSamples.vb
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ Module IniSamples
file.Load(stream)
End Using

' Map "yes" value as "true" boolean.
' Map 'yes' value as 'true' boolean.
file.ValueMappings.Add("yes", True)
' Map "no" value as "false" boolean.
' Map 'no' value as 'false' boolean.
file.ValueMappings.Add("no", False)

Dim playerSection As IniSection = file.Sections("Player")
Expand All @@ -213,6 +213,59 @@ Module IniSamples
playerSection.Keys("Game Time").TryParseValue(playerGameTime)
End Sub

Private Sub BindInternal()
Dim file As New IniFile()
Dim content As String = "[Machine Settings]" + Environment.NewLine +
"Program Files = C:\Program Files" + Environment.NewLine +
"[Application Settings]" + Environment.NewLine +
"Name = Example App" + Environment.NewLine +
"Version = 1.0" + Environment.NewLine +
"Full Name = @{Name} v@{Version}" + Environment.NewLine +
"Executable Path = @{Machine Settings|Program Files}\@{Name}.exe"
Using stream As Stream = New MemoryStream(Encoding.ASCII.GetBytes(content))
file.Load(stream)
End Using

' Bind placeholders with file's content, internal information.
file.ValueBinding.Bind()

' Retrieve application's full name, value is "Example App v1.0".
Dim appFullName As String = file.Sections("Application Settings").Keys("Full Name").Value

' Retrieve application's executable path, value is "C:\\Program Files\\Example App.exe".
Dim appExePath As String = file.Sections("Application Settings").Keys("Executable Path").Value
End Sub

Private Sub BindExternal()
Dim file As New IniFile()
Dim content As String = "[User's Settings]" + Environment.NewLine +
"Nickname = @{User Alias}" + Environment.NewLine +
"Full Name = @{User Name} @{User Surname}" + Environment.NewLine +
"Profile Page = @{Homepage}/Profiles/@{User Alias}"
Using stream As Stream = New MemoryStream(Encoding.ASCII.GetBytes(content))
file.Load(stream)
End Using

' Bind placeholders with user's data, external information.
file.ValueBinding.Bind(
New Dictionary(Of String, String)() From
{
{"User Alias", "Johny"},
{"User Name", "John"},
{"User Surname", "Doe"}
})

' Bind 'Homepage' placeholder with 'www.example.com' value.
file.ValueBinding.Bind(
New KeyValuePair(Of String, String)("Homepage", "www.example.com"))

' Retrieve user's full name, value is "John Doe".
Dim userFullName As String = file.Sections("User's Settings").Keys("Full Name").Value

' Retrieve user's profile page, value is "www.example.com/Profiles/Johny".
Dim userProfilePage As String = file.Sections("User's Settings").Keys("Profile Page").Value
End Sub

Sub Main()
HelloWorld()

Expand All @@ -229,6 +282,10 @@ Module IniSamples
Copy()

Parse()

BindInternal()

BindExternal()
End Sub

End Module
2 changes: 1 addition & 1 deletion MadMilkman.Ini.Tests/IniFileCreateUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void UpdateDisallowDuplicatesSectionsTest()
var options = new IniOptions()
{
SectionDuplicate = IniDuplication.Disallowed
};
};

var file = new IniFile(options);
file.Sections.Add("SECTION1").Keys.Add("KEY1");
Expand Down
Loading

0 comments on commit 8defbe2

Please sign in to comment.