File: misc.tcl

package info (click to toggle)
moodss 19.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,136 kB
  • ctags: 3,149
  • sloc: tcl: 49,048; ansic: 187; perl: 178; makefile: 166; sh: 109; python: 65
file content (54 lines) | stat: -rw-r--r-- 2,084 bytes parent folder | download | duplicates (2)
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
# copyright (C) 1997-2005 Jean-Luc Fontaine (mailto:jfontain@free.fr)
# this program is free software: please read the COPYRIGHT file enclosed in this package or use the Help Copyright menu

# $Id: misc.tcl,v 1.13 2005/01/02 00:45:07 jfontain Exp $


package provide miscellaneous [lindex {$Revision: 1.13 $} 1]


proc minimum {a b} {return [expr {$a < $b? $a: $b}]}
proc maximum {a b} {return [expr {$a > $b? $a: $b}]}

proc ldelete {listName value} {
    upvar 1 $listName list

    set index [lsearch -exact $list $value]
    if {$index < 0} {
        error "\"$value\" is not in list"
    }
    set list [lreplace $list $index $index]
}

proc static {localName args} {
    set global [uplevel 1 namespace which -command [lindex [info level -1] 0]]:$localName
    uplevel 1 upvar #0 $global $localName
    if {![info exists $global]} {
        switch [llength $args] {
            0 return                                                                                  ;# no initialization requested
            1 {set $global [lindex $args 0]}
            default {error {usage: static name ?value?}}
        }
    }                                                                                                    ;# else already initialized
}

proc formattedTime {seconds} {
    set string {}
    set interval [expr {$seconds / 86400}]                                                                                   ;# days
    if {$interval > 0} {
        append string ${interval}d
        set seconds [expr {$seconds % 86400}]
    }
    set interval [expr {$seconds / 3600}]                                                                                   ;# hours
    if {$interval > 0} {
        append string ${interval}h
        set seconds [expr {$seconds % 3600}]
    }
    set interval [expr {$seconds / 60}]                                                                                   ;# minutes
    if {$interval > 0} {
        append string ${interval}m
        set seconds [expr {$seconds % 60}]
    }
    append string ${seconds}s
    return $string
}