File: datatype.tcl

package info (click to toggle)
tcllib 1.19-dfsg-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 67,328 kB
  • sloc: tcl: 208,371; ansic: 14,215; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 583; makefile: 106; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (434 lines) | stat: -rw-r--r-- 10,317 bytes parent folder | download
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
###
# Tool Datatypes
###

::tool::define ::tool::datatype::string {
  method value::get value {
    return $value
  }

  method value::put value {
    return $value
  }
}

::tool::define ::tool::datatype::boolean {
  superclass ::tool::datatype::string

  ###
  # topic: efc05671edb5dba64be56c1c3d102e6f9cdf3287
  ###
  method value::get rawvalue {
    return [string is true -strict $rawvalue]
  }

  ###
  # topic: 1c75cc4d57d468fc594ecad49fc20a954158c18f
  ###
  method value::put rawvalue {
    return [string is true -strict $rawvalue]
  }
}

::tool::define ::tool::datatype::integer {
  superclass ::tool::datatype::string

  option format {default %d}
  
  ###
  # topic: efc05671edb5dba64be56c1c3d102e6f9cdf3287
  ###
  method value::get rawvalue {
    if { $rawvalue eq {} } {
      return {}
    }
    set format [my cget format]
    set c [scan $rawvalue $format newvalue]
    if {![info exists newvalue]} {
      error "Bad value $rawvalue"
    }
    return $newvalue
  }

  ###
  # topic: 1c75cc4d57d468fc594ecad49fc20a954158c18f
  ###
  method value::put rawvalue {
    if { $rawvalue eq {} } {
      return {}
    }
    set format [my cget format]
    set c [scan $rawvalue $format newvalue]
    if {![info exists newvalue]} {
      error "Bad value $rawvalue"
    }
    return $newvalue
  }
}

::tool::define ::tool::datatype::real {
  superclass ::tool::datatype::string

  option format {default %g}
  
  ###
  # topic: c32d30915be323dca1264c441d06a16ea6b62b68
  ###
  method value::get rawvalue {
    if { $rawvalue eq {} } {
      return {}
    }
    set format [my cget format]
    set c [scan $rawvalue $format newvalue]
    if {![info exists newvalue]} {
      error "Bad value $rawvalue"
    }
    return $newvalue
  }

  ###
  # topic: 27f11c0626223864f2602553471cc11d563b2eec
  ###
  method value::put rawvalue {
    if { $rawvalue eq {} } {
      return {}
    }
    set format [my cget format]
    set c [scan $rawvalue $format newvalue]
    if {![info exists newvalue]} {
      error "Bad value $rawvalue"
    }
    return $newvalue
  }
}

::tool::define ::tool::datatype::unixtime {
  superclass ::tool::datatype::string

  option gmt {widget boolean default 0}

  ###
  # topic: ba50b03e339a6f09b429871fc6a2ba3c54516aa2
  # title: Convert an internally encoded value to its externally encoded value
  # description:
  #    Used for widgets that display human-readable values. For example
  #    converting the human readable [emph {c131 - 01-141-L - Passage}]
  #    into an integer ([emph 131]) for encoding in a database field.
  ###
  method value::get value {
    if { $value eq {} } {
      return {}
    }
    set outtime [clock scan $value -gmt [my cget gmt]]
    return $outtime
  }

  ###
  # topic: ef8b8c1dbd3388c95a14de3e551e7c4a43ba7bbf
  # title: Convert an externally encoded value to its internally encoded value
  # description:
  #    Used for widgets that display human-readable values. For example
  #    converting an integer in the database ([emph 131]) to something
  #    mor intelligable to the user [emph {c131 - 01-141-L - Passage}]
  ###
  method value::put value {
    if { $value eq {} } {
      return {}
    }
    set realvalue $value
    return [clock format $realvalue -gmt [my cget gmt]]
  }
}

::tool::define ::tool::datatype::datetime {
  superclass ::tool::datatype::string

  option display_format {default {}}  
  option output_format  {default {}}
  option gmt {widget boolean default 0}

  ###
  # topic: 1e0804037f9558efac701766446ed26bb67b318f
  # title: Convert an internally encoded value to its externally encoded value
  # description:
  #    Used for widgets that display human-readable values. For example
  #    converting the human readable [emph {c131 - 01-141-L - Passage}]
  #    into an integer ([emph 131]) for encoding in a database field.
  ###
  method value::get value {
    if { $value eq {} } {
      return {}
    }
    set format [my cget display_format]
    if { $format ni { {} "unixtime" } } {
      set outtime [clock scan $value -format $format -gmt [my cget gmt]]
    } else {
      set outtime [clock scan $value]
    }
    set format [my cget output_format]
    if { $format ni { {} "unixtime" } } {
      return [clock format $outtime -format $format -gmt [my cget gmt]]
    } else {
      return $outtime
    }
  }

  ###
  # topic: fee644b1fa3dc4796d55a0429e0418171854b4d4
  # title: Convert an externally encoded value to its internally encoded value
  # description:
  #    Used for widgets that display human-readable values. For example
  #    converting an integer in the database ([emph 131]) to something
  #    mor intelligable to the user [emph {c131 - 01-141-L - Passage}]
  ###
  method value::put value {
    if { $value eq {} } {
      return {}
    }
    set format [my cget output_format]
    if { $format ni { {} "unixtime" } } {
      set realvalue [clock scan $value -format $format -gmt [my cget gmt]]
    } else {
      set realvalue $value
    }
    set format [my cget display_format]
    if { $format ni { {} "unixtime" } } {
      return [clock format $realvalue -format $format -gmt [my cget gmt]]
    } else {
      return [clock format $realvalue -gmt [my cget gmt]]
    }
  }
}

