Uniglot/CSharp/Configure.csharp

120 lines
4.8 KiB
Plaintext

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<String> lines = new List<String>();
/// <summary>
/// Create a new Configure object
/// </summary>
/// <param name="filename">The Config file</param>
public Configure(String filename) {
if (!File.Exists(filename)) throw new Exception("File not found.");
StreamReader fileReader = new StreamReader(filename);
String line;
while (true) {
line = fileReader.ReadLine();
ReportMessage.Log(Verbosity.Verbose,String.Format("Read line: {0}",line));
if (line == null) break;
//Ignore comments prefixed with '#'
if (line.StartsWith("#")) continue;
if (line.IndexOf('#') > 0) {
lines.Add(line.Split('#')[0].Trim());
} else {
lines.Add(line.Trim());
}
}
fileReader.Close();
fileReader.Dispose();
}
/// <summary>
/// Create a new string dictionary from the named section.
/// </summary>
/// <param name="section">The name of the section header</param>
/// <returns>New Dictionary<String,String> of read key-value pairs from config section.</returns>
public Dictionary<String,String> ReadSection(String section) {
Dictionary<String,String> foundEntries = new Dictionary<String,String>();
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;
}
/// <summary>
/// If a conf section doesn't use key=value syntax, use this to grab the lines instead.
/// </summary>
/// <param name="section">The name of the section header</param>
/// <returns>New List<String> of read key-value pairs from config section.</returns>
public List<String> ReadSectionLines(String section) {
List<String> foundEntries = new List<String>();
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++) {
foundEntries.Add(lines[j]);
}
break; // Most configuration utilities don't allow multiple same-named sections
}
}
return foundEntries;
}
/// <summary>
/// Get the headers in a conf file.
/// </summary>
/// <returns>The headers from a file</returns>
public List<String> GetHeaders() {
List <String> foundHeaders = new List<String>();
int i = 0;
for (i = 0; i < lines.Count; i++) {
if (lines[i].Length > 5 && lines[i][0] == '[' && lines[i][lines[i].Length-1] == ']') {
string foundHeader = lines[i].Substring(2,lines[i].Length-4);
if (!foundHeaders.Contains(foundHeader)) foundHeaders.Add(foundHeader);
}
}
return foundHeaders;
}
/// <summary>
/// Get the lines from the file.
/// </summary>
/// <returns>The lines from the file</returns>
public List<String> GetLines() {
return this.lines;
}
/// <summary>
/// API for printing the configured section
/// </summary>
/// <param name="sectionEntries">The dictionary that has been read in</param>
public static void PrintSectionAsRead(Dictionary<String,String> sectionEntries) {
foreach (KeyValuePair<string,string> keyValue in sectionEntries) {
Console.WriteLine(String.Format("Key: {0} -- Value: {1}",keyValue.Key,keyValue.Value));
}
}
}
}