Version 0 of Matthias Hoffmann - Tcl-Code-Snippets - misc - globx

Updated 2004-12-13 15:33:01

package provide Globx 0.03

 proc globx {startDir {search *} {cb ""}} {
      set dirStack [list [file normalize $startDir]]
      set files {}
      set fc    0
      while {[llength $dirStack]} {
            set newStack {}
            foreach dir $dirStack {
                    # temporary var's only because eventually using CallBack
                    set fn [glob -noc -typ f          -dir $dir -- $search]
                    set fh [glob -noc -typ {f hidden} -dir $dir -- $search]
                    if {[string equal $cb ""]} {
                       eval lappend files $fn $fh
                    } else {
                       foreach f [concat $fn $fh] {
                               incr fc
                               uplevel [list $cb $f]
                       }
                    }
                    set dn [glob -noc -typ d          -dir $dir *]
                    set dh [glob -noc -typ {d hidden} -dir $dir *]
                    # eval lappend newStack $dn $dh; # v0.01
                    # Wikipatch Start v0.02 ---
                    foreach newDir [concat $dn $dh] {
                            set theDir [file tail $newDir]
                            if {[string equal $theDir "." ] || \
                                [string equal $theDir ".."]} {
                               # Don't push this, otherwise entering an endless
                               # loop (on UNIX, at least)
                            } else {
                               lappend newStack $newDir
                            }
                    }
                    # Wikipatch Ende ---
            }
            set dirStack $newStack
            update; # keep Background alive
      }
      if {[string equal $cb ""]} {
         return [lsort $files]
      } else {
         return $fc
      }
 }

 # Enumerating only directories

 proc globx2 {startDir {cb ""}} {
      set dirStack [list [file normalize $startDir]]
      set dirs {}
      set dc   0
      while {[llength $dirStack]} {
            set newStack {}
            foreach dir $dirStack {
                    set dn [glob -noc -typ d          -dir $dir -- *]
                    set dh [glob -noc -typ {d hidden} -dir $dir -- *]
                    if {[string equal $cb ""]} {
                       eval lappend dirs $dn $dh
                    } else {
                       foreach d [concat $dn $dh] {
                               incr dc
                               uplevel [list $cb $d]
                       }
                    }
                    foreach newDir [concat $dn $dh] {
                            set theDir [file tail $newDir]
                            if {[string equal $theDir "." ] || \
                                [string equal $theDir ".."]} {
                               # Don't push this, otherwise entering an endless
                               # loop (on UNIX, at least)
                            } else {
                               lappend newStack $newDir
                            }
                    }
            }
            set dirStack $newStack
            update
      }
      if {[string equal $cb ""]} {
         return [lsort $dirs]
      } else {
         return $dc
      }
 }