home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-02 | 2.4 KB | 97 lines | [TEXT/McPL] |
- # NOTE: Derived from ./blib/lib/URI/URL/file.pm. Changes made here will be lost.
- package URI::URL::file;
-
- sub mac_path
- {
- my $self = shift;
- my $path = $self->path; #this gives me an unescaped path
- $path = nettolocal($path);
- return $path;
- }
-
- sub nettolocal {
- my $inpath = $_[0];
- my $macpath = '';
- #
- # First nail all groups of /, and convert them to single /.
- #
- $inpath =~ s:/{2,}:/:g;
- #
- # Next get rid of the no-op, ./. But we must make sure names like
- # /g./f./h. are treated as legal file names and not touched. First
- # nail the possible /. at the end...
- #
- $inpath =~ s,/\.$,/,;
- #
- # and then nail the interior ./
- #
- $inpath =~ s,/(\./)+,/,g;
- #
- # Now that they're all taken care of, only the leading one should be
- # left.
- #
- $inpath =~ s,^\./,,;
- #
- # If after this $inpath is empty, it must have been all ./'s. Just return
- # ":" here.
- #
- return ":" unless ($inpath);
- #
- # If here $inpath = "/", we'll return an error. Guessing what might
- # have been meant on a Mac is too hard. Let the user fix the path.
- #
- Carp::croak("root directory: which one do you mean?") if ($inpath eq "/");
- #
- # If there are no / left in the name, it's a file in the pwd. Simply
- # return it as a Mac relative (could leave off the :, but why foster
- # bad habits)
- #
- return ":".$inpath if ($inpath !~ m,/,);
- #
- # Now all that should be left are names and ../.
- #
- my @names = split(/\//,$inpath);
- #
- # There may be a null on the front if the path began with /
- #
- 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 {
- unshift(@outname,$names[$i]);
- }
- }
- #
- # Now check for illegal Mac names before joining them all
- # together
- #
- foreach $name (@outname) {
- if(length($name) > 31 or $name =~ /:/) {
- Carp::croak("$name is > 31 characters or contains :");
- }
- }
- $macpath = join(":",@outname);
- #
- # If the Unix path is relative, so should be the Mac path.
- # Prepend a : (this also puts the proper number of : at the beginning if it
- # already has one or more).
- #
- $macpath = ":".$macpath if ($inpath !~ m,^/,);
- #
- # This is just a nicety: if the Unix path ends in /, the Mac path should
- # end in : unless it already does.
- #
- if ($inpath =~ m,/$,) {
- $macpath .= ":" unless ($macpath =~ m,:$,);
- }
- return $macpath;
- }
-
- 1;
-