Skip to content

Commit

Permalink
v1.0.4.0 New release.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioZ committed Feb 28, 2015
1 parent 65c7828 commit 9480617
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 29 deletions.
Binary file modified MadMilkman.Ini.Documentation.chm
Binary file not shown.
178 changes: 177 additions & 1 deletion MadMilkman.Ini.Samples/MadMilkman.Ini.Samples.CPP/IniSamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using namespace System::Text;
using namespace System::Collections::Generic;
using namespace MadMilkman::Ini;


void HelloWorld()
{
// Create new file.
Expand Down Expand Up @@ -133,6 +132,52 @@ void Save()
Console::WriteLine(iniContent);
}

void Encrypt()
{
// Enable file's protection by providing an encryption password.
IniOptions^ options = gcnew IniOptions();
options->EncryptionPassword = "M4dM1lkM4n.1n1";
IniFile^ file = gcnew IniFile(options);

IniSection^ section = file->Sections->Add("User's Account");
section->Keys->Add("Username", "John Doe");
section->Keys->Add("Password", "P@55\\/\\/0|2D");

// Save and encrypt the file.
file->Save("..\\MadMilkman.Ini.Samples.Files\\Encrypt Example.ini");

file->Sections->Clear();

// Load and dencrypt the file.
file->Load("..\\MadMilkman.Ini.Samples.Files\\Encrypt Example.ini");

Console::WriteLine("User Name: {0}", file->Sections[0]->Keys["Username"]->Value);
Console::WriteLine("Password: {0}", file->Sections[0]->Keys["Password"]->Value);
}

void Compress()
{
// Enable file's size reduction.
IniOptions^ options = gcnew IniOptions();
options->Compression = true;
IniFile^ file = gcnew IniFile(options);

for (int i = 1; i <= 100; i++)
{
file->Sections->Add("Section " + i)->Keys->Add("Key " + i, "Value " + i);
}

// Save and compress the file.
file->Save("..\\MadMilkman.Ini.Samples.Files\\Compress Example.ini");

file->Sections->Clear();

// Load and decompress the file.
file->Load("..\\MadMilkman.Ini.Samples.Files\\Compress Example.ini");

Console::WriteLine(file->Sections->Count);
}

void Custom()
{
IniOptions^ options = gcnew IniOptions();
Expand Down Expand Up @@ -263,6 +308,128 @@ void BindExternal()
String^ userProfilePage = file->Sections["User's Settings"]->Keys["Profile Page"]->Value;
}

private ref class BindingCustomizationSample {
public:
void BindCustomize()
{
IniFile^ file = gcnew IniFile();
String^ content = "[Player]" + Environment::NewLine +
"Name = @{Name}" + Environment::NewLine +
"Surname = @{Surname}" + Environment::NewLine +
"Adult = @{Age}" + Environment::NewLine +
"Medal = @{Rank}";
file->Load(gcnew StringReader(content));

// Customize binding operation.
file->ValueBinding->Binding += gcnew EventHandler<IniValueBindingEventArgs^>(this, &BindingCustomizationSample::CustomEventHandler);

// Execute binding operation.
Dictionary<String^, String^>^ dataSource = gcnew Dictionary<String^, String^>();
dataSource->Add("Name", "John");
dataSource->Add("Age", "20");
dataSource->Add("Rank", "1");
file->ValueBinding->Bind(dataSource);
}

void CustomEventHandler(Object^ sender, IniValueBindingEventArgs^ e)
{
if (!e->IsValueFound)
{
e->Value = "UNKNOWN";
return;
}
if (e->PlaceholderKey->Name->Equals("Adult") && e->PlaceholderName->Equals("Age"))
{
int age;
if (int::TryParse(e->Value, age))
{
e->Value = (age >= 18) ? "YES" : "NO";
}
else
{
e->Value = "UNKNOWN";
}
return;
}
if (e->PlaceholderKey->Name->Equals("Medal") && e->PlaceholderName->Equals("Rank"))
{
int rank;
if (int::TryParse(e->Value, rank))
{
switch (rank)
{
case 1:
e->Value = "GOLD";
break;
case 2:
e->Value = "SILVER";
break;
case 3:
e->Value = "BRONCE";
break;
default:
e->Value = "NONE";
break;
}
}
else
{
e->Value = "UNKNOWN";
}
return;
}
}
};

// Custom type used for serialization sample.
private ref class GameCharacter
{
public:
property String^ Name;

// Serialize this property as a key with "Sword" name.
[IniSerialization("Sword")]
property double Attack;

// Serialize this property as a key with "Shield" name.
[IniSerialization("Shield")]
property double Defence;

// Ignore serializing this property.
[IniSerialization(true)]
property double Health;

GameCharacter()
{
this->Health = 100;
}
};

