/* DictionaryBase.cs * part of zaaReloaded2 * * Copyright 2015-2017 Daniel Kraus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace zaaReloaded2.Thesaurus { /// /// Base class for the /// and the ; implements methods /// to read configuration files. /// public abstract class ThesaurusBase { #region Public properties /// /// Gets a sorted dictionary of records. The records are sorted by the /// first field, and each record contains the entire set of fields, /// including the first field. /// public SortedDictionary Records { get; protected set; } #endregion #region Constructor /// /// Constructs a new dictionary, loading default data from a built-in /// resource, and user data from a custom file. /// public ThesaurusBase() { Records = new SortedDictionary(); LoadDefaultValues(); LoadUserValues(); } #endregion #region Public methods /// /// Returns true if the dictionary contains a record for /// . /// /// Dictionary key to look up. /// True if a record for /// exist, false if not. public bool HasRecord(string key) { return Records.ContainsKey(key); } #endregion #region Abstract methods /// /// Returns a content stream with a default configuration file. /// abstract protected Stream GetDefaultStream(); /// /// Returns the file name of the text file that contains user-defined /// values. /// /// File name. abstract protected string GetUserFileName(); #endregion #region Protected methods /// /// Reads a text file from a TextReader and fills the dictionary /// with the data. /// /// TextReader that provides a text file. protected void ReadFile(TextReader text) { LineParser lp = new LineParser(); string line; string key; while ((line = text.ReadLine()) != null) { lp.Line = line; if (lp.HasFields) { key = lp.Fields[0]; if (Records.ContainsKey(key)) { Records[key] = lp.Fields; } else { Records.Add(key, lp.Fields); } } } } /// /// Reads a text file from a stream and fills the dictionary /// with the data. /// /// TextReader that provides a text file. protected void ReadFile(Stream stream) { StreamReader sr = new StreamReader(stream); ReadFile(sr); } /// /// Loads default dictionary values from a text file that is built /// into the assembly as an embbedded resource. /// protected virtual void LoadDefaultValues() { ReadFile(GetDefaultStream()); } /// /// Attempts to load additional values from a user file whose path /// is provided by . /// protected virtual void LoadUserValues() { try { FileStream fs = new FileStream(GetUserFileName(), FileMode.Open, FileAccess.Read); ReadFile(fs); } catch { } } /// /// Looks up a dictionary key and returns an integer value from the requested field, /// or a default value if the field is empty or contains only dashes. /// /// Key to look up. /// Zero-based index of the field to look up /// (note that field 0 is the key itself). /// Default value that will be returned /// if the field is empty or contains only dashes. /// Value of the requested field, or /// if the field is empty or contains only dashes. /// if fieldNum is negative. protected int LookUpValue(string key, int fieldNum, int defaultValue) { string[] record; if (Records.TryGetValue(key, out record)) { if (fieldNum >= record.Length) return defaultValue; string value = record[fieldNum]; if (string.IsNullOrWhiteSpace(value) || _dashes.IsMatch(value)) { return defaultValue; } else { int intval; if (int.TryParse(value, out intval)) { return intval; } else { return defaultValue; } } } else { return defaultValue; } } /// /// Looks up a dictionary key and returns the value from the requested field, /// or a default value if the field is empty or contains only dashes. /// /// Key to look up. /// Zero-based index of the field to look up /// (note that field 0 is the key itself). /// Default value that will be returned /// if the field is empty or contains only dashes. /// Value of the requested field, or /// if the field is empty or contains only dashes. /// if fieldNum is negative. protected string LookUpValue(string key, int fieldNum, string defaultValue) { string[] record; if (Records.TryGetValue(key, out record)) { if (fieldNum >= record.Length) return defaultValue; string value = record[fieldNum]; if (string.IsNullOrWhiteSpace(value) || _dashes.IsMatch(value)) { return defaultValue; } else { return value; } } else { return defaultValue; } } /// /// Looks up a dictionary key and returns the value from the requested field, /// or an empty string if the field is empty or contains only dashes. /// /// Key to look up. /// Zero-based index of the field to look up /// (note that field 0 is the key itself). /// Value of the requested field, or /// if the field is empty or contains only dashes. /// if fieldNum is negative. protected string LookUpValue(string key, int fieldNum) { return LookUpValue(key, fieldNum, String.Empty); } /// /// Looks up a dictionary key and returns a boolean value from the requested field, /// or a default value if the field is empty or contains only dashes. /// /// /// The following case-insensitive text values are evaluated as "true": X, Y, J, +, /// 1, JA, TRUE, WAHR. /// /// Key to look up. /// Zero-based index of the field to look up /// (note that field 0 is the key itself). /// Default value that will be returned /// if the field is empty or contains only dashes. /// Value of the requested field, or /// if the field is empty or contains only dashes. /// if fieldNum is negative. protected bool LookUpValue(string key, int fieldNum, bool defaultValue) { string textValue = LookUpValue(key, fieldNum, String.Empty); switch (textValue.ToUpper()) { case "X": case "Y": case "J": case "+": case "1": case "JA": case "YES": case "TRUE": case "WAHR": return true; default: return defaultValue; } } #endregion #region Fields private static readonly Regex _dashes = new Regex("^-+$"); #endregion } }