-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnda.tcl
executable file
·204 lines (176 loc) · 4.36 KB
/
nda.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# This whole didgeridoo is legacy code and I need to kill it with fire!
package require base64
proc ndaenc {n} {
return [string map {/ [} [::base64::encode [string tolower $n]]]
}
proc ndadec {n} {
return [::base64::decode [string map {[ /} $n]]
}
proc ndcenc {n} {
return [string map {/ [} [::base64::encode $n]]
}
proc ndcdec {n} {
return [::base64::decode [string map {[ /} $n]]
}
set nd [set tnd [list]]
namespace eval nda {
proc ::nda::get {path} {
#puts stdout "invoked with $path"
global nd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
::set pathe [lrange $parr 1 end]
if {[info exists nd] && ![catch {dict get $nd {*}$parr} eee]} {return $eee} {return ""}
}
proc ::nda::set {path val} {
#puts stdout "invoked with $path"
global nd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
return [dict set nd {*}$parr $val]
}
proc ::nda::unset {path} {
#puts stdout "invoked with $path"
global nd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
if {[info exists nd] && ![catch {dict unset nd {*}$parr} eee]} {return $eee} {return ""}
}
proc ::nda::incr {path} {
#puts stdout "invoked with $path"
global nd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
set orig [::nda::get $path]
if {[string is integer $orig]} {
::nda::set $path [expr $orig+$inc]
} {
::nda::set $path $inc
}
}
namespace export *
namespace ensemble create
}
# alt API:
namespace eval dbase {
proc ::dbase::get {args} {
global nd
if {[info exists nd] && ![catch {dict get $nd {*}$args} eee]} {return $eee} {return ""}
}
proc ::dbase::set {args} {
global nd
if {[lindex $args 1] == ""} {
return ""
}
return [dict set nd {*}$args]
}
proc ::dbase::lappend {args} {
global nd
if {[lindex $args 1] == ""} {
return ""
}
::set orig [::dbase::get {*}[lrange $args 0 end-1]]
::lappend orig [lindex $args end]
return [dict set nd {*}[lrange $args 0 end-1] $orig]
}
proc ::dbase::unset {args} {
global nd
return [dict unset nd {*}$args]
}
namespace export *
namespace ensemble create
}
namespace eval tnda {
proc ::tnda::get {path} {
#puts stdout "invoked with $path"
global tnd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
#::set pathe [lrange $parr 1 end]
if {[info exists tnd] && ![catch {dict get $tnd {*}$parr} eee]} {return $eee} {return ""}
}
proc ::tnda::set {path val} {
#puts stdout "invoked with $path"
global tnd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
#::set pathe [lrange $parr 1 end]
return [dict set tnd {*}$parr $val]
}
proc ::tnda::unset {path} {
#puts stdout "invoked with $path"
global tnd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
if {[info exists tnd] && ![catch {dict unset tnd {*}$parr} eee]} {return $eee} {return ""}
}
proc ::tnda::incr {path {inc 1}} {
#puts stdout "invoked with $path"
global tnd
::set parr [split $path "/"]
if {[lindex $parr 0] == ""} {
return ""
}
::set orig [::tnda::get $path]
if {[string is integer $orig]} {
::tnda::set $path [expr $orig+$inc]
} {
::tnda::set $path $inc
}
}
namespace export *
namespace ensemble create
}
namespace eval cdbase {
proc ::cdbase::get {args} {
global tnd
if {[info exists tnd] && ![catch {dict get $tnd {*}$args} eee]} {return $eee} {return ""}
}
proc ::cdbase::set {args} {
global tnd
if {[lindex $args 1] == ""} {
return ""
}
return [dict set tnd {*}$args]
}
proc ::cdbase::lappend {args} {
global tnd
if {[lindex $args 1] == ""} {
return ""
}
::set orig [::cdbase::get {*}[lrange $args 0 end-1]]
::lappend orig [lindex $args end]
return [dict set tnd {*}[lrange $args 0 end-1] $orig]
}
proc ::cdbase::unset {args} {
global tnd
return [dict unset tnd {*}$args]
}
namespace export *
namespace ensemble create
}
proc tdb {args} {set l [list cdbase]; foreach {i} $args {lappend l $i}; $l}
proc gettext {stringname args} {
gettext.i18n $stringname en $args
}
proc gettext.i18n {stringname language arg} {
if {"" == [set out [format [dict get $::gettext [format "%s.%s" $language $stringname]] {*}$arg]]} {
# default to the English locale if we don't know
set out [format [dict get $::gettext [format "%s.%s" en $stringname]] {*}$arg]
}
return $out
}