::tool::define ::tool::datatype::select {
  superclass ::tool::datatype::string

  option values {}
  
  option state {
    widget select
    values {normal readonly disabled}
    default readonly
  }

  ###
  # topic: 3339ac6fe57b0b23add1e8fb64336c567a7e3694
  ###
  method CalculateValues {} {
    return [my GetConfigValueList]
  }

  ###
  # topic: 6ee18ff095fc0ff746b0dcf0876daa6150adf42c
  ###
  method CalculateValueWidth values {
    set w 0
    set n 0
    foreach v $values {
      incr n
      set l [string length $v]
      incr bins($l)
      if {$l > $w} {
        set w $l
      }
    }
    if { $w > 30} {
      set w 30
    }
    return $w
  }

  ###
  # topic: 14e9b60d5636bca086ca44a90cf15dbefeaa1340
  ###
  method GetConfigValueList {} {
    my variable config
    set values {}
    if {[dict exists $config options_command]} {
      return [eval [dict get $config options_command]]
    }
    if {[dict exists $config values]} {
      return [dict get $config values]
    }
    if {[dict exists $config options]} {
      return [dict get $config options]
    }
    return {}
  }
}

::tool::define ::tool::datatype::select_keyvalue {
  superclass ::tool::datatype::select

  option state {
    widget select
    values {normal readonly disabled}
    default readonly
  }
  option accept_number {
    widget boolean
    default 1
  }

  ###
  # topic: 77bd2ab8551c40ecee13ffc38d6f9af819680de9
  ###
  method CalculateValues {} {
    set values [my GetConfigValueList]
    foreach {key value} $values {
      lappend result $key
    }
    return $result
  }

  ###
  # topic: 3e8bc6c0b4bdafca5b43b927e2c874432239fc4f
  ###
  method value::get rawvalue {
    set values [my GetConfigValueList]
    foreach {var val} $values {
      if {$rawvalue eq $val} {
        return $val
      }
      if {$rawvalue eq $var} {
        return $val
      }
    }
    return $rawvalue
  }

  ###
  # topic: 6a6e2567e1fa5cb769dc67cc402ff2a08cb55ecd
  ###
  method value::put rawvalue {
    set values [my GetConfigValueList]
    foreach {var val} $values {
      if {$rawvalue eq $val} {
        return $var
      }
      if {$rawvalue eq $var} {
        return $var
      }
    }
    if {[my cget accept_number]} {
      if {[string is double $rawvalue]} {
        return $rawvalue
      }
    }
    error "Invalid Value \"$rawvalue\". Valid: [join [dict keys $values] ,]"
  }
}

::tool::define ::tool::datatype::enumerated {
  superclass ::tool::datatype::select

  option state {
    widget select
    values {normal readonly disabled}
    default readonly
  }
  option enum {
    default {}
  }

  ###
  # topic: 5fa7461dcfec89fd52456af62f04aa28696d5974
  ###
  method CalculateValues {} {
    set values {}
    foreach {id code comment} [my GetConfigValueList] {
      lappend values "$id - $code"
    }
    return $values
  }

  ###
  # topic: b47b26deb7db53bbce74a78401302d103d805748
  ###
  method value::get value {
    set value [lindex $value 0]
    foreach {id code comment} [my GetConfigValueList] {
      if {$value == $id } {
        return $id
      }
    }
    return {}
  }

  ###
  # topic: 90776f5900f1bc28fbdf47c895e2d052e3fbaafd
  ###
  method value::put value {
    foreach {id code comment} [my GetConfigValueList] {
      if { [lindex $value 0] == $id } {
        return "$id - $code - $comment"
      }
    }
    return {}
  }
}

::tool::define ::tool::datatype::vector {
  superclass ::tool::datatype::string
  
  property vector_fields {
    x {format {%0.6g} widget entry width 10}
    y {format {%0.6g} widget entry width 10}
    z {format {%0.6g} widget entry width 10}
  }

  method Vector_Fields {} {
    return [my property vector_fields]
  }
  
  ###
  # topic: 6d9aec52f3c16e5248eee30bfacb9045917273aa
  ###
  method value::get newvalue {
    set result {}
    array set content $newvalue
    foreach {vfield info} [my Vector_Fields]  {
      set format [if_null [dict getnull $info format] %s]
      set newvalue [format $format $content($vfield)]
      lappend result $newvalue
    }
    return $result
  }

  ###
  # topic: 96480da6e97f6cfe3574475a7c3b132b39a9003c
  ###
  method value::put inputvalue {
    set idx -1
    foreach {vfield info} [my Vector_Fields] {
      incr idx
      set format [if_null [dict getnull $info format] %s]
      set value [lindex $inputvalue $idx]
      if {[dict exists $info default]} {
        if {$value eq {}} {
          set value [dict get $info default]
        }
      }
      if {$value eq {}} {
        set local_array($vfield) $value
      } elseif { $format in {"%d" int integer} } {
        if [catch {expr {int($value)}} nvalue] {
          puts "Err: $format $vfield. Raw: $value. Err: $nvalue"
          dict set result $vfield $value
        } else {
          dict set result $vfield $nvalue
        }
      } else {
        if [catch {format $format $value} nvalue] {
          puts "Err: $vfield. Raw: $value. Err: $nvalue"
          dict set result $vfield $value
        } else {
          dict set result $vfield $nvalue
        }
      }
    }
    return $result
  }
}

package provide tool::datatype 0.1