#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.Globalization;
using System.IO;
using System.Text;
namespace Nuclex.Support.Settings {
/// Represents an ini- or cfg-like configuration file
///
/// This class tries its best to preserve the formatting of configuration files.
/// Changing a value will keep the line it appears in intact.
///
public partial class ConfigurationFileStore : ISettingsStore {
#region class Category
/// Stores informations about a category found in the configuration file
private class Category {
/// Name of the category as a string
public StringSegment CategoryName;
/// Lookup table for the options in this category
public IDictionary OptionLookup;
/// Lines this category and its options consist of
public IList Lines;
}
#endregion // class Category
#region class Option
/// Stores informations about an option found in the configuration file
private class Option {
/// Index of the line the option is defined in
public int LineIndex;
/// Name of the option as a string
public StringSegment OptionName;
/// Value of the option as a string
public StringSegment OptionValue;
}
#endregion // class Option
/// Initializes a new, empty configuration file
public ConfigurationFileStore() {
this.options = new List();
this.categoryLookup = new Dictionary();
this.RootCategory = new Category() {
OptionLookup = new Dictionary(),
Lines = new List()
};
}
/// Saves the configuration file into the specified writer
/// Writer the configuration file will be saved into
public void Save(TextWriter writer) {
for(int index = 0; index < this.RootCategory.Lines.Count; ++index) {
writer.WriteLine(this.RootCategory.Lines[index]);
}
foreach(Category category in this.categoryLookup.Values) {
for(int index = 0; index < category.Lines.Count; ++index) {
writer.WriteLine(category.Lines[index]);
}
}
}
/// Enumerates the categories defined in the configuration
/// An enumerable list of all used categories
public IEnumerable EnumerateCategories() {
return this.categoryLookup.Keys;
}
/// Enumerates the options stored under the specified category
/// Category whose options will be enumerated
/// An enumerable list of all options in the category
public IEnumerable EnumerateOptions(string category = null) {
Category enumeratedCategory = getCategoryByName(category);
if(enumeratedCategory == null) {
yield break;
}
foreach(Option option in this.RootCategory.OptionLookup.Values) {
OptionInfo optionInfo = new OptionInfo() {
Name = option.OptionName.ToString(),
OptionType = getBestMatchingType(ref option.OptionValue)
};
yield return optionInfo;
}
}
/// Retrieves the value of the specified option
/// Type the option will be converted to
/// Category the option can be found in. Can be null.
/// Name of the option that will be looked up
/// The value of the option with the specified name
public TValue Get(string category, string optionName) {
TValue value;
if(TryGet(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"
);
}
}
}
/// Tries to retrieve the value of the specified option
/// Type the option will be converted to
/// Category the option can be found in. Can be null.
/// Name of the option that will be looked up
/// Will receive the value of the option, if found
///
/// True if the option existed and its value was written into the
/// parameter, false otherwise
///
public bool TryGet(string category, string optionName, out TValue value) {
Category containingCategory = getCategoryByName(category);
if(containingCategory != null) {
Option option;
if(containingCategory.OptionLookup.TryGetValue(optionName, out option)) {
value = (TValue)Convert.ChangeType(option.OptionValue.ToString(), typeof(TValue));
return true;
}
}
value = default(TValue);
return false;
}
/// Saves an option in the settings store
/// Type of value that will be saved
/// Category the option will be placed in. Can be null.
/// Name of the option that will be saved
/// The value under which the option will be saved
public void Set(string category, string optionName, TValue value) {
string valueAsString = (string)Convert.ChangeType(
value, typeof(string), CultureInfo.InvariantCulture
);
Category targetCategory;
if(string.IsNullOrEmpty(category)) {
targetCategory = this.RootCategory;
} else if(!this.categoryLookup.TryGetValue(category, out targetCategory)) {
targetCategory = createCategory(category);
createOption(targetCategory, optionName, valueAsString);
return;
}
Option targetOption;
if(targetCategory.OptionLookup.TryGetValue(optionName, out targetOption)) {
changeOption(targetCategory, targetOption, valueAsString);
} else {
createOption(targetCategory, optionName, valueAsString);
}
}
/// Removes the option with the specified name
/// Category the option is found in. Can be null.
/// Name of the option that will be removed
/// True if the option was found and removed
public bool Remove(string category, string optionName) {
Category sourceCategory = getCategoryByName(category);
if(sourceCategory == null) {
return false;
}
Option option;
if(!sourceCategory.OptionLookup.TryGetValue(optionName, out option)) {
return false;
}
sourceCategory.Lines.RemoveAt(option.LineIndex);
sourceCategory.OptionLookup.Remove(optionName);
foreach(Option shiftedOption in sourceCategory.OptionLookup.Values) {
if(shiftedOption.LineIndex > option.LineIndex) {
--shiftedOption.LineIndex;
}
}
return true;
}
/// Looks a category up by its name
///
/// Name of the category. Can be null for the root category
///
/// The category with the specified name
private Category getCategoryByName(string categoryName) {
Category category;
if(string.IsNullOrEmpty(categoryName)) {
category = this.RootCategory;
} else if(!this.categoryLookup.TryGetValue(categoryName, out category)) {
return null;
}
return category;
}
/// Creates a new option
/// Category the option will be added to
/// Name of the option
/// Value that will be assigned to the option
private void createOption(Category category, string name, string value) {
int valueLength;
if(value == null) {
valueLength = 0;
} else {
valueLength = value.Length;
}
// Build the complete line containing the option assignment
string line;
{
StringBuilder builder = new StringBuilder(name.Length + 3 + valueLength);
builder.Append(name);
builder.Append(" = ");
if(valueLength > 0) {
builder.Append(value);
}
line = builder.ToString();
}
Option newOption = new Option() {
OptionName = new StringSegment(line, 0, name.Length),
OptionValue = new StringSegment(line, name.Length + 3, valueLength)
};
// Figure out which line the new option should be put in
int lastLineIndex = category.Lines.Count - 1;
if((lastLineIndex > 0) && (category.Lines[lastLineIndex].Length == 0)) {
newOption.LineIndex = lastLineIndex;
category.Lines.Insert(lastLineIndex, line);
} else {
newOption.LineIndex = category.Lines.Count;
category.Lines.Add(line);
category.Lines.Add(string.Empty);
}
category.OptionLookup.Add(name, newOption);
}
/// Changes the value of an option
/// Category that holds the option
/// Option whose value will be changed
/// New value that will be assigned to the option
private void changeOption(Category category, Option option, string newValue) {
int newValueLength;
if(newValue == null) {
newValueLength = 0;
} else {
newValueLength = newValue.Length;
}
// Form the new line
string line = option.OptionValue.Text;
{
StringBuilder builder = new StringBuilder(
line.Length - option.OptionValue.Count + newValueLength
);
// Stuff before the value
if(option.OptionValue.Offset > 0) {
builder.Append(line, 0, option.OptionValue.Offset);
}
// The value itself
if(newValueLength > 0) {
builder.Append(newValue);
}
// Stuff after the value
int endIndex = option.OptionValue.Offset + option.OptionValue.Count;
if(endIndex < line.Length) {
builder.Append(line, endIndex, line.Length - endIndex);
}
line = builder.ToString();
}
option.OptionValue = new StringSegment(line, option.OptionValue.Offset, newValueLength);
category.Lines[option.LineIndex] = line;
}
/// Creates a new category in the configuration file
/// Name of the new category
/// The category that was created
private Category createCategory(string category) {
string categoryDefinition;
{
StringBuilder builder = new StringBuilder(category.Length + 2);
builder.Append('[');
builder.Append(category);
builder.Append(']');
categoryDefinition = builder.ToString();
}
Category newCategory = new Category() {
CategoryName = new StringSegment(categoryDefinition, 1, category.Length),
OptionLookup = new Dictionary(),
Lines = new List()
};
newCategory.Lines.Add(categoryDefinition);
newCategory.Lines.Add(string.Empty);
this.categoryLookup.Add(category, newCategory);
return newCategory;
}
/// Records where options are stored in the configuration file
private IList options;
/// Root category where options above any category definition go
private Category RootCategory;
/// Lookup table for all categories by their name
private IDictionary categoryLookup;
}
} // namespace Nuclex.Support.Settings