using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; namespace TestConfReader { /// /// Class Program. Application driver. /// class Program { /// /// Tries to read the configuration file given as first parameter. /// /// program command line parameters static void Main(string[] args) { if (args.Length == 1) { Dictionary settings = new Dictionary(); bool bOk = Program.Test(args[0], settings); if (bOk == true) { Console.WriteLine("TestConfReader - Success"); Console.WriteLine("Values:"); foreach (string key in settings.Keys) { object value = settings[key]; Console.WriteLine("\t{0} : {1} - {2}", key, value, value.GetType().FullName); } } else { Console.WriteLine("TestConfReader - Failed"); Console.WriteLine("File {0} not existing or wrong", args[0]); } } else { Console.WriteLine("TestConfReader - Sample program which test configuration files"); Console.WriteLine(); Console.WriteLine("usage: testconf path_to_conf_file"); Console.WriteLine(); } } /// /// Tests the configuration file and tries to read values. If successful /// these values are put into the dictionary passed as parameter. /// /// path to configuration files /// target dictionary /// true if the file contains no errors and exists static bool Test(string path, Dictionary settings) { if (File.Exists(path) == true) { using (StreamReader reader = new StreamReader(path)) { Regex numberPattern = new Regex(@"^[\+|-]?\d+[\.|,]?\d+$"); do { string line = reader.ReadLine(); if (line != null) { line = line.Trim(); // first we skip comments. Comment lines are // identified by the # character. If we have // to write multi-line comments we have to // put at the beginning of each line #. // // we also skip empty lines if ((line.StartsWith("#") == false) && (line != string.Empty)) { // ok if this is not a comment then // the form should be: // // PropertyName PropertyValue int divide = line.IndexOf(' '); if (divide < 0) { return false; } string pName = line.Substring(0, divide); // property values can be either strings // or numbers. Strings are not allowed to // contain spaces. If you need to put spaces // inside enclose the whole string within "". string pValue = line.Substring(divide + 1); pValue = pValue.Trim(); if ((pValue.StartsWith("\"") == true) && (pValue.EndsWith("\"") == true)) { pValue = pValue.Substring(1, pValue.Length - 2); settings[pName] = pValue; } else { string lpValue = pValue.ToLower(); // maybe a boolean value? if (lpValue == "true") { settings[pName] = true; } else if (lpValue == "false") { settings[pName] = false; } // or a number? else if (numberPattern.Match(pValue).Success == true) { int iValue = 0; float fValue = 0.0f; double dValue = 0.0d; decimal mValue = 0; // ok the property is a number // // starting with int if (int.TryParse(pValue, out iValue) == true) { settings[pName] = iValue; } // float? else if (float.TryParse(pValue, out fValue) == true) { settings[pName] = fValue; } // double? else if (double.TryParse(pValue, out dValue) == true) { settings[pName] = dValue; } // decimal? else if (decimal.TryParse(pValue, out mValue) == true) { settings[pName] = mValue; } else { // unrecognized format // saving it as a string. settings[pName] = pValue; } } else { // ok the property value is a string settings[pName] = pValue; } } } } } while (reader.EndOfStream == false); } return true; } return false; } } }