config.vala
author Stiletto <blasux@blasux.ru>
Fri, 19 Oct 2012 00:38:54 +0400
changeset 1 76caf6a3f413
child 6 0d97b0d61ed2
permissions -rw-r--r--
Somehow compiles and even joins a conference

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));
    }
}