Renamed the configuration namespace to settings; more progress on the configuration file parser; partially implemented the memory settings store

git-svn-id: file:///srv/devel/repo-conversion/nusu@299 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2014-07-19 22:16:41 +00:00
parent ddc76cf09f
commit 2c3d9db4c9
9 changed files with 342 additions and 82 deletions

View File

@ -197,14 +197,14 @@
<Compile Include="Source\Collections\WeakCollection.Test.cs"> <Compile Include="Source\Collections\WeakCollection.Test.cs">
<DependentUpon>WeakCollection.cs</DependentUpon> <DependentUpon>WeakCollection.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Configuration\ConfigurationFileStore.cs" /> <Compile Include="Source\Settings\ConfigurationFileStore.cs" />
<Compile Include="Source\Configuration\ConfigurationFileStore.Parsing.cs"> <Compile Include="Source\Settings\ConfigurationFileStore.Parsing.cs">
<DependentUpon>ConfigurationFileStore.cs</DependentUpon> <DependentUpon>ConfigurationFileStore.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Configuration\ISettingsStore.cs" /> <Compile Include="Source\Settings\ISettingsStore.cs" />
<Compile Include="Source\Configuration\MemoryStore.cs" /> <Compile Include="Source\Settings\MemoryStore.cs" />
<Compile Include="Source\Configuration\OptionInfo.cs" /> <Compile Include="Source\Settings\OptionInfo.cs" />
<Compile Include="Source\Configuration\WindowsRegistryStore.cs" /> <Compile Include="Source\Settings\WindowsRegistryStore.cs" />
<Compile Include="Source\GarbagePolicy.cs" /> <Compile Include="Source\GarbagePolicy.cs" />
<Compile Include="Source\IO\PartialStream.cs" /> <Compile Include="Source\IO\PartialStream.cs" />
<Compile Include="Source\IO\PartialStream.Test.cs"> <Compile Include="Source\IO\PartialStream.Test.cs">
@ -317,7 +317,6 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Documents\CommandLine.txt" />
<Content Include="Documents\DoubleConverter.txt" /> <Content Include="Documents\DoubleConverter.txt" />
<Content Include="Documents\Nuclex.Support.txt" /> <Content Include="Documents\Nuclex.Support.txt" />
<Content Include="Documents\Request Framework.txt" /> <Content Include="Documents\Request Framework.txt" />

View File

@ -1,29 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2014 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nuclex.Support.Source.Configuration {
class MemoryStore {
}
}

View File

@ -1,34 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2014 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
namespace Nuclex.Support.Configuration {
/// <summary>Stores settings in the registry of Windows systems</summary>
public class WindowsRegistryStore : IDisposable {
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
}
}
} // namespace Nuclex.Support.Configuration

View File

