home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / lib / zip / File / Basename.pm next >
Text File  |  1999-04-02  |  9KB  |  286 lines

  1. package File::Basename;
  2.  
  3. =head1 NAME
  4.  
  5. fileparse - split a pathname into pieces
  6.  
  7. basename - extract just the filename from a path
  8.  
  9. dirname - extract just the directory from a path
  10.  
  11. =head1 SYNOPSIS
  12.  
  13.     use File::Basename;
  14.  
  15.     ($name,$path,$suffix) = fileparse($fullname,@suffixlist)
  16.     fileparse_set_fstype($os_string);
  17.     $basename = basename($fullname,@suffixlist);
  18.     $dirname = dirname($fullname);
  19.  
  20.     ($name,$path,$suffix) = fileparse("lib/File/Basename.pm","\.pm");
  21.     fileparse_set_fstype("VMS");
  22.     $basename = basename("lib/File/Basename.pm",".pm");
  23.     $dirname = dirname("lib/File/Basename.pm");
  24.  
  25. =head1 DESCRIPTION
  26.  
  27. These routines allow you to parse file specifications into useful
  28. pieces using the syntax of different operating systems.
  29.  
  30. =over 4
  31.  
  32. =item fileparse_set_fstype
  33.  
  34. You select the syntax via the routine fileparse_set_fstype().
  35.  
  36. If the argument passed to it contains one of the substrings
  37. "VMS", "MSDOS", "MacOS", "AmigaOS" "MSWin32" or "RISCOS", the file
  38. specification syntax of that operating system is used in future calls
  39. to fileparse(), basename(), and dirname().  If it contains none of
  40. these substrings, UNIX syntax is used.  This pattern matching is
  41. case-insensitive.  If you've selected VMS syntax, and the file
  42. specification you pass to one of these routines contains a "/",
  43. they assume you are using UNIX emulation and apply the UNIX syntax
  44. rules instead, for that function call only.
  45.  
  46. If the argument passed to it contains one of the substrings "VMS",
  47. "MSDOS", "MacOS", "AmigaOS", "os2", "MSWin32" or "RISCOS", then the pattern
  48. matching for suffix removal is performed without regard for case,
  49. since those systems are not case-sensitive when opening existing files
  50. (though some of them preserve case on file creation).
  51.  
  52. If you haven't called fileparse_set_fstype(), the syntax is chosen
  53. by examining the builtin variable C<$^O> according to these rules.
  54.  
  55. =item fileparse
  56.  
  57. The fileparse() routine divides a file specification into three
  58. parts: a leading B<path>, a file B<name>, and a B<suffix>.  The
  59. B<path> contains everything up to and including the last directory
  60. separator in the input file specification.  The remainder of the input
  61. file specification is then divided into B<name> and B<suffix> based on
  62. the optional patterns you specify in C<@suffixlist>.  Each element of
  63. this list is interpreted as a regular expression, and is matched
  64. against the end of B<name>.  If this succeeds, the matching portion of
  65. B<name> is removed and prepended to B<suffix>.  By proper use of
  66. C<@suffixlist>, you can remove file types or versions for examination.
  67.  
  68. You are guaranteed that if you concatenate B<path>, B<name>, and
  69. B<suffix> together in that order, the result will denote the same
  70. file as the input file specification.
  71.  
  72. =back
  73.  
  74. =head1 EXAMPLES
  75.  
  76. Using UNIX file syntax:
  77.  
  78.     ($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
  79.                     '\.book\d+');
  80.  
  81. would yield
  82.  
  83.     $base eq 'draft'
  84.     $path eq '/virgil/aeneid/',
  85.     $type eq '.book7'
  86.  
  87. Similarly, using VMS syntax:
  88.  
  89.     ($name,$dir,$type) = fileparse('Doc_Root:[Help]Rhetoric.Rnh',
  90.                    '\..*');
  91.  
  92. would yield
  93.  
  94.     $name eq 'Rhetoric'
  95.     $dir  eq 'Doc_Root:[Help]'
  96.     $type eq '.Rnh'
  97.  
  98. =over
  99.  
  100. =item C<basename>
  101.  
  102. The basename() routine returns the first element of the list produced
  103. by calling fileparse() with the same arguments, except that it always
  104. quotes metacharacters in the given suffixes.  It is provided for
  105. programmer compatibility with the UNIX shell command basename(1).
  106.  
  107. =item C<dirname>
  108.  
  109. The dirname() routine returns the directory portion of the input file
  110. specification.  When using VMS or MacOS syntax, this is identical to the
  111. second element of the list produced by calling fileparse() with the same
  112. input file specification.  (Under VMS, if there is no directory information
  113. in the input file specification, then the current default device and
  114. directory are returned.)  When using UNIX or MSDOS syntax, the return
  115. value conforms to the behavior of the UNIX shell command dirname(1).  This
  116. is usually the same as the behavior of fileparse(), but differs in some
  117. cases.  For example, for the input file specification F<lib/>, fileparse()
  118. considers the directory name to be F<lib/>, while dirname() considers the
  119. directory name to be F<.>).
  120.  
  121. =back
  122.  
  123. =cut
  124.  
  125.  
  126. ## use strict;
  127. use re 'taint';
  128.  
  129. require Exporter;
  130. @ISA = qw(Exporter);
  131. @EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
  132. use vars qw($VERSION $Fileparse_fstype $Fileparse_igncase);
  133. $VERSION = "2.6";
  134.  
  135.  
  136. #   fileparse_set_fstype() - specify OS-based rules used in future
  137. #                            calls to routines in this package
  138. #
  139. #   Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
  140. #       Any other name uses Unix-style rules and is case-sensitive
  141.  
  142. sub fileparse_set_fstype {
  143.   my @old = ($Fileparse_fstype, $Fileparse_igncase);
  144.   if (@_) {
  145.     $Fileparse_fstype = $_[0];
  146.     $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i);
  147.   }
  148.   wantarray ? @old : $old[0];
  149. }
  150.  
  151. #   fileparse() - parse file specification
  152. #
  153. #   Version 2.4  27-Sep-1996  Charles Bailey  bailey@genetics.upenn.edu
  154.  
  155.  
  156. sub fileparse {
  157.   my($fullname,@suffices) = @_;
  158.   my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
  159.   my($dirpath,$tail,$suffix,$basename);
  160.   my($taint) = substr($fullname,0,0);  # Is $fullname tainted?
  161.  
  162.   if ($fstype =~ /^VMS/i) {
  163.     if ($fullname =~ m#/#) { $fstype = '' }  # We're doing Unix emulation
  164.     else {
  165.       ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/);
  166.       $dirpath ||= '';  # should always be defined
  167.     }
  168.   }
  169.   if ($fstype =~ /^RISCOS/i) {
  170.       if (!defined &RISCOS::Filespec::convert
  171.           || &RISCOS::Filespec::convert()) {
  172.         # Don't have the function, or don't have conversion on - Unix emulation
  173.         $fstype = '';
  174.       }
  175.       else {
  176.         ($dirpath,$basename) = ($fullname =~ /(.*[:\.])?(.*)/);
  177.     $dirpath = '@.' unless $dirpath;
  178.       }
  179.   }
  180.   if ($fstype =~ /^MS(DOS|Win32)/i) {
  181.     ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/);
  182.     $dirpath .= '.\\' unless $dirpath =~ /[\\\/]$/;
  183.   }
  184.   elsif ($fstype =~ /^MacOS/i) {
  185.     ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/);
  186.   }
  187.   elsif ($fstype =~ /^AmigaOS/i) {
  188.     ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/);
  189.     $dirpath = './' unless $dirpath;
  190.   }
  191.   elsif ($fstype !~ /^VMS/i) {  # default to Unix
  192.     ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#);
  193.     if ($^O eq 'VMS' and $fullname =~ m:/[^/]+/000000/?:) {
  194.       # dev:[000000] is top of VMS tree, similar to Unix '/'
  195.       ($basename,$dirpath) = ('',$fullname);
  196.     }
  197.     $dirpath = './' unless $dirpath;
  198.   }
  199.  
  200.   if (@suffices) {
  201.     $tail = '';
  202.     foreach $suffix (@suffices) {
  203.       my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
  204.       if ($basename =~ s/$pat//) {
  205.         $taint .= substr($suffix,0,0);
  206.         $tail = $1 . $tail;
  207.       }
  208.     }
  209.   }
  210.  
  211.   $tail .= $taint if defined $tail; # avoid warning if $tail == undef
  212.   wantarray ? ($basename . $taint, $dirpath . $taint, $tail)
  213.             : $basename . $taint;
  214. }
  215.  
  216.  
  217. #   basename() - returns first element of list returned by fileparse()
  218.  
  219. sub basename {
  220.   my($name) = shift;
  221.   (fileparse($name, map("\Q$_\E",@_)))[0];
  222. }
  223.  
  224.  
  225. #    dirname() - returns device and directory portion of file specification
  226. #        Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
  227. #        filespecs except for names ending with a separator, e.g., "/xx/yy/".
  228. #        This differs from the second element of the list returned
  229. #        by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
  230. #        the last directory name if the filespec ends in a '/' or '\'), is lost.
  231.  
  232. sub dirname {
  233.     my($basename,$dirname) = fileparse($_[0]);
  234.     my($fstype) = $Fileparse_fstype;
  235.  
  236.     if ($fstype =~ /VMS/i) { 
  237.         if ($_[0] =~ m#/#) { $fstype = '' }
  238.         else { return $dirname || $ENV{DEFAULT} }
  239.     }
  240.     if ($fstype =~ /^RISCOS/i) {
  241.       if (!defined &RISCOS::Filespec::convert || &RISCOS::Filespec::convert()) {
  242.         $fstype = '';
  243.       } else {
  244.         $dirname =~ s/\.*$//;
  245.         unless( length($basename) ) {
  246.       ($basename,$dirname) = fileparse $dirname;
  247.       $dirname =~ s/\.*$//;
  248.     }
  249.       }
  250.     }
  251.     if ($fstype =~ /MacOS/i) { return $dirname }
  252.     elsif ($fstype =~ /MSDOS/i) { 
  253.         $dirname =~ s/([^:])[\\\/]*$/$1/;
  254.         unless( length($basename) ) {
  255.         ($basename,$dirname) = fileparse $dirname;
  256.         $dirname =~ s/([^:])[\\\/]*$/$1/;
  257.     }
  258.     }
  259.     elsif ($fstype =~ /MSWin32/i) { 
  260.         $dirname =~ s/([^:])[\\\/]*$/$1/;
  261.         unless( length($basename) ) {
  262.         ($basename,$dirname) = fileparse $dirname;
  263.         $dirname =~ s/([^:])[\\\/]*$/$1/;
  264.     }
  265.     }
  266.     elsif ($fstype =~ /AmigaOS/i) {
  267.         if ( $dirname =~ /:$/) { return $dirname }
  268.         chop $dirname;
  269.         $dirname =~ s#[^:/]+$## unless length($basename);
  270.     }
  271.     else { 
  272.         $dirname =~ s:(.)/*$:$1:;
  273.         unless( length($basename) ) {
  274.         local($File::Basename::Fileparse_fstype) = $fstype;
  275.         ($basename,$dirname) = fileparse $dirname;
  276.         $dirname =~ s:(.)/*$:$1:;
  277.     }
  278.     }
  279.  
  280.     $dirname;
  281. }
  282.  
  283. fileparse_set_fstype $^O;
  284.  
  285. 1;
  286.