Made some progress on my configuration file parser / writer; ParserHelper can now skip over floating point values

git-svn-id: file:///srv/devel/repo-conversion/nusu@298 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2014-07-19 11:04:19 +00:00
parent 0bb50d9254
commit ddc76cf09f
11 changed files with 513 additions and 79 deletions

View File

@ -197,6 +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\Configuration\ConfigurationFileStore.Parsing.cs">
<DependentUpon>ConfigurationFileStore.cs</DependentUpon>
</Compile>
<Compile Include="Source\Configuration\ISettingsStore.cs" />
<Compile Include="Source\Configuration\MemoryStore.cs" />
<Compile Include="Source\Configuration\OptionInfo.cs" />
<Compile Include="Source\Configuration\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">

View File

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Nuclex.Support.Configuration {
/// <summary>Represents an ini- or cfg-like configuration file</summary>
/// <remarks>
/// This class tries its best to preserve the formatting of configuration files.
/// Changing a value will keep the line it appears in intact.
/// </remarks>
public class ConfigurationFile {
/// <summary>Initializes a new, empty configuration file</summary>
public ConfigurationFile() {
this.lines = new List<string>();
}
/// <summary>Parses a configuration file from the specified text reader</summary>
/// <param name="reader">Reader the configuration file will be parsed from</param>
/// <returns>The configuration file parsed from the specified reader</returns>
public static ConfigurationFile Parse(TextReader reader) {
throw new NotImplementedException();
}
/// <summary>Saves the configuration file into the specified writer</summary>
/// <param name="writer">Writer the configuration file will be saved into</param>
public void Save(TextWriter writer) {
}
/// <summary>Lines contained in the configuration file</summary>
private IList<string> lines;
}
} // namespace Nuclex.Support.Configuration

View File

@ -0,0 +1,139 @@
#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.IO;
using Nuclex.Support.Parsing;
namespace Nuclex.Support.Configuration {
partial class ConfigurationFileStore {
/// <summary>Parses a configuration file from the specified text reader</summary>
/// <param name="reader">Reader the configuration file will be parsed from</param>
/// <returns>The configuration file parsed from the specified reader</returns>
public static ConfigurationFileStore Parse(TextReader reader) {
var store = new ConfigurationFileStore();
for(; ; ) {
string line = reader.ReadLine();
if(line == null) {
return store;
}
}
}
/// <summary>Determines the best matching type for an option value</summary>
/// <param name="value">Value for which the best matching type will be found</param>
/// <returns>The best matching type for the specified option value</returns>
private static Type getBestMatchingType(ref StringSegment value) {
if(value.Count == 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.Count >= 2) {
int index = value.Offset;
if(ParserHelper.SkipInteger(value.Text, ref index)) {
if(index < value.Offset + value.Count) {
if(value.Text[index] == '.') {
return typeof(float);
}
}
return typeof(int);
}
} else if(value.Count >= 1) { // If it's at least one character, it may be a number
int index = value.Offset;
ParserHelper.SkipNumericals(value.Text, ref index);
if(index > value.Offset) {
return typeof(int);
}
}
// If it parses as a boolean literal, then it must be a boolean
if(parseBooleanLiteral(ref value) != null) {
return typeof(bool);
}
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

View File

@ -0,0 +1,167 @@
#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.IO;
using Nuclex.Support.Parsing;
namespace Nuclex.Support.Configuration {
/// <summary>Represents an ini- or cfg-like configuration file</summary>
/// <remarks>
/// This class tries its best to preserve the formatting of configuration files.
/// Changing a value will keep the line it appears in intact.
/// </remarks>
public partial class ConfigurationFileStore : ISettingsStore {
#region class Category
/// <summary>Stores informations about a category found in the configuration file</summary>
private class Category {
/// <summary>Index of the line the category is defined in</summary>
public int LineIndex;
/// <summary>Name of the category as a string</summary>
public StringSegment CategoryName;
}
#endregion // class Category
#region class Option
/// <summary>Stores informations about an option found in the configuration file</summary>
private class Option {
/// <summary>Index of the line the option is defined in</summary>
public int LineIndex;
/// <summary>Name of the option as a string</summary>
public StringSegment OptionName;
/// <summary>Value of the option as a string</summary>
public StringSegment OptionValue;
}
#endregion // class Option
/// <summary>Initializes a new, empty configuration file</summary>
public ConfigurationFileStore() {
this.lines = new List<string>();
this.categories = new List<Category>();
this.options = new List<Option>();
}
/// <summary>Saves the configuration file into the specified writer</summary>
/// <param name="writer">Writer the configuration file will be saved into</param>
public void Save(TextWriter writer) {
for(int index = 0; index < this.lines.Count; ++index) {
writer.WriteLine(this.lines[index]);
}
}
/// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns>
public IEnumerable<string> EnumerateCategories() {
for(int index = 0; index < this.categories.Count; ++index) {
yield return this.categories[index].CategoryName.ToString();
}
}
/// <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) {
for(int index = 0; index < this.options.Count; ++index) {
OptionInfo optionInfo = new OptionInfo() {
Name = this.options[index].OptionName.ToString(),
OptionType = getBestMatchingType(ref this.options[index].OptionValue)
};
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 configuration file"
);
} else {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' under the category '" +
category + "' in the configuration file"
);
}
}
}
/// <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>Lines contained in the configuration file</summary>
private IList<string> lines;
/// <summary>Records where categories are stored in the configuration file</summary>
private IList<Category> categories;
/// <summary>Records where options are stored in the configuration file</summary>
private IList<Option> options;
}
} // namespace Nuclex.Support.Configuration

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nuclex.Support.Configuration {
/// <summary>Interface by which settings stored in a file can be accessed</summary>
public interface ISettings {
/// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns>
IEnumerable<string> EnumerateCategories();
/// <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>
IEnumerable<OptionInfo> EnumerateOptions(string category = null);
TValue Get<TValue>(string category, string optionName);
TValue Get<TValue>(string optionName);
}
} // namespace Nuclex.Support.Configuration

