Enumerating categories and options with the registry-based settings store is now working; updated mono-3.5 project

git-svn-id: file:///srv/devel/repo-conversion/nusu@316 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2014-07-22 10:43:23 +00:00
parent b6fe183994
commit e5f8b99519
6 changed files with 192 additions and 71 deletions

View File

@ -267,10 +267,6 @@
<Compile Include="Source\TypeHelper.Test.cs"> <Compile Include="Source\TypeHelper.Test.cs">
<DependentUpon>TypeHelper.cs</DependentUpon> <DependentUpon>TypeHelper.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Semaphore.cs" />
<Compile Include="Source\Semaphore.Test.cs">
<DependentUpon>Semaphore.cs</DependentUpon>
</Compile>
<Compile Include="Source\FloatHelper.cs" /> <Compile Include="Source\FloatHelper.cs" />
<Compile Include="Source\FloatHelper.Test.cs"> <Compile Include="Source\FloatHelper.Test.cs">
<DependentUpon>FloatHelper.cs</DependentUpon> <DependentUpon>FloatHelper.cs</DependentUpon>
@ -300,9 +296,6 @@
<DependentUpon>StringSegment.cs</DependentUpon> <DependentUpon>StringSegment.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\WeakReference.cs" /> <Compile Include="Source\WeakReference.cs" />
<Compile Include="Source\WeakReference.Phone7.cs">
<DependentUpon>WeakReference.cs</DependentUpon>
</Compile>
<Compile Include="Source\WeakReference.Test.cs"> <Compile Include="Source\WeakReference.Test.cs">
<DependentUpon>WeakReference.cs</DependentUpon> <DependentUpon>WeakReference.cs</DependentUpon>
</Compile> </Compile>

View File

@ -176,6 +176,75 @@ namespace Nuclex.Support.Parsing {
return false; return false;
} }
/// <summary>Tried to parse a boolean literal</summary>
/// <param name="value">Value that will be parsed as a boolean literal</param>
/// <returns>
/// True or false if the value was a boolean literal, null if it wasn't
/// </returns>
public static bool? ParseBooleanLiteral(string value) {
if(value == null) {
return null;
}
var stringSegment = new StringSegment(value, 0, value.Length);
return ParseBooleanLiteral(ref stringSegment);
}
/// <summary>Tried to parse a boolean literal</summary>
/// <param name="value">Value that will be parsed as a boolean literal</param>
/// <returns>
/// True or false if the value was a boolean literal, null if it wasn't
/// </returns>
public static bool? ParseBooleanLiteral(ref StringSegment value) {
switch(value.Count) {
// If the string spells 'no', it is considered a boolean
case 2: {
bool isSpellingNo =
((value.Text[value.Offset + 0] == 'n') || (value.Text[value.Offset + 0] == 'N')) &&
((value.Text[value.Offset + 1] == 'o') || (value.Text[value.Offset + 1] == 'O'));
return isSpellingNo ? new Nullable<bool>(false) : null;
}
// If the string spells 'yes', it is considered a boolean
case 3: {
bool isSpellingYes =
((value.Text[value.Offset + 0] == 'y') || (value.Text[value.Offset + 0] == 'Y')) &&
((value.Text[value.Offset + 1] == 'e') || (value.Text[value.Offset + 1] == 'E')) &&
((value.Text[value.Offset + 2] == 's') || (value.Text[value.Offset + 2] == 'S'));
return isSpellingYes ? new Nullable<bool>(true) : null;
}
// If the string spells 'true', it is considered a boolean
case 4: {
bool isSpellingTrue =
((value.Text[value.Offset + 0] == 't') || (value.Text[value.Offset + 0] == 'T')) &&
((value.Text[value.Offset + 1] == 'r') || (value.Text[value.Offset + 1] == 'R')) &&
((value.Text[value.Offset + 2] == 'u') || (value.Text[value.Offset + 2] == 'U')) &&
((value.Text[value.Offset + 3] == 'e') || (value.Text[value.Offset + 3] == 'E'));
return isSpellingTrue ? new Nullable<bool>(true) : null;
}
// If the string spells 'false', it is considered a boolean
case 5: {
bool isSpellingFalse =
((value.Text[value.Offset + 0] == 'f') || (value.Text[value.Offset + 0] == 'F')) &&
((value.Text[value.Offset + 1] == 'a') || (value.Text[value.Offset + 1] == 'A')) &&
((value.Text[value.Offset + 2] == 'l') || (value.Text[value.Offset + 2] == 'L')) &&
((value.Text[value.Offset + 3] == 's') || (value.Text[value.Offset + 3] == 'S')) &&
((value.Text[value.Offset + 4] == 'e') || (value.Text[value.Offset + 4] == 'E'));
return isSpellingFalse ? new Nullable<bool>(false) : null;
}
// Anything else is not considered a boolean
default: {
return null;
}
}
}
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing

