home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-08-18 | 3.5 KB | 105 lines | [TEXT/McPL] |
- # NOTE: Derived from ./blib/lib/URI/URL/file.pm. Changes made here will be lost.
- #
- # MacPerl assumptions: $path contains a local path (with :) not a network
- # path (with /). $path *must* contain a : to be
- # considered absolute ($path = "Macintosh HD" will
- # be treated as a relative, $path = "Macintosh HD:"
- # will be treated as absolute. This is annoying, but
- # then so are the MacOS rules for pathnames-in-string).
- # $path should be converted to network before passing
- # to $url->path. It's yet to be determined whether
- # they should be generally escaped before that,
- # but / in names will be taken care of here as will
- # the legal names "." and "..".
- #
- package URI::URL::file;
-
- sub newlocal {
- my($class, $path) = @_;
-
- Carp::croak("This version only works for Mac filesystems")
- unless $ostype eq "mac";
- # XXX: Should implement the same thing for other systems
-
- my $url = new URI::URL "file://";
- if (!defined $path or $path eq "") {
- require Cwd;
- my $cwd = Cwd::fastcwd();
- # force trailing : on dir
- $cwd =~ s/:?$/:/;
- $path = $cwd;
- } elsif ($path =~ m/^:/ or $path !~ /:/) {
- require Cwd;
- my $cwd = Cwd::fastcwd();
- # force trailing : on dir, but only if the path doesn't already have one
- $cwd =~ s/:?$/:/ unless $path =~ /^:/;
- $path = $cwd . $path;
- }
- $path = localtonet($path);
- #
- # Now do something to prevent unpleasantness if the path contains either
- # / or ., both of which, along with any %, have been trigraphed in localtonet
- #
- my $hold = $URI::URL::reserved_no_slash;
- $URI::URL::reserved_no_slash =~ s/%//;
- $url->path($path);
- $URI::URL::reserved_no_slash = $hold;
- $url;
- }
- sub localtonet {
- #
- # Mac path to the Unix like equivalent to be used in file URL's.
- # This makes no attempt to detect illegal Mac paths (e.g. a:::a).
- #
- my $inpath = $_[0];
- #
- # First problem: if the path contains "/", we've go to do something. You
- # can just trigraph it, but then the % will get changed to a %25 in the
- # subsequent call to $url->path above. So we do two things, trigraph all
- # the %'s here, and then trigraph the /'s (and, below, the .'s). Then above
- # we will kludge and prevent $url->path from trigraphing %'s.
- #
- $inpath =~ s,%,%25,g;
- $inpath =~ s,/,%2F,g;
- #
- # If there are no :'s in the name at all, assume it's a single item in the
- # current directory. Return it
- #
- return $inpath if ($inpath !~ m,:,);
- #
- # If we now split on :, there will be just as many nulls in the list as
- # there should be up requests, except if it begins with a :, where there
- # will be one extra.
- #
- my @names = split(/:/,$inpath);
- shift(@names) unless $names[0];
- my @outname = ();
- #
- # Work from the end.
- #
- my $i;
- for($i = $#names; $i >= 0;$i--) {
- if ($names[$i] eq "") {
- unshift(@outname,"..");
- } else {
- #
- # There's a problem similar to the / problem here. If we trigraph the .'s,
- # you have to prevent the %'s from being trigraphed further on.
- #
- #
- $names[$i] = "%2E%2E" if($names[$i] eq "..");
- $names[$i] = "%2E" if($names[$i] eq ".");
- unshift(@outname,$names[$i]);
- }
- }
- my $netpath = join("/",@outname);
- $netpath = $netpath . "/" if($inpath =~ /:$/);
- if($inpath !~ m,^:,) {
- return "/".$netpath;
- } else {
- return $netpath;
- }
- }
-
- 1;
-