using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XeroBrowser { internal class Config { private static string Cfg_File = Environment.GetEnvironmentVariable("appdata") + "/Xero Browser/prefrences.conf"; private const string SEPERATOR = "="; private static Dictionary cfgEntries = new Dictionary(); static Config() { loadCfg(); } private static void loadCfg() { cfgEntries.Clear(); if (!File.Exists(Cfg_File)) saveCfg(); using (StreamReader cfgReader = File.OpenText(Cfg_File)) { for (string? line = cfgReader.ReadLine(); line is not null; line = cfgReader.ReadLine()) { if (line.StartsWith("#")) continue; string[] values = line.Split(SEPERATOR.First()); cfgEntries.Add(values.First().Trim(), String.Join(SEPERATOR, values.Skip(1)).Trim()); } } } private static void saveCfg() { using (StreamWriter cfgWriter = File.CreateText(Cfg_File)) { cfgWriter.WriteLine("############################################################################################"); cfgWriter.WriteLine("# Caution: This is your settings file for Xero Browser! #"); cfgWriter.WriteLine("# Modifying anything in this file could cause things to break and is thus not reccomended. #"); cfgWriter.WriteLine("# Continue at your own risk! #"); cfgWriter.WriteLine("############################################################################################"); foreach (KeyValuePair values in cfgEntries) { cfgWriter.WriteLine(String.Join(SEPERATOR, new String[] { values.Key.Trim(), values.Value.Trim() })); } } } public static void SetEntry(string key, string value) { key = key.Trim(); value = value.Trim().Replace("\n", String.Empty).Replace("\r", String.Empty); if (EntryExists(key)) cfgEntries[key] = value; else cfgEntries.Add(key, value); saveCfg(); } public static bool EntryExists(string key) { return cfgEntries.ContainsKey(key); } public static string GetEntry(string key) { return EntryExists(key) ? cfgEntries[key] : String.Empty; } public static void RemoveEntry(string key) { cfgEntries.Remove(key.Trim()); saveCfg(); } public static string GetEntry(string key, string defaultValue) { if (!EntryExists(key)) { SetEntry(key, defaultValue); return defaultValue; } return GetEntry(key); } } }