home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / AbsPath.pm < prev    next >
Encoding:
Perl POD Document  |  1999-01-17  |  3.2 KB  |  146 lines

  1. package Win32::AbsPath;
  2. require Exporter;
  3. @ISA = (Exporter);
  4. @EXPORT = qw();   #&new);
  5. @EXPORT_OK = qw(RelativeToAbsolute Relative2Absolute canonpath FixPath FixPaths FullPath FullPaths);
  6. use Cwd;
  7. $VERSION = '1.0';
  8.  
  9. sub Relative2Absolute {
  10.  local $_;
  11.  foreach $_ (@_) {
  12. #print "DO: $_\n";
  13.   my $root;
  14.   s#\\#/#g;
  15.   if (m#^(\w:)(/.*)$#) {
  16.     $root = $1;
  17.     $_ = $2;
  18.   } elsif (m#^(//[^/]+/[^/]+)(.*)#) {
  19.     $root = $1;
  20.     $_ = $2;
  21.   } elsif (m#^(/.*)#) {
  22.    $root = getcwd();
  23.    $root =~ s#^(\w:|//[^/]+/[^/]+).*#$1#;
  24.   } elsif (m#^(\w:)(.*)$#) {
  25.     $root = $1;
  26.     $_ = $2;
  27.     my $oldcwd = getcwd();
  28.     chdir($root) or return;
  29.     $_ = substr( getcwd(), 2).'/'.$_;
  30.     chdir($oldcwd) or return;
  31.   } else {
  32.     $_ = getcwd().'/'.$_;
  33.     ($root,$_) = m#^(\w:|//[^/]+/[^/]+)(.*)$#;
  34.   }
  35.   s#//+#/#g;
  36.   s#(/\.)+(?=/|$)##g;
  37.   s#/\.\.(\.+)(?=/|$)#'/..'x(length($1)+1)#ge;
  38.   while(s{/[^/]+/\.\.(/|$)}{/}){};
  39.   s#(/\.\.)*/?$##;
  40.   s#^(/\.\.)+##;
  41.   $_=$root.$_;
  42.   s#^(\w:)$#$1\\#;
  43.   s#/#\\#g;
  44.  }
  45.  @_
  46. }
  47. *FixPaths = \&Relative2Absolute;
  48. *FullPaths=\&Relative2Absolute;
  49. *rel2abs = \&Relative2Absolute;
  50.  
  51. *canonpath=\&RelativeToAbsolute;
  52. *Fix=\&RelativeToAbsolute;
  53. *FixPath=\&RelativeToAbsolute;
  54. *FullPath=\&RelativeToAbsolute;
  55. *reltoabs = \&RelativeToAbsolute;
  56. sub RelativeToAbsolute ($) {
  57.  my $str = shift;
  58.  Relative2Absolute $str;
  59.  $str;
  60. }
  61.  
  62. 1;
  63.  
  64. =head1 NAME
  65.  
  66. Win32::AbsPath - convert relative to absolute paths
  67.  
  68. Version 1.0
  69.  
  70. =head1 SYNOPSIS
  71.  
  72.  use Win32::AbsPath;
  73.  $path = Win32::AbsPath::Fix '../some\dir\file.doc'
  74.  system("winword $path");
  75.  
  76.  use Win32::AbsPath qw(Relative2Absolute);
  77.  @paths = qw(
  78.   ..\dir\file.txt
  79.   ./other.doc
  80.   c:\boot.ini
  81.  );
  82.  Relative2Absolute @paths;
  83.  
  84. =head1 DESCRIPTION
  85.  
  86. Convert relative paths to absolute. Understands UNC paths.
  87.  
  88. The functions understands many different types of paths
  89.  
  90.     dir\file.txt
  91.     ..\dir\file.txt
  92.     c:\dir\file.txt
  93.     c:\dir\..\file.txt
  94.     \dir\file.txt
  95.     \\server\share\dir\..\file.txt
  96.     c:dir\file.txt
  97.  
  98. and of course you may pepper these with whatever mixtures of \.\ and
  99. \..\ you like. You may use both forward and backward slashes, the result
  100. will be in backward slashes.
  101.  
  102. ! The ussage of paths of type c:file.txt is slightly deprecated. It IS
  103. supported, but may lead to a change of current directory. The function
  104. first chdir()s top the current directory on the drive mentioned in the
  105. path and then back to cwd() in time it was called. If any of those
  106. chdir()s fails, the result of the function will be undef.
  107.  
  108. This is likely to happen if one of the drives is a floppy or CD, or
  109. if one of the drives was a network drive and was disconnected.
  110.  
  111. =head1 Functions
  112.  
  113. =over 2
  114.  
  115. =item Relative2Absolute
  116.  
  117.  Relative2Absolute @list;
  118.  
  119. Converts all paths in @list to absolute paths C<in-place>.
  120. That is the function changes the list you pass in.
  121.  
  122. =item RelativeToAbsolute
  123.  
  124.  $abspath = RelativeToAbsolute $relpath;
  125.  
  126. Converts the relative path to absolute. Returns the absolute path, but
  127. doesn't change the parameter. It takes exactly one parameter!
  128.  
  129.  print join(' ',RelativeToAbsolute '_file.txt','_other.txt');
  130.     prints
  131.  c:\_file.txt _other.txt
  132.     instead of
  133.  c:\_file.txt c:\_other.txt
  134.  
  135. =item Win32::AbsPath::Fix $path
  136.  
  137. The same as RelativeToAbsolute.
  138.  
  139. =back
  140.  
  141. =head2 AUTHOR
  142.  
  143. <Jenda@Krynicky.cz> and Mike <blazer@mail.nevalink.ru>
  144.  
  145. =cut
  146.