-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.tcl
74 lines (55 loc) · 1.59 KB
/
config.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package require inifile
namespace eval config {
variable Default
variable Config
variable UserConfigFile
}
proc config::change_user_config {conf_file} {
variable UserConfigFile
set UserConfigFile $conf_file
}
proc config::default {} {
global RootDir
variable Default
set conf_file [file join [file dirname [info script]] "default.conf"]
if { ![file exist $conf_file]} {
::error::error "$conf_file does not exist."
return
}
set handle [::ini::open $conf_file r]
foreach section [::ini::sections $handle] {
foreach key [::ini::keys $handle $section] {
set Default($key) [::ini::value $handle $section $key]
}
}
::ini::close $handle
}
proc config::load {} {
variable Config
variable Default
variable UserConfigFile
if { ![file exist $UserConfigFile]} {
return
}
set handle [::ini::open $UserConfigFile]
array set Config [array get Default]
foreach section [::ini::sections $handle] {
foreach key [::ini::keys $handle $section] {
set Config($key) [::ini::value $handle $section $key]
}
}
::ini::close $handle
}
proc config::save {args} {
variable Config
variable UserConfigFile
if { ![file exist $UserConfigFile]} {
catch {close [open $UserConfigFile w]}
}
set handle [::ini::open $UserConfigFile]
foreach key [array names Config] {
::ini::set $handle "Main" $key $Config($key)
}
catch {::ini::commit $handle}
catch {::ini::close $handle}
}