Changeset 1828


Ignore:
Timestamp:
Jul 15, 2010 7:28:00 AM (14 years ago)
Author:
dkearney
Message:

adding expandPath function used by chuse, possible some other tools. if given a full path, it will resolve simlinks for you and return the real location of a file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/scripts/utils.tcl

    r1715 r1828  
    2323proc Rappture::utils::hexdump {args} {
    2424    Rappture::getopts args params {
    25         value -lines unlimited
     25        value -lines unlimited
    2626    }
    2727    if {[llength $args] != 1} {
    28         error "wrong # args: should be \"hexdump ?-lines num? data\""
     28        error "wrong # args: should be \"hexdump ?-lines num? data\""
    2929    }
    3030    set newval [lindex $args 0]
     
    3434
    3535    if {$params(-lines) != "unlimited" && $params(-lines) <= 0} {
    36         return $rval
     36        return $rval
    3737    }
    3838
     
    4040    set len [string length $newval]
    4141    for {set i 0} {$i < $len} {incr i 8} {
    42         append rval [format "%#06x: " $i]
    43         set ascii ""
    44         for {set j 0} {$j < 8} {incr j} {
    45             if {$i+$j < $len} {
    46                 set char [string index $newval [expr {$i+$j}]]
    47                 binary scan $char c ichar
    48                 set hexchar [format "%02x" [expr {0xff & $ichar}]]
    49             } else {
    50                 set char " "
    51                 set hexchar "  "
    52             }
    53             append rval "$hexchar "
    54             if {[regexp {[\000-\037\177-\377]} $char]} {
    55                 append ascii "."
    56             } else {
    57                 append ascii $char
    58             }
    59         }
    60         append rval " | $ascii\n"
     42        append rval [format "%#06x: " $i]
     43        set ascii ""
     44        for {set j 0} {$j < 8} {incr j} {
     45            if {$i+$j < $len} {
     46                set char [string index $newval [expr {$i+$j}]]
     47                binary scan $char c ichar
     48                set hexchar [format "%02x" [expr {0xff & $ichar}]]
     49            } else {
     50                set char " "
     51                set hexchar "  "
     52            }
     53            append rval "$hexchar "
     54            if {[regexp {[\000-\037\177-\377]} $char]} {
     55                append ascii "."
     56            } else {
     57                append ascii $char
     58            }
     59        }
     60        append rval " | $ascii\n"
    6161
    62         if {"unlimited" != $params(-lines) && $i/8+1 >= $params(-lines)} {
    63             if {$i < $len-1} {
    64                 append rval "more..."
    65             }
    66             break
    67         }
     62        if {"unlimited" != $params(-lines) && $i/8+1 >= $params(-lines)} {
     63            if {$i < $len-1} {
     64                append rval "more..."
     65            }
     66            break
     67        }
    6868    }
    6969    return $rval
     
    7878proc Rappture::utils::binsize {size} {
    7979    foreach {factor units} {
    80         1073741824 GB
    81         1048576 MB
    82         1024 kB
    83         1 bytes
     80        1073741824 GB
     81        1048576 MB
     82        1024 kB
     83        1 bytes
    8484    } {
    85         if {$size/$factor > 0} {
    86             if {$factor > 1} {
    87                 set size [format "%.1f" [expr {double($size)/$factor}]]
    88             }
    89             break
    90         }
     85        if {$size/$factor > 0} {
     86            if {$factor > 1} {
     87                set size [format "%.1f" [expr {double($size)/$factor}]]
     88            }
     89            break
     90        }
    9191    }
    9292    return "$size $units"
     
    127127    return $desc
    128128}
     129
     130# ----------------------------------------------------------------------
     131# USAGE: expandPath <path>
     132#
     133# Returns the true location of the provided path,
     134# automatically expanding links to form an absolute path.
     135# ----------------------------------------------------------------------
     136proc Rappture::utils::expandPath {args} {
     137    set path ""
     138    set dirs [file split [lindex $args 0]]
     139
     140    while {[llength $dirs] > 0} {
     141        set d [lindex $dirs 0]
     142        set dirs [lrange $dirs 1 end]
     143        if {[catch {file link [file join $path $d]} out] == 0} {
     144            # directory d is a link, follow it
     145            set outdirs [file split $out]
     146            if {[string compare "/" [lindex $outdirs 0]] == 0} {
     147                # directory leads back to root
     148                # clear path
     149                # reset dirs list
     150                set path ""
     151                set dirs $outdirs
     152            } else {
     153                # relative path for the link
     154                # prepend directory to dirs list
     155                set dirs [concat $outdirs $dirs]
     156            }
     157        } else {
     158            set path [file join $path $d]
     159        }
     160    }
     161    return $path
     162}
     163
Note: See TracChangeset for help on using the changeset viewer.