View File

@ -0,0 +1,80 @@
#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.Configuration {
/// <summary>Interface by which settings and configuration data can be accessed</summary>
public interface ISettingsStore {
/// <summary>Enumerates the categories defined in the configuration</summary>
/// <returns>An enumerable list of all used categories</returns>
IEnumerable<string> EnumerateCategories();
/// <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>
IEnumerable<OptionInfo> EnumerateOptions(string category = null);
/// <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>
TValue Get<TValue>(string category, string optionName);
/// <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>
bool TryGet<TValue>(string category, string optionName, out TValue value);
/// <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>
void Set<TValue>(string category, string optionName, TValue value);
/// <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>
bool Remove(string category, string optionName);
}
} // namespace Nuclex.Support.Configuration
#if WANT_TO_SUPPORT_MESSED_UP_CONFIGURATION_FILES
/// <remarks>
/// Some settings stores allow multiple options with the same name to exist.
/// If you request a collection of values (IEnumerable, ICollection, IList or their
/// generic variants), you will be given a collection of all values appearing
/// in the scope you specified.
/// </remarks>
#endif

View File

@ -0,0 +1,29 @@
#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,4 +1,24 @@
using System; #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 { namespace Nuclex.Support.Configuration {

View File

@ -0,0 +1,34 @@
#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

@ -93,7 +93,7 @@ namespace Nuclex.Support.Parsing {
public void CanSkipNumbersInNullString() { public void CanSkipNumbersInNullString() {
int index = 0; int index = 0;
Assert.DoesNotThrow( Assert.DoesNotThrow(
delegate() { ParserHelper.SkipNumbers((string)null, ref index); } delegate() { ParserHelper.SkipNumericals((string)null, ref index); }
); );
Assert.AreEqual(0, index); Assert.AreEqual(0, index);
} }
@ -103,7 +103,7 @@ namespace Nuclex.Support.Parsing {
public void CanSkipNumbersInEmptyString() { public void CanSkipNumbersInEmptyString() {
int index = 0; int index = 0;
Assert.DoesNotThrow( Assert.DoesNotThrow(
delegate() { ParserHelper.SkipNumbers(string.Empty, ref index); } delegate() { ParserHelper.SkipNumericals(string.Empty, ref index); }
); );
Assert.AreEqual(0, index); Assert.AreEqual(0, index);
} }
@ -112,7 +112,7 @@ namespace Nuclex.Support.Parsing {
[Test] [Test]
public void NumbersCanBeSkipped() { public void NumbersCanBeSkipped() {
int index = 6; int index = 6;
ParserHelper.SkipNumbers("123abc456def789", ref index); ParserHelper.SkipNumericals("123abc456def789", ref index);
Assert.AreEqual(9, index); Assert.AreEqual(9, index);
} }

View File

@ -68,7 +68,7 @@ namespace Nuclex.Support.Parsing {
/// This skips only numeric characters, but not complete numbers -- if the number /// This skips only numeric characters, but not complete numbers -- if the number
/// begins with a minus or plus sign, for example, this function will not skip it. /// begins with a minus or plus sign, for example, this function will not skip it.
/// </remarks> /// </remarks>
public static void SkipNumbers(string text, ref int index) { public static void SkipNumericals(string text, ref int index) {
if(text == null) { if(text == null) {
return; return;
} }
@ -102,14 +102,14 @@ namespace Nuclex.Support.Parsing {
if((text[index] == '-') || (text[index] == '+')) { if((text[index] == '-') || (text[index] == '+')) {
nextIndex = index + 1; nextIndex = index + 1;
SkipNumbers(text, ref nextIndex); SkipNumericals(text, ref nextIndex);
if(nextIndex == (index + 1)) { if(nextIndex == (index + 1)) {
return false; return false;
} }
} else { } else {
nextIndex = index; nextIndex = index;
SkipNumbers(text, ref nextIndex); SkipNumericals(text, ref nextIndex);
if(nextIndex == index) { if(nextIndex == index) {
return false; return false;
} }
@ -154,6 +154,28 @@ namespace Nuclex.Support.Parsing {
} }
} }
/// <summary>Skips a floating point value appearing in the input text</summary>
/// <param name="text">Text in which a floating point value will be skipped</param>
/// <param name="index">Index at which the floating point value begins</param>
/// <returns>True if the floating point value was skipped, otherwise false</returns>
public static bool SkipFloat(string text, ref int index) {
if(SkipInteger(text, ref index)) {
if(index < text.Length) {
if(text[index] == '.') {
++index;
SkipNumericals(text, ref index);
}
if((text[index] == 'e') || (text[index] == 'E')) {
throw new NotImplementedException("Exponential format not supported yet");
}
}
return true;
}
return false;
}
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing