Skip to content

Commit

Permalink
Added a untested ConfigFilenameManager, although not finished, this s…
Browse files Browse the repository at this point in the history
…hould make it "quite" easy to use:

var iniConfig = new IniConfig();
var proxy = ProxyBuilder.CreateProxy<Interface which extends IIniSection>();
iniConfig.AddSection(proxy);
var configManager = new ConfigFilenameManager("Greenshot","greenshot");
await configManager.LoadAsync(iniConfig);

// Now everything is loaded
var myConfig = proxy.PropertyObject;

// Change values

// write
await configManager.WriteAsync(iniConfig);
  • Loading branch information
Lakritzator committed May 5, 2015
1 parent 122a2c1 commit 2becfe3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 35 deletions.
10 changes: 5 additions & 5 deletions Dapplo.Config.Test/IniTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task TestIniInit()
iniConfig.AddSection(iniTest);
using (var testMemoryStream = new MemoryStream())
{
await iniConfig.ReadFromStream(testMemoryStream);
await iniConfig.ReadFromStreamAsync(testMemoryStream);
Assert.IsTrue(iniTest.WindowCornerCutShape.Count > 0);
}
}
Expand All @@ -68,9 +68,9 @@ public async Task TestIniWriteRead()
{
var iniConfig = new IniConfig();

var iniTest = _propertyProxy.PropertyObject;
iniConfig.AddSection(iniTest);
iniConfig.AddSection(_propertyProxy);

var iniTest = _propertyProxy.PropertyObject;
// Change some values
iniTest.Name = Name;
iniTest.FirstName = FirstName;
Expand All @@ -83,7 +83,7 @@ public async Task TestIniWriteRead()
iniTest.Age = ticks;
using (var writeStream = new MemoryStream())
{
await iniConfig.WriteToStream(writeStream);
await iniConfig.WriteToStreamAsync(writeStream);

// Set the not written value to a testable value, this should not be read (and overwritten) by reading the ini file.
iniTest.NotWritten = TestValueForNonSerialized;
Expand All @@ -93,7 +93,7 @@ public async Task TestIniWriteRead()

// Test reading
writeStream.Seek(0, SeekOrigin.Begin);
var isFileRead = await iniConfig.ReadFromStream(writeStream);
var isFileRead = await iniConfig.ReadFromStreamAsync(writeStream);
if (isFileRead)
{
Assert.AreEqual(Name, iniTest.Name);
Expand Down
117 changes: 94 additions & 23 deletions Dapplo.Config/Ini/ConfigFilenameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,125 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dapplo.Config.Ini
{
/// <summary>
/// Helps to specify how the ini file is read
/// </summary>
public enum ConfigFilePolicy
{
Portable,
Basic,
Fixed
}

/// <summary>
/// This manages the location of the Ini-File for one instance
/// </summary>
public class ConfigFilenameManager
{
private readonly string _filename;
private readonly ConfigFilePolicy _policy;
private readonly string _fileName;
private readonly string _applicationName;
private const string Defaults = "-defaults";
private const string Constants = "-constants";
private const string IniExtension = "ini";

/// <summary>
/// Setup the management of an .ini file location
/// </summary>
/// <param name="fileName"></param>
/// <param name="policy"></param>
public ConfigFilenameManager(string fileName, ConfigFilePolicy policy = ConfigFilePolicy.Basic)
public ConfigFilenameManager(string applicationName, string fileName)
{
_filename = fileName;
_policy = policy;
_applicationName = applicationName;
_fileName = fileName;
}

public string WritableIniLocation
{
get
/// <summary>
/// Return the location of the ini file
/// </summary>
/// <param name="fixedDirectory"></param>
/// <returns>string with full path to the ini file</returns>
public string IniFileLocation(string fixedDirectory = null) {
string iniFile;
if (fixedDirectory != null)
{
iniFile = Path.Combine(fixedDirectory, string.Format("{0}.{1}", _fileName, IniExtension));
}
else
{
return _filename;
iniFile = Path.Combine(AppDataDirectory, string.Format("{0}.{1}", _fileName, IniExtension));
}
return iniFile;
}

public string ConstantLocation
{
get
/// <summary>
/// Create the reading list for the files
/// </summary>
/// <param name="fixedDirectory">If you want to read the file(s) from a certain directory specify it here</param>
/// <returns>reading list</returns>
public string[] FileReadOrder(string fixedDirectory = null) {
string defaultsFile;
string constantsFile;
string iniFile = IniFileLocation(fixedDirectory);

if (fixedDirectory != null)
{
defaultsFile = Path.Combine(fixedDirectory, string.Format("{0}{1}.{2}", _fileName, Defaults, IniExtension));
constantsFile = Path.Combine(fixedDirectory, string.Format("{0}{1}.{2}", _fileName, Constants, IniExtension));
}
else
{
defaultsFile = Path.Combine(StartupDirectory, string.Format("{0}{1}.{2}", _fileName, Defaults, IniExtension));
if (!File.Exists(defaultsFile))
{
defaultsFile = Path.Combine(AppDataDirectory, string.Format("{0}{1}.{2}", _fileName, Defaults, IniExtension));
}
constantsFile = Path.Combine(StartupDirectory, string.Format("{0}{1}.{2}", _fileName, Constants, IniExtension));
if (!File.Exists(constantsFile))
{
constantsFile = Path.Combine(AppDataDirectory, string.Format("{0}{1}.{2}", _fileName, Constants, IniExtension));
}
}
return new string[]{defaultsFile, iniFile, constantsFile};
}

/// <summary>
/// Load the ini files "default", "normal" and fixed
/// </summary>
/// <param name="config">IniConfig to load</param>
/// <param name="fixedDirectory">If you want to read the file(s) from a certain directory specify it here</param>
public async Task LoadAsync(IniConfig config, string fixedDirectory = null) {
foreach(string filename in FileReadOrder(fixedDirectory))
{
return _filename;
if (filename == null || !File.Exists(filename))
{
continue;
}
await config.ReadFromFileAsync(filename);
}
}

/// <summary>
/// Write the ini file
/// </summary>
/// <param name="config">IniConfig to load</param>
/// <param name="fixedDirectory">If you want to read the file(s) from a certain directory specify it here</param>
public async Task WriteAsync(IniConfig config, string fixedDirectory = null) {
await config.WriteToFileAsync(IniFileLocation(fixedDirectory));
}

/// <summary>
/// Retrieve the startup directory
/// </summary>
private string StartupDirectory {
get {
return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
}
}

/// <summary>
/// Retrieve the ApplicationData directory
/// </summary>
private string AppDataDirectory {
get {
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), _applicationName);
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions Dapplo.Config/Ini/IniConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public void AddSection(IIniSection section)
}
}

/// <summary>
/// Add an ini section to this IniConfig by specifying the proxy
/// </summary>
/// <param name="section"></param>
public void AddSection<T>(IPropertyProxy<T> sectionProxy) where T: IIniSection {
AddSection(sectionProxy.PropertyObject);
}

/// <summary>
/// Reset all the values, in all the registered ini sections, to their default
/// </summary>
Expand All @@ -69,7 +77,7 @@ public void Reset()
/// </summary>
/// <param name="filename">File to write to</param>
/// <returns>Task</returns>
public async Task WriteToFile(string filename)
public async Task WriteToFileAsync(string filename)
{
if (string.IsNullOrEmpty(filename))
{
Expand All @@ -80,7 +88,7 @@ public async Task WriteToFile(string filename)
using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
// Write the registered ini sections to the stream
await WriteToStream(stream);
await WriteToStreamAsync(stream);
}

}
Expand All @@ -90,7 +98,7 @@ public async Task WriteToFile(string filename)
/// </summary>
/// <param name="stream">Stream to write to</param>
/// <returns>Task</returns>
public async Task WriteToStream(Stream stream)
public async Task WriteToStreamAsync(Stream stream)
{
// Do not dispose the writer, this will close the supplied stream and that is not our job!
var writer = new StreamWriter(stream, Encoding.UTF8);
Expand All @@ -112,10 +120,12 @@ public async Task WriteToStream(Stream stream)

// Before we are going to write, we need to check if the section header "[Sectionname]" is already written.
// If not, do so now before writing the properties of the section itself
if (!isSectionHeaderWritten) {
if (!isSectionHeaderWritten)
{
await writer.WriteLineAsync();
string description = section.GetSectionDescription();
if (!string.IsNullOrEmpty(description)) {
if (!string.IsNullOrEmpty(description))
{
await writer.WriteLineAsync(string.Format(";{0}", description));
}
await writer.WriteLineAsync(string.Format("[{0}]", section.GetSectionName()));
Expand Down Expand Up @@ -150,12 +160,13 @@ public async Task WriteToStream(Stream stream)
/// Initialize the IniConfig by reading all the properties from the file and setting them on the IniSections
/// </summary>
/// <returns>Task with bool indicating if the ini file was read</returns>
public async Task<bool> ReadFromFile(string filename)
public async Task<bool> ReadFromFileAsync(string filename)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException("filename");
}

if (File.Exists(filename))
{
var properties = await IniReader.ReadAsync(filename, Encoding.UTF8);
Expand All @@ -168,7 +179,7 @@ public async Task<bool> ReadFromFile(string filename)
/// Initialize the IniConfig by reading all the properties from the file and setting them on the IniSections
/// </summary>
/// <returns>Task with bool indicating if the ini file was read</returns>
public async Task<bool> ReadFromStream(Stream stream)
public async Task<bool> ReadFromStreamAsync(Stream stream)
{
var properties = await IniReader.ReadAsync(stream, Encoding.UTF8);
return FillSections(properties);
Expand Down

0 comments on commit 2becfe3

Please sign in to comment.