|
1 class Config : Object { |
|
2 protected Gee.HashMap<string,string> hash; |
|
3 protected string rkey(string section, string key) { |
|
4 return section+"\n"+key; |
|
5 } |
|
6 public Config.from_file(string filename) { |
|
7 base(); |
|
8 hash = new Gee.HashMap<string,string>(); |
|
9 IOChannel cfg; |
|
10 /* try { */ |
|
11 cfg = new IOChannel.file(filename,"r"); |
|
12 /*} catch (FileError e) { |
|
13 File.new_for_path(filename);*/ |
|
14 string section = "default"; |
|
15 do { |
|
16 string str; |
|
17 size_t length, termpos; |
|
18 IOStatus stat = cfg.read_line(out str,out length,out termpos); |
|
19 if (stat == IOStatus.NORMAL) { |
|
20 str = str[0:(long)termpos]; |
|
21 if ( str.has_prefix("[") && str.has_suffix("]") ) { |
|
22 section = str[1:-1]; |
|
23 stdout.printf("Section '%s'\n",section); |
|
24 } else { |
|
25 string[] parts = str.split("=",2); |
|
26 if (parts.length==2) { |
|
27 var nkey = rkey(section,parts[0]); |
|
28 hash[nkey] = parts[1]; |
|
29 stdout.printf("LOL: key: %s value: %s\n",nkey,parts[1]); |
|
30 } |
|
31 } |
|
32 } else |
|
33 break; |
|
34 } while (true); |
|
35 } |
|
36 public string get(string section, string key) { |
|
37 return hash[rkey(section,key)]; |
|
38 } |
|
39 public string get_default(string section, string key, string def) { |
|
40 var nkey = rkey(section,key); |
|
41 if (hash.has_key(nkey)) |
|
42 return hash[nkey]; |
|
43 return def; |
|
44 } |
|
45 public bool has_key(string section, string key) { |
|
46 return hash.has_key(rkey(section,key)); |
|
47 } |
|
48 } |
|
49 |