Changeset 1342 for trunk/gui/scripts/tuples.tcl
- Timestamp:
- Mar 18, 2009, 2:59:21 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/tuples.tcl
r115 r1342 34 34 private common _counter 0 ;# for auto-generated column names 35 35 } 36 36 37 37 # ---------------------------------------------------------------------- 38 38 # CONSTRUCTOR … … 57 57 itcl::body Rappture::Tuples::column {option args} { 58 58 switch -- $option { 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 59 insert { 60 # parse the incoming args 61 if {[llength $args] < 1} { 62 error "wrong # args: should be \"column insert pos ?-name n? ?-label l? ?-default v?\"" 63 } 64 set pos [lindex $args 0] 65 set args [lrange $args 1 end] 66 Rappture::getopts args params { 67 value -name #auto 68 value -label "" 69 value -default "" 70 } 71 if {[llength $args] != 0} { 72 error "wrong # args: should be \"column insert pos ?-name n? ?-label l? ?-default v?\"" 73 } 74 75 # insert the new column 76 set cname $params(-name) 77 if {$params(-name) == "#auto"} { 78 set cname "column[incr _counter]" 79 } 80 if {[lsearch -exact $_colnames $cname] >= 0} { 81 error "column name \"$cname\" already exists" 82 } 83 set _colnames [linsert $_colnames $pos $cname] 84 set _col2info($cname-label) $params(-label) 85 set _col2info($cname-default) $params(-default) 86 87 # run through all existing tuples and insert the default val 88 set max [array size _tuples] 89 for {set i 0} {$i < $max} {incr i} { 90 set oldval $_tuples($i) 91 set _tuples($i) [linsert $oldval $pos $params(-default)] 92 } 93 } 94 delete { 95 foreach cname $args { 96 set pos [lsearch -exact $_colnames $cname] 97 if {$pos < 0} { 98 error "bad column name \"$cname\"" 99 } 100 set _colnames [lreplace $_colnames $pos $pos] 101 unset _col2info($cname-label) 102 unset _col2info($cname-default) 103 104 # run through all existing tuples and delete the column 105 set max [array size _tuples] 106 for {set i 0} {$i < $max} {incr i} { 107 set oldval $_tuples($i) 108 set _tuples($i) [lreplace $oldval $pos $pos] 109 } 110 } 111 } 112 names { 113 if {[llength $args] == 0} { 114 return $_colnames 115 } elseif {[llength $args] == 1} { 116 set pattern [lindex $args 0] 117 set rlist "" 118 foreach cname $_colnames { 119 if {[string match $pattern $cname]} { 120 lappend rlist $cname 121 } 122 } 123 return $rlist 124 } else { 125 error "wrong # args: should be \"column names ?pattern?\"" 126 } 127 } 128 info { 129 if {[llength $args] != 1} { 130 error "wrong # args: should be \"column info name\"" 131 } 132 set cname [lindex $args 0] 133 set pos [lsearch -exact $_colnames $cname] 134 if {$pos < 0} { 135 error "bad column name \"$cname\"" 136 } 137 return [list -label $_col2info($cname-label) -default $_col2info($cname-default)] 138 } 139 default { 140 error "bad option \"$option\": should be delete, info, insert, names" 141 } 142 142 } 143 143 } … … 155 155 156 156 if {"end" == $pos} { 157 157 set pos $max 158 158 } elseif {![string is integer $pos]} { 159 159 error "bad position \"$pos\": should be integer or \"end\"" 160 160 } elseif {$pos < 0} { 161 161 set pos 0 162 162 } elseif {$pos > $max} { 163 163 set pos $max 164 164 } 165 165 … … 167 167 set need [llength $args] 168 168 for {set i [expr {$max-1}]} {$i >= $pos} {incr i -1} { 169 169 set _tuples([expr {$i+$need}]) $_tuples($i) 170 170 } 171 171 172 172 # add the tuples at the specified pos 173 173 foreach t $args { 174 175 176 177 178 179 174 # make sure each tuple has enough columns 175 while {[llength $t] < $cols} { 176 lappend t "" 177 } 178 set _tuples($pos) $t 179 incr pos 180 180 } 181 181 } … … 191 191 itcl::body Rappture::Tuples::delete {{from ""} {to ""}} { 192 192 if {"" == $from && "" == $to} { 193 194 193 catch {unset _tuples} 194 return 195 195 } 196 196 if {[array size _tuples] == 0} { 197 197 return ;# nothing to delete 198 198 } 199 199 … … 204 204 set gap [expr {$to-$from+1}] 205 205 for {set i $from} {$i <= $last-$gap} {incr i} { 206 206 set _tuples($i) $_tuples([expr {$i+$gap}]) 207 207 } 208 208 for {} {$i <= $last} {incr i} { 209 209 unset _tuples($i) 210 210 } 211 211 } … … 223 223 itcl::body Rappture::Tuples::put {args} { 224 224 Rappture::getopts args params { 225 225 value -format "" 226 226 } 227 227 if {[llength $args] != 2} { 228 228 error "wrong # args: should be \"put ?-format cols? pos tuple\"" 229 229 } 230 230 foreach {pos tuple} $args break … … 232 232 233 233 if {![info exists _tuples($pos)]} { 234 234 error "index $pos doesn't exist" 235 235 } 236 236 237 237 if {[string length $params(-format)] == 0} { 238 239 240 241 242 243 238 # no format -- add tuple as-is (with proper number of columns) 239 set cols [llength $_colnames] 240 while {[llength $tuple] < $cols} { 241 lappend tuple "" 242 } 243 set _tuples($pos) $tuple 244 244 } else { 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 245 # convert column names to indices 246 set nlist "" 247 foreach cname $params(-format) { 248 set n [lsearch -exact $_colnames $cname] 249 if {$n < 0} { 250 error "bad column name \"$cname\"" 251 } 252 lappend nlist $n 253 } 254 255 # convert data only for those indices 256 set val $_tuples($pos) 257 foreach n $nlist t $tuple { 258 set val [lreplace $val $n $n $t] 259 } 260 set _tuples($pos) $val 261 261 } 262 262 } … … 271 271 itcl::body Rappture::Tuples::get {args} { 272 272 Rappture::getopts args params { 273 273 value -format "" 274 274 } 275 275 if {[llength $args] > 2} { 276 276 error "wrong # args: should be \"get ?-format cols? ?from? ?to?\"" 277 277 } 278 278 set from "" … … 283 283 # empty? then return nothing 284 284 if {[array size _tuples] == 0} { 285 285 return "" 286 286 } 287 287 288 288 set rlist "" 289 289 if {[string length $params(-format)] == 0} { 290 291 292 293 290 # no format string -- return everything as-is 291 for {set i $from} {$i <= $to} {incr i} { 292 lappend rlist $_tuples($i) 293 } 294 294 } else { 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 295 # convert column names to indices 296 set nlist "" 297 foreach cname $params(-format) { 298 set n [lsearch -exact $_colnames $cname] 299 if {$n < 0} { 300 error "bad column name \"$cname\"" 301 } 302 lappend nlist $n 303 } 304 set single [expr {[llength $nlist] == 1}] 305 306 # convert data only for those indices 307 for {set i $from} {$i <= $to} {incr i} { 308 set t "" 309 foreach n $nlist { 310 if {$single} { 311 set t [lindex $_tuples($i) $n] 312 } else { 313 lappend t [lindex $_tuples($i) $n] 314 } 315 } 316 lappend rlist $t 317 } 318 318 } 319 319 return $rlist … … 332 332 itcl::body Rappture::Tuples::find {args} { 333 333 Rappture::getopts args params { 334 334 value -format "" 335 335 } 336 336 if {[llength $args] > 1} { 337 337 error "wrong # args: should be \"find ?-format cols? ?tuple?\"" 338 338 } 339 339 … … 341 341 set nlist "" 342 342 foreach cname $params(-format) { 343 344 345 346 347 343 set n [lsearch -exact $_colnames $cname] 344 if {$n < 0} { 345 error "bad column name \"$cname\"" 346 } 347 lappend nlist $n 348 348 } 349 349 … … 352 352 set last [expr {[array size _tuples]-1}] 353 353 if {[llength $args] == 0} { 354 355 356 357 354 # no tuple? then all match 355 for {set i 0} {$i <= $last} {incr i} { 356 lappend rlist $i 357 } 358 358 } else { 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 359 set tuple [lindex $args 0] 360 if {[llength $nlist] == 0} { 361 # no format? then look for an exact match 362 for {set i 0} {$i <= $last} {incr i} { 363 if {[string equal $tuple $_tuples($i)]} { 364 lappend rlist $i 365 } 366 } 367 } else { 368 # match only the columns in the -format 369 for {set i 0} {$i <= $last} {incr i} { 370 set matching 1 371 foreach n $nlist t $tuple { 372 set val [lindex $_tuples($i) $n] 373 if {![string equal $t $val]} { 374 set matching 0 375 break 376 } 377 } 378 if {$matching} { 379 lappend rlist $i 380 } 381 } 382 } 383 383 } 384 384 return $rlist … … 406 406 set last [expr {[array size _tuples]-1}] 407 407 if {"" == $from && "" == $to} { 408 408 return [list 0 $last] 409 409 } 410 410 411 411 if {"end" == $from} { 412 412 set from $last 413 413 } elseif {![string is integer $from]} { 414 414 error "bad position \"$from\": should be integer or \"end\"" 415 415 } 416 416 if {$from < 0} { 417 417 set from 0 418 418 } elseif {$from > $last} { 419 419 set from $last 420 420 } 421 421 422 422 if {"" == $to} { 423 423 set to $from 424 424 } elseif {"end" == $to} { 425 425 set to $last 426 426 } elseif {![string is integer $to]} { 427 427 error "bad position \"$to\": should be integer or \"end\"" 428 428 } 429 429 if {$to < 0} { 430 430 set to 0 431 431 } elseif {$to > $last} { 432 432 set to $last 433 433 } 434 434 435 435 if {$from > $to} { 436 437 438 439 436 # make sure to/from are in proper order 437 set tmp $from 438 set from $to 439 set to $tmp 440 440 } 441 441 return [list $from $to]
Note: See TracChangeset
for help on using the changeset viewer.