home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / Texte / scribus / scribus-1.3.3.9-win32-install.exe / tcl / tix8.1 / UnixFile.tcl < prev    next >
Text File  |  2001-11-03  |  8KB  |  413 lines

  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. #    $Id: UnixFile.tcl,v 1.4.2.1 2001/11/03 07:25:12 idiscovery Exp $
  4. #
  5. # UnixFile.tcl --
  6. #
  7. #    Unix file access portibility routines.
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15.  
  16. proc tixInitFileCmpt:Unix {} {
  17.  
  18. # tixFSSplit --
  19. # Splits a directory into its hierarchical components
  20. #
  21. # "hlist-type hierachical path"        <- "vpath"
  22. # "name"
  23. # "directory name"            <- "path"
  24. #
  25. proc tixFSSplit {dir} {
  26.     if {[string compare [tixFSPathType $dir] "absolute"]} {
  27.     error "$dir must be an absolute path"
  28.     }
  29.  
  30.     set path ""
  31.     set p ""
  32.     foreach d [tixFileSplit $dir] {
  33.     set p [tixFSJoin $p $d]
  34.     lappend path [list $p $d $p]
  35.     }
  36.     return $path
  37. }
  38.  
  39. # returns true if $dir is an valid path (always true in Unix)
  40. #
  41. proc tixFSValid {dir} {
  42.     return 1
  43. }
  44.  
  45. # Directory separator
  46. #
  47. proc tixFSSep {} {
  48.     return "/"
  49. }
  50.  
  51. # tixFSIntName
  52. #
  53. #    Returns the "virtual path" of a filename
  54. #
  55. proc tixFSIntName {dir} {
  56.     if {[string compare [tixFSPathType $dir] "absolute"]} {
  57.     error "$dir must be an absolute path"
  58.     }
  59.  
  60.     return $dir
  61. }
  62.  
  63. proc tixFSResolveName {p} {
  64.     return $p
  65. }
  66.  
  67.  
  68. # These subcommands of "file" only exist in Tcl 7.5+. We define the following
  69. # wrappers so that the code also works under Tcl 7.4
  70. #
  71. global tcl_version
  72. if {![string compare $tcl_version 7.4]} {
  73.  
  74.     proc tixFSPathType {dir} {
  75.     if {![string compare [string index $dir 0] /]} {
  76.         return "absolute"
  77.     }
  78.     if {![string compare [string index $dir 0] ~]} {
  79.         return "absolute"
  80.     }
  81.     return "relative"
  82.     }
  83.  
  84.     proc tixFSJoin {dir sub} {
  85.     set joined $dir/$sub
  86.  
  87.     regsub -all {[/]+} $joined / joined
  88.     return $joined
  89.     }
  90.  
  91. } else {
  92.     proc tixFSPathType {dir} {
  93.     return [file pathtype $dir]
  94.     }
  95.  
  96.     proc tixFSJoin {dir sub} {
  97.     return [file join $dir $sub]
  98.     }
  99. }
  100.  
  101. # dir:        Make a listing of this directory
  102. # showSubDir:    Want to list the subdirectories?
  103. # showFile:    Want to list the non-directory files in this directory?
  104. # showPrevDir:    Want to list ".." as well?
  105. # showHidden:    Want to list the hidden files?
  106. #
  107. # return value:    a list of files and/or subdirectories
  108. #
  109. proc tixFSListDir {dir showSubDir showFile showPrevDir showHidden {pattern ""}} {
  110.     set appPWD [pwd]
  111.  
  112.     if {[catch {cd $dir} err]} {
  113.     # The user has entered an invalid directory
  114.     # %% todo: prompt error, go back to last succeed directory
  115.     cd $appPWD
  116.     return ""
  117.     }
  118.  
  119.     if {$pattern == ""} {
  120.     if $showHidden {
  121.         set pattern "* .*"
  122.     } else {
  123.         set pattern *
  124.     }
  125.     } elseif {$pattern == "*"} {
  126.     if $showHidden {
  127.         set pattern "* .*"
  128.     }
  129.     }
  130.  
  131.     set list ""
  132.     foreach pat $pattern {
  133.     if {[catch {set names [lsort [glob -nocomplain $pat]]} err]} {
  134.         # Cannot read directory
  135.         # %% todo: show directory permission denied
  136.         continue
  137.     }
  138.  
  139.     catch {
  140.         # We are catch'ing, just in case the "file" command
  141.         # returns unexpected errors
  142.         #
  143.         foreach fname $names {
  144.         if {![string compare . $fname]} {
  145.             continue
  146.         }
  147.         if {[file isdirectory $fname]} {
  148.             if {![string compare ".." $fname] && !$showPrevDir} {
  149.             continue
  150.             }
  151.             if $showSubDir {
  152.             lappend list [file tail $fname]
  153.             }
  154.         } else {
  155.             if $showFile {
  156.             lappend list [file tail $fname]
  157.             }
  158.         }
  159.         }
  160.     }
  161.     }
  162.  
  163.     cd $appPWD
  164.  
  165.     if {[llength $pattern] > 1} {
  166.     # get rid of duplicated names
  167.     #
  168.     set list1 ""
  169.     set oldfile ""
  170.     foreach name [lsort $list] {
  171.         if {$name == $oldfile} {
  172.         continue
  173.         }
  174.         lappend list1 $name
  175.         set oldfile $name
  176.     }
  177.     return [_tixFSMakeList $dir $list1]
  178.     } else {
  179.     return [_tixFSMakeList $dir $list]
  180.     }
  181. }
  182.  
  183. # _tixFSMakeList -
  184. #
  185. #    Internal procedure. Used only by tixFSListDir
  186. proc _tixFSMakeList {dir list} {
  187.     set l ""
  188.     foreach file $list {
  189.     set path [tixFSJoin $dir $file]
  190.     lappend l [list $path $file $path]
  191.     }
  192.  
  193.     return $l
  194. }
  195.  
  196. # Directory separator
  197. #
  198. proc tixDirSep {} {
  199.     return "/"
  200. }
  201.  
  202.  
  203. # tixFSInfo --
  204. #
  205. #    Returns information about the file system of this OS
  206. #
  207. # hasdrives: Boolean
  208. #    Does this file system support seperate disk drives?
  209. #
  210. proc tixFSInfo {args} {
  211.     case [lindex $args 0] {
  212.     hasdrives {
  213.         return 0
  214.     }
  215.     }
  216. }
  217.  
  218. #----------------------------------------------------------------------
  219. # Obsolete
  220. #----------------------------------------------------------------------
  221.  
  222. # nativeName:    native filename used in this OS, comes from the user or
  223. #        application programmer
  224. # defParent:    if the filename is not an absolute path, treat it as a
  225. #        subfolder of $defParent
  226. proc tixFileIntName {nativeName {defParent ""}} {
  227.     if {![tixIsAbsPath $nativeName]} {
  228.     if {$defParent != ""} {
  229.         set path [tixSubFolder $defParent $nativeName]
  230.     } else {
  231.         set path $nativeName
  232.     }
  233.     } else {
  234.     set path $nativeName
  235.     }
  236.  
  237.     set intName ""
  238.     set path [tixFile trimslash [tixFile tildesubst $path]]
  239.     foreach name [tixFileSplit $path] {
  240.     set intName [tixSubFolder $intName $name]
  241.     }
  242.     return $intName
  243. }
  244.  
  245. proc tixNativeName {name {mustBeAbs ""}} {
  246.     return $name
  247. }
  248.  
  249. proc tixFileDisplayName {intName} {
  250.     if {$intName == "/"} {
  251.     return "/"
  252.     } else {
  253.     return [file tail $intName]
  254.     }
  255. }
  256.  
  257.  
  258. proc tixFileSplit {intName} {
  259.  
  260.     set l ""
  261.     foreach n [split $intName /] {
  262.     if {$n == ""} {
  263.         continue
  264.     }
  265.     if {$n == "."} {
  266.         continue
  267.     }
  268.  
  269.     lappend l $n
  270.     }
  271.     
  272.  
  273.     while {1} {
  274.     set idx [lsearch $l ".."]
  275.     if {$idx == -1} {
  276.         break;
  277.     }
  278.     set l [lreplace $l [expr $idx -1] $idx]
  279.     }
  280.  
  281.  
  282.     if {[string index $intName 0] == "/"} {
  283.     return [concat "/" $l]
  284.     } else {
  285.     return $l
  286.     }
  287. }
  288.  
  289. proc tixSubFolder {parent sub} {
  290.     if {$parent == ""} {
  291.     return $sub
  292.     }
  293.     if {$parent == "/"} {
  294.     return /$sub
  295.     } else {
  296.     return $parent/$sub
  297.     }
  298. }
  299.  
  300. # dir:        Make a listing of this directory
  301. # showSubDir:    Want to list the subdirectories?
  302. # showFile:    Want to list the non-directory files in this directory?
  303. # showPrevDir:    Want to list ".." as well?
  304. # showHidden:    Want to list the hidden files?
  305. #
  306. # return value:    a list of files and/or subdirectories
  307. #
  308. proc tixListDir {dir showSubDir showFile showPrevDir showHidden {pattern ""}} { 
  309.  
  310.     set appPWD [pwd]
  311.  
  312.     if {[catch {cd $dir} err]} {
  313.     # The user has entered an invalid directory
  314.     # %% todo: prompt error, go back to last succeed directory
  315.     cd $appPWD
  316.     return ""
  317.     }
  318.  
  319.     if {$pattern == ""} {
  320.     if $showHidden {
  321.         set pattern "* .*"
  322.     } else {
  323.         set pattern *
  324.     }
  325.     } elseif {$pattern == "*"} {
  326.     if $showHidden {
  327.         set pattern "* .*"
  328.     }
  329.     }
  330.  
  331.     set list ""
  332.     foreach pat $pattern {
  333.     if {[catch {set names [lsort [glob -nocomplain $pat]]} err]} {
  334.         # Cannot read directory
  335.         # %% todo: show directory permission denied
  336.         continue
  337.     }
  338.  
  339.     catch {
  340.         # We are catch'ing, just in case the "file" command
  341.         # returns unexpected errors
  342.         #
  343.         foreach fname $names {
  344.         if {![string compare . $fname]} {
  345.             continue
  346.         }
  347.         if {[file isdirectory $fname]} {
  348.             if {![string compare ".." $fname] && !$showPrevDir} {
  349.             continue
  350.             }
  351.             if $showSubDir {
  352.             lappend list [file tail $fname]
  353.             }
  354.         } else {
  355.             if $showFile {
  356.             lappend list [file tail $fname]
  357.             }
  358.         }
  359.         }
  360.     }
  361.     }
  362.  
  363.     cd $appPWD
  364.  
  365.     if {[llength $pattern] > 1} {
  366.     set list1 ""
  367.     set oldfile ""
  368.     foreach name [lsort $list] {
  369.         if {$name == $oldfile} {
  370.         continue
  371.         }
  372.         lappend list1 $name
  373.         set oldfile $name
  374.     }
  375.     return $list1
  376.     } else {
  377.     return $list
  378.     }
  379. }
  380.  
  381. # returns the "root directory" of this operating system
  382. #
  383. proc tixRootDir {} {
  384.     return "/"
  385. }
  386.  
  387. proc tixIsAbsPath {nativeName} {
  388.     set c [string index $nativeName 0]
  389.     if {$c == "~" || $c == "/"} {
  390.     return 1
  391.     } else {
  392.     return 0
  393.     }
  394. }
  395.  
  396. proc tixVerifyFile {file} {
  397.     return [tixFileIntName $file]
  398. }
  399.  
  400. proc tixFilePattern {args} {
  401.     if {[lsearch $args allFiles] != -1} {
  402.     return *
  403.     }
  404.     return *
  405. }
  406. }
  407.  
  408.  
  409.  
  410.  
  411.  
  412.