##
##  wml::sup::path - Filesystem Path Manipulation
##  Copyright (c) 1997-2001 Ralf S. Engelschall, All Rights Reserved. 
##

<protect pass=2>
<:
sub relpath {
    my ($from, $to) = @_;
    my (@F, @T, $i, $j, $path);

    #   canonicalize input
    $from .= '/' if ($from !~ m|/$|);
    $to   .= '/' if ($to   !~ m|/$|);
    $from = '' if ($from eq './');
    $to   = '' if ($to   eq './');
    $path = '';

    #   split into directory parts
    @F = split('/', $from);
    @T = split('/', $to);

    #   skip common prefix dirs
    for ($i = 0; $i <= $#F; $i++) {
        last if ($F[$i] ne $T[$i]);
    }

    #   go back for remaining "from" dirs
    for ($j = $i; $j <= $#F; $j++) {
        $path .= '../';
    }

    #   go foreward for remaining "to" dirs
    for ($j = $i; $j <= $#T; $j++) {
        $path .= "$T[$j]/";
    }

    return $path;
}

sub canonpath {
    my ($path) = @_;

    my $pathL = '';
    while ($path ne $pathL) {
        $pathL = $path;
        $path =~ s|/+|/|g;
        $path =~ s|/\./|/|g;
        $path =~ s|/\.$|/|;
        $path =~ s|^\./(.)|$1|g;
        $path =~ s{(^|/)([^/]+)(/\.\./)}{if ($2 ne '..') { $1 } else { $1.$2.$3 }}eg;
    }
    $path =~ s{(^|/)([^/]+)(/\.\.)$}{if ($2 ne '..') { $1 } else { $1.$2.$3.'/' }}eg;
    $path = './' if $path eq '';
    return $path;
}

sub canonurl {
    my ($url) = @_;

    if ($url =~ m|^(.+?://)(.+)|) {
        $url = $1.&canonpath($2);
    }
    else {
        $url = &canonpath($url);
    }
    return $url;
}
:>
</protect>

##EOF##