void Serialize()
{
IniFile^ file = gcnew IniFile();
IniSection^ section = file->Sections->Add("User's Character");

GameCharacter^ character = gcnew GameCharacter();
character->Name = "John";
character->Attack = 5.5;
character->Defence = 1;
character->Health = 75;

// Serialize GameCharacter object into section's keys.
section->Serialize(character);

// Deserialize section into GameCharacter object.
GameCharacter^ savedCharacter = section->Deserialize<GameCharacter^>();

Console::WriteLine(section->Keys["Name"]->Value);
Console::WriteLine(savedCharacter->Name);
Console::WriteLine(section->Keys["Sword"]->Value);
Console::WriteLine(savedCharacter->Attack);
Console::WriteLine(section->Keys["Shield"]->Value);
Console::WriteLine(savedCharacter->Defence);
}

void main()
{
HelloWorld();
Expand All @@ -275,6 +442,10 @@ void main()

Save();

Encrypt();

Compress();

Custom();

Copy();
Expand All @@ -284,4 +455,9 @@ void main()
BindInternal();

BindExternal();

BindingCustomizationSample^ sample = gcnew BindingCustomizationSample();
sample->BindCustomize();

Serialize();
}
62 changes: 54 additions & 8 deletions MadMilkman.Ini.Samples/MadMilkman.Ini.Samples.CS/IniSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ private static void Save()
private static void Encrypt()
{
// Enable file's protection by providing an encryption password.
var options = new IniOptions() { EncryptionPassword = "M4dM1lkM4n.1n1" };
var file = new IniFile(options);
IniOptions options = new IniOptions() { EncryptionPassword = "M4dM1lkM4n.1n1" };
IniFile file = new IniFile(options);

var section = file.Sections.Add("User's Account");
IniSection section = file.Sections.Add("User's Account");
section.Keys.Add("Username", "John Doe");
section.Keys.Add("Password", @"P@55\/\/0|2D");

Expand All @@ -158,8 +158,8 @@ private static void Encrypt()
private static void Compress()
{
// Enable file's size reduction.
var options = new IniOptions() { Compression = true };
var file = new IniFile(options);
IniOptions options = new IniOptions() { Compression = true };
IniFile file = new IniFile(options);

for (int i = 1; i <= 100; i++)
file.Sections.Add("Section " + i).Keys.Add("Key " + i, "Value " + i);
Expand Down Expand Up @@ -310,15 +310,14 @@ private static void BindExternal()
string userProfilePage = file.Sections["User's Settings"].Keys["Profile Page"].Value;
}

private static void BindCustomization()
private static void BindCustomize()
{
IniFile file = new IniFile();
string content = "[Player]" + Environment.NewLine +
"Name = @{Name}" + Environment.NewLine +
"Surname = @{Surname}" + Environment.NewLine +
"Adult = @{Age}" + Environment.NewLine +
"Medal = @{Rank}";

file.Load(new StringReader(content));

// Customize binding operation.
Expand Down Expand Up @@ -378,6 +377,51 @@ private static void BindCustomization()
});
}

// Custom type used for serialization sample.
private class GameCharacter
{
public string Name { get; set; }

// Serialize this property as a key with "Sword" name.
[IniSerialization("Sword")]
public double Attack { get; set; }

// Serialize this property as a key with "Shield" name.
[IniSerialization("Shield")]
public double Defence { get; set; }

// Ignore serializing this property.
[IniSerialization(true)]
public double Health { get; set; }

public GameCharacter() { this.Health = 100; }
}

private static void Serialize()
{
IniFile file = new IniFile();
IniSection section = file.Sections.Add("User's Character");

GameCharacter character = new GameCharacter();
character.Name = "John";
character.Attack = 5.5;
character.Defence = 1;
character.Health = 75;

// Serialize GameCharacter object into section's keys.
section.Serialize(character);

// Deserialize section into GameCharacter object.
GameCharacter savedCharacter = section.Deserialize<GameCharacter>();

Console.WriteLine(section.Keys["Name"].Value);
Console.WriteLine(savedCharacter.Name);
Console.WriteLine(section.Keys["Sword"].Value);
Console.WriteLine(savedCharacter.Attack);
Console.WriteLine(section.Keys["Shield"].Value);
Console.WriteLine(savedCharacter.Defence);
}

static void Main()
{
HelloWorld();
Expand All @@ -404,7 +448,9 @@ static void Main()

BindExternal();

BindCustomization();
BindCustomize();

Serialize();
}
}
}
Loading

0 comments on commit 9480617

Please sign in to comment.