The configuration file store now understands 'yes' and 'no' as booleans; implemented the Get() and TryGet() methods of the windows registry settings store
git-svn-id: file:///srv/devel/repo-conversion/nusu@317 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
e5f8b99519
commit
88105794a9
@ -401,6 +401,27 @@ namespace Nuclex.Support.Settings {
|
||||
Assert.That(() => load(fileContents), Throws.Exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that configuration files containing duplicate option names can not
|
||||
/// be used with the configuration file store
|
||||
/// </summary>
|
||||
[
|
||||
Test,
|
||||
TestCase("value = yes", true),
|
||||
TestCase("value = true", true),
|
||||
TestCase("value = no", false),
|
||||
TestCase("value = false", false)
|
||||
]
|
||||
public void BooleanLiteralsAreUnderstood(string fileContents, bool expectedValue) {
|
||||
ConfigurationFileStore configurationFile = load(fileContents);
|
||||
|
||||
if(expectedValue) {
|
||||
Assert.That(configurationFile.Get<bool>(null, "value"), Is.True);
|
||||
} else {
|
||||
Assert.That(configurationFile.Get<bool>(null, "value"), Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Loads a configuration file from a string</summary>
|
||||
/// <param name="fileContents">Contents of the configuration file</param>
|
||||
/// <returns>The configuration file loaded from the string</returns>
|
||||
|
@ -24,6 +24,8 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
using Nuclex.Support.Parsing;
|
||||
|
||||
namespace Nuclex.Support.Settings {
|
||||
|
||||
/// <summary>Represents an ini- or cfg-like configuration file</summary>
|
||||
@ -153,8 +155,20 @@ namespace Nuclex.Support.Settings {
|
||||
if(containingCategory != null) {
|
||||
Option option;
|
||||
if(containingCategory.OptionLookup.TryGetValue(optionName, out option)) {
|
||||
value = (TValue)Convert.ChangeType(option.OptionValue.ToString(), typeof(TValue));
|
||||
return true;
|
||||
if(typeof(TValue) == typeof(bool)) {
|
||||
bool? boolean = ParserHelper.ParseBooleanLiteral(ref option.OptionValue);
|
||||
if(boolean.HasValue) {
|
||||
value = (TValue)(object)boolean.Value;
|
||||
return true;
|
||||
} else {
|
||||
throw new FormatException(
|
||||
"The value '" + option.OptionValue.ToString() + "' is not a boolean"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
value = (TValue)Convert.ChangeType(option.OptionValue.ToString(), typeof(TValue));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,10 @@ License along with this library
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
using Nuclex.Support.Parsing;
|
||||
|
||||
namespace Nuclex.Support.Settings {
|
||||
@ -126,14 +128,11 @@ namespace Nuclex.Support.Settings {
|
||||
/// parameter, false otherwise
|
||||
/// </returns>
|
||||
public bool TryGet<TValue>(string category, string optionName, out TValue value) {
|
||||
throw new NotImplementedException();
|
||||
if(string.IsNullOrEmpty(category)) {
|
||||
object valueAsObject = this.rootKey.GetValue(optionName);
|
||||
value = (TValue)Convert.ChangeType(valueAsObject, typeof(TValue));
|
||||
return tryGetValueFromKey(this.rootKey, optionName, out value);
|
||||
} else {
|
||||
using(RegistryKey categoryKey = this.rootKey.OpenSubKey(category, this.writable)) {
|
||||
object valueAsObject = this.rootKey.GetValue(optionName);
|
||||
value = (TValue)Convert.ChangeType(valueAsObject, typeof(TValue));
|
||||
return tryGetValueFromKey(this.rootKey, optionName, out value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -155,6 +154,43 @@ namespace Nuclex.Support.Settings {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>Tries to retrieve the value of a registry key if it exists</summary>
|
||||
/// <typeparam name="TValue">Type of value the registry key is expected to have</typeparam>
|
||||
/// <param name="categoryKey">Registry key the value is stored under</param>
|
||||
/// <param name="optionName">Name of the option in the registry</param>
|
||||
/// <param name="value">Will receive the value read from the registry</param>
|
||||
/// <returns>True if the value was found, false otherwise</returns>
|
||||
private bool tryGetValueFromKey<TValue>(
|
||||
RegistryKey categoryKey, string optionName, out TValue value
|
||||
) {
|
||||
object valueAsObject = categoryKey.GetValue(optionName);
|
||||
if(valueAsObject == null) {
|
||||
value = default(TValue);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(typeof(TValue) == typeof(bool)) {
|
||||
string valueAsString = (string)Convert.ChangeType(
|
||||
valueAsObject, typeof(string), CultureInfo.InvariantCulture
|
||||
);
|
||||
|
||||
bool? boolean = ParserHelper.ParseBooleanLiteral(valueAsString);
|
||||
if(boolean.HasValue) {
|
||||
value = (TValue)(object)boolean.Value;
|
||||
return true;
|
||||
} else {
|
||||
throw new FormatException(
|
||||
"The value '" + valueAsString + "' can not be intepreted as a boolean"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
value = (TValue)Convert.ChangeType(
|
||||
valueAsObject, typeof(TValue), CultureInfo.InvariantCulture
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Figures out which .NET type best matches the registry value</summary>
|
||||
/// <param name="categoryKey">Registry key the key is stored in</param>
|
||||
/// <param name="optionName">Name of the option that will be retrieved</param>
|
||||
|
Loading…
Reference in New Issue
Block a user