using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.IO; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; namespace Sarand.Net.Standard.Base.Core { //======================================================================= public class Property { //------------------------------------------------------------------- // Data members. private String section = "", name = "", value = ""; //------------------------------------------------------------------- // Construction. public Property (String section, String name, String value) { this.section = section; this.name = name; this.value = value; } //------------------------------------------------------------------- // Comparison. public bool Matches (String section, String name) { return (StringUtil.EqualsCI(this.section, section) && StringUtil.EqualsCI(this.name, name)); } //------------------------------------------------------------------- // Attributes. public String GetSection () { return section; } public String GetName () { return name; } public String GetValue () { return value; } //------------------------------------------------------------------- } //======================================================================= public class PropertySet { //------------------------------------------------------------------- // Data members. private List properties = new List(); //------------------------------------------------------------------- // Construction. public PropertySet () { } //------------------------------------------------------------------- /// /// Add property to set. /// public void AddProperty (String section, String name, String value) { properties.Add(new Property(section, name, value)); } //------------------------------------------------------------------- /// /// Retrieve value from property set. /// public String GetPropertyValue (String section, String name) { foreach (var property in properties) { if (property.Matches(section, name)) return property.GetValue(); } return ""; } //------------------------------------------------------------------- // Retrieve peoperties from file. public static PropertySet GetPropertiesFromFile (String filepath) { if (StringUtil.IsEmpty(filepath)) { SaLogger.Warn("PropertySet.GetPropertiesFromFile Blank file path specified"); return null; } if (!FileUtil.IsFile(filepath)) { SaLogger.Warn("PropertySet.GetPropertiesFromFile Invalid file specified: '" + filepath + "'"); return null; } PropertySet propertySet = new PropertySet(); String section = ""; foreach (var row in FileUtil.ReadAllFileLines(filepath)) { String data = row.Trim(); // Blank lines: if (String.IsNullOrEmpty(data)) continue; // Check for comment: else if (data.Left(1) == ";" || data.Left(2) == "//") continue; // Check for [Section]: else if (data[0] == '[') { section = data.Substring(1).Split(']')[0]; } else { // Otherwise it is a named value: propertySet.AddProperty(section, data.Split('=')[0], string.Join("=", data.Split('=').Skip(1).ToArray())); } } return propertySet; } //------------------------------------------------------------------- // Utilities. public void Dump () { System.Diagnostics.Debug.WriteLine("PropertySet dump:"); foreach (var property in properties) { string info = property.GetSection(); if (!String.IsNullOrEmpty(info)) info += "."; info += property.GetName() + "=" + property.GetValue(); System.Diagnostics.Debug.WriteLine("\t" + info); } } //------------------------------------------------------------------- } //======================================================================= }