config.vala
changeset 1 76caf6a3f413
child 6 0d97b0d61ed2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.vala	Fri Oct 19 00:38:54 2012 +0400
@@ -0,0 +1,49 @@
+class Config : Object {
+    protected Gee.HashMap<string,string> hash;
+    protected string rkey(string section, string key) {
+        return section+"\n"+key;
+    }
+    public Config.from_file(string filename) {
+        base();
+        hash = new Gee.HashMap<string,string>();
+        IOChannel cfg;
+        /* try { */
+            cfg = new IOChannel.file(filename,"r");
+        /*} catch (FileError e) {
+            File.new_for_path(filename);*/
+        string section = "default";
+        do {
+            string str;
+            size_t length, termpos;
+            IOStatus stat = cfg.read_line(out str,out length,out termpos);
+            if (stat == IOStatus.NORMAL) {
+                str = str[0:(long)termpos];
+                if ( str.has_prefix("[") && str.has_suffix("]") ) {
+                    section = str[1:-1];
+                    stdout.printf("Section '%s'\n",section);
+                } else {
+                    string[] parts = str.split("=",2);
+                    if (parts.length==2) {
+                        var nkey = rkey(section,parts[0]);
+                        hash[nkey] = parts[1];
+                        stdout.printf("LOL: key: %s value: %s\n",nkey,parts[1]);
+                    }
+                }
+            } else
+                break;
+        } while (true);
+    }
+    public string get(string section, string key) {
+        return hash[rkey(section,key)];
+    }
+    public string get_default(string section, string key, string def) {
+        var nkey = rkey(section,key);
+        if (hash.has_key(nkey))
+            return hash[nkey];
+        return def;
+    }
+    public bool has_key(string section, string key) {
+        return hash.has_key(rkey(section,key));
+    }
+}
+