View File

@ -253,67 +253,13 @@ namespace Nuclex.Support.Settings {
} }
// If it parses as a boolean literal, then it must be a boolean // If it parses as a boolean literal, then it must be a boolean
if(parseBooleanLiteral(ref value) != null) { if(ParserHelper.ParseBooleanLiteral(ref value) != null) {
return typeof(bool); return typeof(bool);
} }
return typeof(string); return typeof(string);
} }
/// <summary>Tried to parse a boolean literal</summary>
/// <param name="value">Value that will be parsed as a boolean literal</param>
/// <returns>
/// True or false if the value was a boolean literal, null if it wasn't
/// </returns>
private static bool? parseBooleanLiteral(ref StringSegment value) {
switch(value.Count) {
// If the string spells 'no', it is considered a boolean
case 2: {
bool isSpellingNo =
((value.Text[value.Offset + 0] == 'n') || (value.Text[value.Offset + 0] == 'N')) &&
((value.Text[value.Offset + 1] == 'o') || (value.Text[value.Offset + 1] == 'O'));
return isSpellingNo ? new Nullable<bool>(false) : null;
}
// If the string spells 'yes', it is considered a boolean
case 3: {
bool isSpellingYes =
((value.Text[value.Offset + 0] == 'y') || (value.Text[value.Offset + 0] == 'Y')) &&
((value.Text[value.Offset + 1] == 'e') || (value.Text[value.Offset + 1] == 'E')) &&
((value.Text[value.Offset + 2] == 's') || (value.Text[value.Offset + 2] == 'S'));
return isSpellingYes ? new Nullable<bool>(true) : null;
}
// If the string spells 'true', it is considered a boolean
case 4: {
bool isSpellingTrue =
((value.Text[value.Offset + 0] == 't') || (value.Text[value.Offset + 0] == 'T')) &&
((value.Text[value.Offset + 1] == 'r') || (value.Text[value.Offset + 1] == 'R')) &&
((value.Text[value.Offset + 2] == 'u') || (value.Text[value.Offset + 2] == 'U')) &&
((value.Text[value.Offset + 3] == 'e') || (value.Text[value.Offset + 3] == 'E'));
return isSpellingTrue ? new Nullable<bool>(true) : null;
}
// If the string spells 'false', it is considered a boolean
case 5: {
bool isSpellingFalse =
((value.Text[value.Offset + 0] == 'f') || (value.Text[value.Offset + 0] == 'F')) &&
((value.Text[value.Offset + 1] == 'a') || (value.Text[value.Offset + 1] == 'A')) &&
((value.Text[value.Offset + 2] == 'l') || (value.Text[value.Offset + 2] == 'L')) &&
((value.Text[value.Offset + 3] == 's') || (value.Text[value.Offset + 3] == 'S')) &&
((value.Text[value.Offset + 4] == 'e') || (value.Text[value.Offset + 4] == 'E'));
return isSpellingFalse ? new Nullable<bool>(false) : null;
}
// Anything else is not considered a boolean
default: {
return null;
}
}
}
} }
} // namespace Nuclex.Support.Configuration } // namespace Nuclex.Support.Configuration

View File

@ -29,6 +29,9 @@ namespace Nuclex.Support.Settings {
/// <summary>Unit tests for the windows registry settings store</summary> /// <summary>Unit tests for the windows registry settings store</summary>
[TestFixture] [TestFixture]
internal class WindowsRegistryStoreTest { internal class WindowsRegistryStoreTest {
} }
} // namespace Nuclex.Support.Settings } // namespace Nuclex.Support.Settings

View File

@ -23,28 +23,74 @@ License along with this library
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Win32;
using Nuclex.Support.Parsing;
namespace Nuclex.Support.Settings { namespace Nuclex.Support.Settings {
/// <summary>Stores settings in the registry on Windows operating systems</summary> /// <summary>Stores settings in the registry on Windows operating systems</summary>
public class WindowsRegistryStore : ISettingsStore, IDisposable { public class WindowsRegistryStore : ISettingsStore, IDisposable {
/// <summary>Initializes a new settings store on the specified registry path</summary>
/// <param name="hive">Hive in which to look</param>
/// <param name="directory">Base path of the settings in the specified hive</param>
/// <param name="writable">Whether to open the registry in writable mode</param>
public WindowsRegistryStore(RegistryHive hive, string directory, bool writable = true) {
using(RegistryKey hiveKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default)) {
this.rootKey = hiveKey.OpenSubKey(directory, writable);
}
this.writable = writable;
}
/// <summary>Initializes a new settings store on the specified registry key</summary>
/// <param name="rootKey">Registry key the settings are stored under</param>
/// <param name="writable">Whether the registry was opened in writable mode</param>
/// <remarks>
/// This constructor takes ownership of the registry key. It will be disposed when
/// the settings store is disposed.
/// </remarks>
public WindowsRegistryStore(RegistryKey rootKey, bool writable = true) {
this.rootKey = rootKey;
this.writable = writable;
}
/// <summary>Immediately releases all resources owned by the instance</summary> /// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() { public void Dispose() {
if(this.rootKey != null) {
this.rootKey.Dispose();
this.rootKey = null;
}
} }
/// <summary>Enumerates the categories defined in the configuration</summary> /// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns> /// <returns>An enumerable list of all used categories</returns>
public IEnumerable<string> EnumerateCategories() { public IEnumerable<string> EnumerateCategories() {
throw new NotImplementedException(); return this.rootKey.GetSubKeyNames();
} }
/// <summary>Enumerates the options stored under the specified category</summary> /// <summary>Enumerates the options stored under the specified category</summary>
/// <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) {
throw new NotImplementedException(); if(string.IsNullOrEmpty(category)) {
string[] valueNames = this.rootKey.GetValueNames();
for(int index = 0; index < valueNames.Length; ++index) {
yield return new OptionInfo() {
Name = valueNames[index],
OptionType = getBestMatchingType(this.rootKey, valueNames[index])
};
}
} else {
using(RegistryKey categoryKey = this.rootKey.OpenSubKey(category, this.writable)) {
string[] valueNames = categoryKey.GetValueNames();
for(int index = 0; index < valueNames.Length; ++index) {
yield return new OptionInfo() {
Name = valueNames[index],
OptionType = getBestMatchingType(categoryKey, valueNames[index])
};
}
}
}
} }
/// <summary>Retrieves the value of the specified option</summary> /// <summary>Retrieves the value of the specified option</summary>
@ -81,6 +127,15 @@ namespace Nuclex.Support.Settings {
/// </returns> /// </returns>
public bool TryGet<TValue>(string category, string optionName, out TValue value) { public bool TryGet<TValue>(string category, string optionName, out TValue value) {
throw new NotImplementedException(); throw new NotImplementedException();
if(string.IsNullOrEmpty(category)) {
object valueAsObject = this.rootKey.GetValue(optionName);
value = (TValue)Convert.ChangeType(valueAsObject, typeof(TValue));
} else {
using(RegistryKey categoryKey = this.rootKey.OpenSubKey(category, this.writable)) {
object valueAsObject = this.rootKey.GetValue(optionName);
value = (TValue)Convert.ChangeType(valueAsObject, typeof(TValue));
}
}
} }
/// <summary>Saves an option in the settings store</summary> /// <summary>Saves an option in the settings store</summary>
@ -100,6 +155,61 @@ namespace Nuclex.Support.Settings {
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <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>
/// <returns>The best matching .NET type for the registry key's value</returns>
private static Type getBestMatchingType(RegistryKey categoryKey, string optionName) {
RegistryValueKind valueKind = categoryKey.GetValueKind(optionName);
switch(valueKind) {
case RegistryValueKind.Binary: { return typeof(byte[]); }
case RegistryValueKind.DWord: { return typeof(int); }
case RegistryValueKind.QWord: { return typeof(long); }
case RegistryValueKind.MultiString: { return typeof(string[]); }
case RegistryValueKind.ExpandString:
case RegistryValueKind.String: {
string value = (string)categoryKey.GetValue(optionName);
if(value.Length == 0) {
return typeof(string);
}
// If there are at least two characters, it may be an integer with
// a sign in front of it
if(value.Length >= 2) {
int index = 0;
if(ParserHelper.SkipInteger(value, ref index)) {
if(index >= value.Length) {
return typeof(int);
}
if(value[index] == '.') {
return typeof(float);
}
}
} else { // If it's just a single character, it may be a number
if(char.IsNumber(value, 0)) {
return typeof(int);
}
}
// If it parses as a boolean literal, then it must be a boolean
if(ParserHelper.ParseBooleanLiteral(value) != null) {
return typeof(bool);
}
return typeof(string);
}
case RegistryValueKind.Unknown:
case RegistryValueKind.None:
default: { return typeof(string); }
}
}
/// <summary>Key on which the registry store is operating</summary>
private RegistryKey rootKey;
/// <summary>Whether the user can write to the registry key</summary>
private bool writable;
} }
} // namespace Nuclex.Support.Settings } // namespace Nuclex.Support.Settings

