Skip to content

Szpi/IniWrapper

Repository files navigation

IniWrapper

Latest version codecov License: MIT

Build Status

  Azure Pipelines Travis
master Azure Pipelines Travis

IniWrapper uses provided model to save / read values from ini file and bind them to this model. The purpose of this library is NOT parsing ini file, but to wrap it to provide easier use of existing parsing libraries. In configuration there is possibility to pass IniParser interface, which is used as file access layer. This library provides class that wraps Windows C++ methods to retrieve values from ini file.

For more information please go to wiki page.

If you find any bug or come up with an idea how to make this library better, feel free to raise an issue.

Generate configuration class with ease

If you already have ini file you can use IniWrapper.ConfigurationGenerator console application to automatically generate classes for you.

Integration with ini-parser

If you want IniWrapper to support multiplatform please use IniWrapper.ini-parser extension to IniWrapper, which uses ini-parser to parse ini file.

Quick start

Loading configuration

You can use custom IniParser class by passing it to Create Method in IniWrapperFactory class. Then call LoadConfiguration method with class that IniWrapper should discover and bind values.

var iniWrapperFactory = new IniWrapperFactory();
var iniWrapper = iniWrapperFactory.Create(new CustomIniParser());

var loadedIniConfiguration = iniWrapper.LoadConfiguration<TestConfiguration>();

If you want to use default IniParser you can call CreateWithDefaultIniParser method. By doing this library will create IniParser that wraps Windows C++ methods from kernel. For more information see Microsoft documentation for WritePrivateProfileString, GetPrivateProfileString and GetPrivateProfileSection and IniParser.cs. For CreateWithDefaultIniParser property IniFilePath in IniSettings has to be set.

var iniWrapperFactory = new IniWrapperFactory();
var inisettings = new IniSettings()
{	
	IniFilePath = "test.ini"
};
var iniWrapper = iniWrapperFactory.CreateWithDefaultIniParser(inisettings);

var loadedIniConfiguration = iniWrapper.LoadConfiguration<TestConfiguration>();

Configure library's Settings to change it's default behaviour.

var iniWrapper = new IniWrapperFactory().Create(iniSettings =>
{
  iniSettings.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
  iniSettings.EnumerableEntitySeparator = '*';
  iniSettings.IniFilePath = "test.ini";
  iniSettings.NullValueHandling = NullValueHandling.ReplaceWithEmptyString;
  iniSettings.DefaultIniWrapperBufferSize = 1024;
}, iniParser);

Note: In version 1.1.0 and 1.0.0 you have to call IniWrapperFactory with CreateWithDefaultIniWrapper.

var iniWrapper = iniWrapperFactory.CreateWithDefaultIniWrapper("test.ini");

Saving configuration

To save configuration just call SaveConfiguration method and pass configuration class.

var iniWrapperFactory = new IniWrapperFactory();
var inisettings = new IniSettings()
{	
	IniFilePath = "test.ini"
};
var iniWrapper = iniWrapperFactory.CreateWithDefaultIniParser(inisettings);

iniWrapper.SaveConfiguration(new TestConfiguration());

Note: In version 1.1.0 and 1.0.0 you have to call IniWrapperFactory with CreateWithDefaultIniWrapper.

var iniWrapper = iniWrapperFactory.CreateWithDefaultIniWrapper("test.ini");

How does library work?

For given configuration class:

public struct TestConfiguration
{
    public string TestString { get; set; }
    public List<int> TestIntList { get; set; }
}

IniWrapper will call IIniParser with following ini parameters Section:TestConfiguration, Key: TestString, Value : value in TestString property.

Overall rules:

  • Section is evaluated from class / struct name
  • Key is evaluated from property / field name
  • Value is taken from property / field value

Exception is for IDictionary type:

  • Section is evaluated from property / field name
  • Key is taken from Key (from IDictionary)
  • Value is taken from Value (from IDictionary)

To override library's default name resolving you can use IniOptionsAttribute.