X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;ds=sidebyside;f=Config.cs;h=ac70dc860902716f6b1dcccdc94e65bdb63f8a7d;hb=bf0210888b2c1a7eb26656fdcaec4e0559569772;hp=49e7203cfaaf38bfccbc59b69a5d23701a2375ab;hpb=1b1f91b709e01680efda771cd905e460c08fb19a;p=WebThing.git diff --git a/Config.cs b/Config.cs index 49e7203..ac70dc8 100644 --- a/Config.cs +++ b/Config.cs @@ -7,6 +7,7 @@ using System.Text.RegularExpressions; namespace bytex64.WebThing { public class Config { public static List ConfigPath; + public static string ConfigPathOut = null; public static Dictionary Options; public static Dictionary> PluginOptions; @@ -29,12 +30,17 @@ namespace bytex64.WebThing { ConfigPath = new List(); ConfigPath.Add("/etc/WebThing"); if (Env.Contains("HOME")) { - ConfigPath.Add(Env["HOME"] + "/.config/WebThing"); - ConfigPath.Add(Env["HOME"] + "/.WebThing"); + string homepath = Env["HOME"] + "/.config/WebThing"; + ConfigPath.Add(homepath); + if (Directory.Exists(homepath)) + ConfigPathOut = homepath; } string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); if (LocalAppData != "") { - ConfigPath.Add(LocalAppData + "/WebThing"); + string homepath = LocalAppData + "/WebThing"; + ConfigPath.Add(homepath); + if (Directory.Exists(homepath)) + ConfigPathOut = homepath; } } @@ -142,7 +148,7 @@ namespace bytex64.WebThing { public static void LoadFile(string filename) { TextReader f; try { - f = new StreamReader(File.OpenRead(filename)); + f = new StreamReader(filename); } catch (FileNotFoundException) { Console.WriteLine("Could not open configuration file {0}", filename); return; @@ -179,5 +185,46 @@ namespace bytex64.WebThing { f.Close(); } + + public static void SaveFile(string filename) { + TextWriter f; + try { + f = new StreamWriter(filename); + } catch (IOException) { + Console.WriteLine("Could not save configuration to {0}", filename); + return; + } + + foreach (string key in Options.Keys) { + if (CommandLineOptions.Contains(key)) continue; + f.WriteLine("{0} = {1}", key, Options[key]); + } + + foreach (string plugin in PluginOptions.Keys) { + foreach (string key in PluginOptions[plugin].Keys) { + string pkey = String.Format("{0}.{1}", plugin, key); + if (CommandLineOptions.Contains(pkey)) continue; + f.WriteLine("{0} = {1}", pkey, PluginOptions[plugin][key]); + } + } + + f.Close(); + } + + public static void SaveFile(WebThingPlugin p, string filename) { + string plugin_name = p.GetType().FullName; + SaveFile(plugin_name, filename); + } + + public static void SaveFile(string plugin_name, string filename) { + // TODO: Throw exception if plugin does not exist + TextWriter f = new StreamWriter(filename); + + foreach (string key in PluginOptions[plugin_name].Keys) { + f.WriteLine("{0}.{1} = {2}", plugin_name, key, PluginOptions[plugin_name][key]); + } + + f.Close(); + } } }