View File

@ -31,15 +31,15 @@ namespace Nuclex.Support {
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
[Serializable] [Serializable]
#endif #endif
public class WeakReference<ReferencedType> : WeakReference public class WeakReference<TReferenced> : WeakReference
where ReferencedType : class { where TReferenced : class {
/// <summary> /// <summary>
/// Initializes a new instance of the WeakReference class, referencing /// Initializes a new instance of the WeakReference class, referencing
/// the specified object. /// the specified object.
/// </summary> /// </summary>
/// <param name="target">The object to track or null.</param> /// <param name="target">The object to track or null.</param>
public WeakReference(ReferencedType target) : public WeakReference(TReferenced target) :
base(target) { } base(target) { }
/// <summary> /// <summary>
@ -51,7 +51,7 @@ namespace Nuclex.Support {
/// Indicates when to stop tracking the object. If true, the object is tracked /// Indicates when to stop tracking the object. If true, the object is tracked
/// after finalization; if false, the object is only tracked until finalization. /// after finalization; if false, the object is only tracked until finalization.
/// </param> /// </param>
public WeakReference(ReferencedType target, bool trackResurrection) : public WeakReference(TReferenced target, bool trackResurrection) :
base(target, trackResurrection) { } base(target, trackResurrection) { }
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
@ -89,8 +89,8 @@ namespace Nuclex.Support {
/// The reference to the target object is invalid. This can occur if the current /// The reference to the target object is invalid. This can occur if the current
/// System.WeakReference object has been finalized /// System.WeakReference object has been finalized
/// </exception> /// </exception>
public new ReferencedType Target { public new TReferenced Target {
get { return (base.Target as ReferencedType); } get { return (base.Target as TReferenced); }
set { base.Target = value; } set { base.Target = value; }
} }