@ -23,24 +23,102 @@ using System.IO;
using Nuclex.Support.Parsing; using Nuclex.Support.Parsing;
namespace Nuclex.Support.Configuration { namespace Nuclex.Support.Settings {
partial class ConfigurationFileStore { partial class ConfigurationFileStore {
#region class ParserState
/// <summary>Remembers the target store and current category of the parser</summary>
private class ParserState {
/// <summary>Store to which the parsed categories and options will be added</summary>
public ConfigurationFileStore Store;
/// <summary>Current category options belong to</summary>
public Category Category;
}
#endregion // class ParserState
/// <summary>Parses a configuration file from the specified text reader</summary> /// <summary>Parses a configuration file from the specified text reader</summary>
/// <param name="reader">Reader the configuration file will be parsed from</param> /// <param name="reader">Reader the configuration file will be parsed from</param>
/// <returns>The configuration file parsed from the specified reader</returns> /// <returns>The configuration file parsed from the specified reader</returns>
public static ConfigurationFileStore Parse(TextReader reader) { public static ConfigurationFileStore Parse(TextReader reader) {
var store = new ConfigurationFileStore(); var store = new ConfigurationFileStore();
var state = new ParserState() {
Store = store,
Category = store.RootCategory
};
for(; ; ) { for(; ; ) {
string line = reader.ReadLine(); string line = reader.ReadLine();
if(line == null) { if(line == null) {
break;
}
parseLine(state, line);
}
return store; return store;
} }
/// <summary>Incrementally parses a line read from a configuration file</summary>
/// <param name="state">Current parser state</param>
/// <param name="line">Line that has been read</param>
private static void parseLine(ParserState state, string line) {
state.Store.lines.Add(line);
// If the line is empty, ignore it
int length = line.Length;
if(length == 0) {
return;
} }
// Skip all spaces at the beginning of the line
int firstCharacterIndex = 0;
ParserHelper.SkipSpaces(line, ref firstCharacterIndex);
// If the line contained nothing but spaces, ignore it
if(firstCharacterIndex == length) {
return;
}
// If the line is a comment, ignore it
if((line[firstCharacterIndex] == '#') || (line[firstCharacterIndex] == ';')) {
return;
}
// Now the line is either a category definition or some attempt to set an option
if(line[firstCharacterIndex] == '[') {
parseCategory(state, line, firstCharacterIndex);
} else {
parseOption(state, line, firstCharacterIndex);
}
}
/// <summary>Parses a category definition encountered on a line</summary>
/// <param name="state">Current parser state</param>
/// <param name="line">Line containing the category definition</param>
/// <param name="firstCharacterIndex">Index of the definition's first character</param>
private static void parseCategory(
ParserState state, string line, int firstCharacterIndex
) {
throw new NotImplementedException();
}
/// <summary>Parses an option definition encountered on a line</summary>
/// <param name="state">Current parser state</param>
/// <param name="line">Line containing the option definition</param>
/// <param name="firstCharacterIndex">Index of the definition's first character</param>
private static void parseOption(
ParserState state, string line, int firstCharacterIndex
) {
Option option = new Option() {
LineIndex = state.Store.lines.Count - 1
};
throw new NotImplementedException();
} }
/// <summary>Determines the best matching type for an option value</summary> /// <summary>Determines the best matching type for an option value</summary>

View File

@ -24,7 +24,7 @@ using System.IO;
using Nuclex.Support.Parsing; using Nuclex.Support.Parsing;
namespace Nuclex.Support.Configuration { namespace Nuclex.Support.Settings {
/// <summary>Represents an ini- or cfg-like configuration file</summary> /// <summary>Represents an ini- or cfg-like configuration file</summary>
/// <remarks> /// <remarks>
@ -44,6 +44,9 @@ namespace Nuclex.Support.Configuration {
/// <summary>Name of the category as a string</summary> /// <summary>Name of the category as a string</summary>
public StringSegment CategoryName; public StringSegment CategoryName;
/// <summary>Lookup table for the options in this category</summary>
public IDictionary<string, Option> OptionLookup;
} }
#endregion // class Category #endregion // class Category
@ -71,6 +74,11 @@ namespace Nuclex.Support.Configuration {
this.lines = new List<string>(); this.lines = new List<string>();
this.categories = new List<Category>(); this.categories = new List<Category>();
this.options = new List<Option>(); this.options = new List<Option>();
this.categoryLookup = new Dictionary<string, Category>();
this.RootCategory = new Category() {
LineIndex = -1,
OptionLookup = new Dictionary<string, Option>()
};
} }
/// <summary>Saves the configuration file into the specified writer</summary> /// <summary>Saves the configuration file into the specified writer</summary>
@ -93,10 +101,20 @@ namespace Nuclex.Support.Configuration {
/// <param name="category">Category whose options will be enumerated</param> /// <param name="category">Category whose options will be enumerated</param>
/// <returns>An enumerable list of all options in the category</returns> /// <returns>An enumerable list of all options in the category</returns>
public IEnumerable<OptionInfo> EnumerateOptions(string category = null) { public IEnumerable<OptionInfo> EnumerateOptions(string category = null) {
for(int index = 0; index < this.options.Count; ++index) { Category enumeratedCategory;
if(string.IsNullOrEmpty(category)) {
enumeratedCategory = this.RootCategory;
} else if(!this.categoryLookup.TryGetValue(category, out enumeratedCategory)) {
throw new KeyNotFoundException(
"There is no category named '" + category + "' in the configuration file"
);
}
foreach(Option option in this.RootCategory.OptionLookup.Values) {
OptionInfo optionInfo = new OptionInfo() { OptionInfo optionInfo = new OptionInfo() {
Name = this.options[index].OptionName.ToString(), Name = option.OptionName.ToString(),
OptionType = getBestMatchingType(ref this.options[index].OptionValue) OptionType = getBestMatchingType(ref option.OptionValue)
}; };
yield return optionInfo; yield return optionInfo;
} }
@ -157,11 +175,17 @@ namespace Nuclex.Support.Configuration {
/// <summary>Lines contained in the configuration file</summary> /// <summary>Lines contained in the configuration file</summary>
private IList<string> lines; private IList<string> lines;
/// <summary>Records where categories are stored in the configuration file</summary> /// <summary>Records where categories are stored in the configuration file</summary>
private IList<Category> categories; private IList<Category> categories;
/// <summary>Records where options are stored in the configuration file</summary> /// <summary>Records where options are stored in the configuration file</summary>
private IList<Option> options; private IList<Option> options;
/// <summary>Root category where options above any category definition go</summary>
private Category RootCategory;
/// <summary>Lookup table for all categories by their name</summary>
private IDictionary<string, Category> categoryLookup;
} }
} // namespace Nuclex.Support.Configuration } // namespace Nuclex.Support.Settings

View File

@ -21,7 +21,7 @@ License along with this library
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Nuclex.Support.Configuration { namespace Nuclex.Support.Settings {
/// <summary>Interface by which settings and configuration data can be accessed</summary> /// <summary>Interface by which settings and configuration data can be accessed</summary>
public interface ISettingsStore { public interface ISettingsStore {
@ -68,7 +68,7 @@ namespace Nuclex.Support.Configuration {
} }
} // namespace Nuclex.Support.Configuration } // namespace Nuclex.Support.Settings
#if WANT_TO_SUPPORT_MESSED_UP_CONFIGURATION_FILES #if WANT_TO_SUPPORT_MESSED_UP_CONFIGURATION_FILES
/// <remarks> /// <remarks>

View File

@ -0,0 +1,121 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2014 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Settings {
/// <summary>Stores settings in memory</summary>
public class MemoryStore : ISettingsStore {
/// <summary>Initializes a new settings store managing settings in memory</summary>
public MemoryStore() {
this.options = new Dictionary<string, IDictionary<string, object>>();
this.rootOptions = new Dictionary<string, object>();
}
/// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns>
public IEnumerable<string> EnumerateCategories() {
return this.options.Keys;
}
/// <summary>Enumerates the options stored under the specified category</summary>
/// <param name="category">Category whose options will be enumerated</param>
/// <returns>An enumerable list of all options in the category</returns>
public IEnumerable<OptionInfo> EnumerateOptions(string category = null) {
IDictionary<string, object> categoryOptions;
if(string.IsNullOrEmpty(category)) {
categoryOptions = this.rootOptions;
} else if(!this.options.TryGetValue(category, out categoryOptions)) {
yield break;
}
foreach(KeyValuePair<string, object> option in categoryOptions) {
OptionInfo optionInfo = new OptionInfo() {
Name = option.Key,
OptionType = option.Value.GetType()
};
yield return optionInfo;
}
}
/// <summary>Retrieves the value of the specified option</summary>
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
/// <param name="category">Category the option can be found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be looked up</param>
/// <returns>The value of the option with the specified name</returns>
public TValue Get<TValue>(string category, string optionName) {
TValue value;
if(TryGet<TValue>(category, optionName, out value)) {
return value;
} else {
if(string.IsNullOrEmpty(category)) {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' in the settings"
);
} else {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' under the category '" +
category + "' in the settings"
);
}
}
}
/// <summary>Tries to retrieve the value of the specified option</summary>
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
/// <param name="category">Category the option can be found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be looked up</param>
/// <param name="value">Will receive the value of the option, if found</param>
/// <returns>
/// True if the option existed and its value was written into the <paramref name="value" />
/// parameter, false otherwise
/// </returns>
public bool TryGet<TValue>(string category, string optionName, out TValue value) {
throw new NotImplementedException();
}
/// <summary>Saves an option in the settings store</summary>
/// <typeparam name="TValue">Type of value that will be saved</typeparam>
/// <param name="category">Category the option will be placed in. Can be null.</param>
/// <param name="optionName">Name of the option that will be saved</param>
/// <param name="value">The value under which the option will be saved</param>
public void Set<TValue>(string category, string optionName, TValue value) {
throw new NotImplementedException();
}
/// <summary>Removes the option with the specified name</summary>
/// <param name="category">Category the option is found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be removed</param>
/// <returns>True if the option was found and removed</returns>
public bool Remove(string category, string optionName) {
throw new NotImplementedException();
}
/// <summary>Categories and the options stored in them</summary>
private IDictionary<string, IDictionary<string, object>> options;
/// <summary>Options stored at the root level</summary>
private IDictionary<string, object> rootOptions;
}
} // namespace Nuclex.Support.Settings

View File

@ -20,7 +20,7 @@ License along with this library
using System; using System;
namespace Nuclex.Support.Configuration { namespace Nuclex.Support.Settings {
/// <summary>Informations about an option stored in a settings container</summary> /// <summary>Informations about an option stored in a settings container</summary>
public struct OptionInfo { public struct OptionInfo {
@ -32,4 +32,4 @@ namespace Nuclex.Support.Configuration {
} }
} // namespace Nuclex.Support.Configuration } // namespace Nuclex.Support.Settings

View File

@ -0,0 +1,101 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2014 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Settings {
/// <summary>Stores settings in the registry on Windows operating systems</summary>
public class WindowsRegistryStore : ISettingsStore, IDisposable {
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
}
/// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns>
public IEnumerable<string> EnumerateCategories() {
throw new NotImplementedException();
}
/// <summary>Enumerates the options stored under the specified category</summary>
/// <param name="category">Category whose options will be enumerated</param>
/// <returns>An enumerable list of all options in the category</returns>
public IEnumerable<OptionInfo> EnumerateOptions(string category = null) {
throw new NotImplementedException();
}
/// <summary>Retrieves the value of the specified option</summary>
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
/// <param name="category">Category the option can be found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be looked up</param>
/// <returns>The value of the option with the specified name</returns>
public TValue Get<TValue>(string category, string optionName) {
TValue value;
if(TryGet<TValue>(category, optionName, out value)) {
return value;
} else {
if(string.IsNullOrEmpty(category)) {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' in the registry"
);
} else {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' under the category '" +
category + "' in the registry"
);
}
}
}
/// <summary>Tries to retrieve the value of the specified option</summary>
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
/// <param name="category">Category the option can be found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be looked up</param>
/// <param name="value">Will receive the value of the option, if found</param>
/// <returns>
/// True if the option existed and its value was written into the <paramref name="value" />
/// parameter, false otherwise
/// </returns>
public bool TryGet<TValue>(string category, string optionName, out TValue value) {
throw new NotImplementedException();
}
/// <summary>Saves an option in the settings store</summary>
/// <typeparam name="TValue">Type of value that will be saved</typeparam>
/// <param name="category">Category the option will be placed in. Can be null.</param>
/// <param name="optionName">Name of the option that will be saved</param>
/// <param name="value">The value under which the option will be saved</param>
public void Set<TValue>(string category, string optionName, TValue value) {
throw new NotImplementedException();
}
/// <summary>Removes the option with the specified name</summary>
/// <param name="category">Category the option is found in. Can be null.</param>
/// <param name="optionName">Name of the option that will be removed</param>
/// <returns>True if the option was found and removed</returns>
public bool Remove(string category, string optionName) {
throw new NotImplementedException();
}
}
} // namespace Nuclex.Support.Settings