# Copyright (c) 2001 Josha Foust (tivoweb@lightn.org)
proc strim {str} {
regsub -all \}$|^\{ $str "" str
return $str
}
proc defaultval {val1 val2} {
if { $val2 != "" } {
return $val2
} else {
return $val1
}
}
proc nth {number} {
switch -glob $number {
1? {return ${number}th}
*1 {return ${number}st}
*2 {return ${number}nd}
*3 {return ${number}rd}
default { return ${number}th}
}
}
proc nth_sup {number} {
switch -glob $number {
1? {return ${number}th}
{ 1} {return " ${number}st"}
{ 2} {return " [string trim $number]nd"}
{ 3} {return " ${number}rd"}
{ ?} {return " ${number}th"}
*1 {return ${number}st}
*2 {return ${number}nd}
*3 {return ${number}rd}
default { return ${number}th}
}
}
proc unknown {cmd args} {
set cmd0 [lindex $cmd 0]
if {[llength [set cmds [info commands $cmd0]]]==1} {
return [uplevel "$cmds [string range $cmd [string length $cmd0] end] $args"]
}
return -code error "invalid command name \"$cmd\""
}
proc get_fsidbyprefix {dirname prefix} {
if { [catch {mfs scan $dirname -start $prefix -count 1} batch] } {
global errorCode errorInfo
if { $errorCode == "errNmNameNotFound" } {
return
} else {
error $batch $errorInfo $errorCode
}
}
set item [lindex $batch 0]
set name [lindex $item 1]
if { [PrefixMatches $prefix $name] } {
return $item
} else {
return ""
}
}
proc lsearchall {llist pattern} {
set rlist ""
set index [lsearch $llist $pattern]
set index1 $index
while {$index1 != -1} {
lappend rlist $index
set index1 [lsearch [lrange $llist [expr $index + 1] end] $pattern]
set index [expr $index + $index1 + 1]
}
if {$rlist == ""} {
return -1
} else {
return $rlist
}
}
# Original ForeachMfsFile borrowed from TiVo's mfslib.tcl
proc ForeachMfsFileTrans { idVar nameVar typeVar dirName prefix count body } {
global errorInfo errorCode
upvar $idVar id
upvar $nameVar name
upvar $typeVar type
# DaveA 8/11/2005: don't throw an error if the directory is missing
if {![MfsFileExists $dirName]} { return }
# Get the first batch of names
RetryTransaction {
if { [catch {mfs scan $dirName -start $prefix -count $count} batch] } {
global errorCode errorInfo
if { $errorCode == "errNmNameNotFound" } {
return
} else {
error $batch $errorInfo $errorCode
}
}
}
set done 0
while { [llength $batch] > 0 } {
# Execute the body for each item in this batch
RetryTransaction {
foreach item $batch {
set id [lindex $item 0]
set name [lindex $item 1]
set type [lindex $item 2]
# bail if we're past the entries that start with the given prefix
if { $prefix != "" && ! [PrefixMatches $prefix $name] } {
set done 1
break
}
set code [catch {uplevel $body} string]
if { $code == 1 } {
global errorCode errorInfo
if { $errorCode == "errTmActiveLockConflict" ||
$errorCode == "errTmBackgroundHoldoff" ||
$errorCode == "errFsLockConflict" } {
error $batch $errorInfo $errorCode
} else {
set done 2
break
}
} elseif { $code == 3 } {
# this is a break in the body. just return normally
set done 3
break
} elseif { $code != 0 } {
set done 4
break
}
}
}
switch -exact $done {
1 {return}
2 {return -code error -errorinfo $errorInfo \
-errorcode $errorCode $string}
3 {return}
4 {return -code $code $string}
}
# Get the next batch
set lastName [lindex [lindex $batch end] 1]
RetryTransaction {
set batch [mfs scan $dirName -start $lastName -count $count]
if { $lastName == [lindex [lindex $batch 0] 1] } {
set batch [lrange $batch 1 end]
}
}
}
}
# Original base 64 decode by Stephen Uhler/Brent Welch (c) 1997 Sun Microsystems
proc base64dec {source} {
set output {}
set group 0
set j 18
foreach char [split $source {}] {
if {[string compare $char "="]} {
#if { ![info exists base64($char)] } {
# continue
#}
#set bits $base64($char)
set bits [string first $char {ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/}]
if { $bits < 0 } {
continue
}
set group [expr {$group | ($bits << $j)}]
if {[incr j -6] < 0} {
scan [format %06x $group] %2x%2x%2x a b c
append output [format %c%c%c $a $b $c]
set group 0
set j 18
}
} else {
scan [format %06x $group] %2x%2x%2x a b c
if {$j == 6} {
append output [format %c $a]
} elseif {$j == 0} {
append output [format %c%c $a $b]
}
break
}
}
return $output
}
#set i 0
#foreach char {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
# a b c d e f g h i j k l m n o p q r s t u v w x y z \
# 0 1 2 3 4 5 6 7 8 9 + /} {
# set base64($char) $i
# incr i
#}