namespace bytex64.WebThing {
public class Config {
public static List<string> ConfigPath;
+ public static string ConfigPathOut = null;
public static Dictionary<string,string> Options;
public static Dictionary<string,Dictionary<string,string>> PluginOptions;
ConfigPath = new List<string>();
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;
}
}
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;
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();
+ }
+
+ // Data parsers
+ public static bool ParseBool(string v) {
+ switch(v.ToLower()) {
+ case "true":
+ case "t":
+ case "1":
+ case "on":
+ case "yes":
+ return true;
+ }
+ return false;
+ }
}
}