diff --git a/CSharp/Configure.csharp b/CSharp/Configure.csharp new file mode 100644 index 0000000..7bf397c --- /dev/null +++ b/CSharp/Configure.csharp @@ -0,0 +1,68 @@ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; + +namespace AniNIX.Shared { + + public class Configure { + + // This is the file to configure from + private List lines = new List(); + + /// + /// Create a new Configure object + /// + /// The Config file + public Configure(String filename) { + StreamReader fileReader = new StreamReader(filename); + String line = fileReader.ReadLine().Trim(); + while (line != null) { + //Ignore comments prefixed with '#' + if (line.IndexOf('#') > 0) { + if (line[0] == '#') continue; + lines.Add(line.Split('#')[0].Trim()); + } else { + lines.Add(line); + } + line = fileReader.ReadLine(); + } + fileReader.Close(); + fileReader.Dispose(); + } + /// + /// Create a new string dictionary from the named section. + /// + /// The name of the section header + /// New Dictionary of read key-value pairs from config section. + public Dictionary ReadSection(String section) { + Dictionary foundEntries = new Dictionary(); + String header = String.Format("[ {0} ]",section); + int i = 0, j; + // Read to find the header + for (i = 0; i < lines.Count; i++) { + if (lines[i].Equals(header)) { + i++; + for (j = i; j < lines.Count && lines[j] != null && !String.IsNullOrWhiteSpace(lines[j]); j++) { + int index = lines[j].IndexOf('='); + String key = lines[j].Substring(0,index), value = lines[j].Substring(index+1); + if (foundEntries.ContainsKey(key)) { foundEntries[key] = value; } else { foundEntries.Add(key,value); } + } + break; // Most configuration utilities don't allow multiple same-named sections + } + } + return foundEntries; + } + + /// + /// API for printing the configured section + /// + /// The dictionary that has been read in + public static void PrintSectionAsRead(Dictionary sectionEntries) { + foreach (KeyValuePair keyValue in sectionEntries) { + Console.WriteLine(String.Format("Key: {0} -- Value: {1}",keyValue.Key,keyValue.Value)); + } + } + + } +}