author | Stiletto <blasux@blasux.ru> |
Mon, 05 Nov 2012 23:54:44 +0400 | |
changeset 12 | d3e36b368fc5 |
parent 6 | 0d97b0d61ed2 |
permissions | -rw-r--r-- |
1
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
1 |
class Config : Object { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
2 |
protected Gee.HashMap<string,string> hash; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
3 |
protected string rkey(string section, string key) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
4 |
return section+"\n"+key; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
5 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
6 |
public Config.from_file(string filename) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
7 |
base(); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
8 |
hash = new Gee.HashMap<string,string>(); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
9 |
IOChannel cfg; |
6
0d97b0d61ed2
Log error if failed to load config file
Stiletto <blasux@blasux.ru>
parents:
1
diff
changeset
|
10 |
try { |
1
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
11 |
cfg = new IOChannel.file(filename,"r"); |
6
0d97b0d61ed2
Log error if failed to load config file
Stiletto <blasux@blasux.ru>
parents:
1
diff
changeset
|
12 |
} catch (FileError e) { |
0d97b0d61ed2
Log error if failed to load config file
Stiletto <blasux@blasux.ru>
parents:
1
diff
changeset
|
13 |
log("config", LogLevelFlags.LEVEL_ERROR, "Failed to load config file '%s'", filename); |
0d97b0d61ed2
Log error if failed to load config file
Stiletto <blasux@blasux.ru>
parents:
1
diff
changeset
|
14 |
return; |
0d97b0d61ed2
Log error if failed to load config file
Stiletto <blasux@blasux.ru>
parents:
1
diff
changeset
|
15 |
} |
1
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
16 |
string section = "default"; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
17 |
do { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
18 |
string str; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
19 |
size_t length, termpos; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
20 |
IOStatus stat = cfg.read_line(out str,out length,out termpos); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
21 |
if (stat == IOStatus.NORMAL) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
22 |
str = str[0:(long)termpos]; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
23 |
if ( str.has_prefix("[") && str.has_suffix("]") ) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
24 |
section = str[1:-1]; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
25 |
} else { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
26 |
string[] parts = str.split("=",2); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
27 |
if (parts.length==2) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
28 |
var nkey = rkey(section,parts[0]); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
29 |
hash[nkey] = parts[1]; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
30 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
31 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
32 |
} else |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
33 |
break; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
34 |
} while (true); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
35 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
36 |
public string get(string section, string key) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
37 |
return hash[rkey(section,key)]; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
38 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
39 |
public string get_default(string section, string key, string def) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
40 |
var nkey = rkey(section,key); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
41 |
if (hash.has_key(nkey)) |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
42 |
return hash[nkey]; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
43 |
return def; |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
44 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
45 |
public bool has_key(string section, string key) { |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
46 |
return hash.has_key(rkey(section,key)); |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
47 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
48 |
} |
76caf6a3f413
Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
49 |