convert a fully qualified path to relative path

convert a fully qualified path to relative path

Download torelativepath.zip

Synopsis:

torelativepath.pl


torelativepath.pl

Synopsis
sub toRelativePath($$)
  {
  my ($path, $prefix) = @_;
  $path = toUnixPath($path);
  $prefix = toUnixPath($prefix);
  $prefix .= '/' if (substr($prefix, -1) ne '/');
  
  $path =~ s/$prefix//;
  return toDosPath($path);
  }

#-- unit tests
sub test_torelativepath()
  {
  ut::assert(toRelativePath("c:/bin/bob", "c:/bin"),     'bob');
  ut::assert(toRelativePath("c:/bin/bob", "c:/bin/"),    'bob');
  ut::assert(toRelativePath("c:/bin/bob", "c:\\bin\\"),  'bob');

  ut::assert(toRelativePath("c:/bin/bob/", "c:/bin"),    'bob\\');
  ut::assert(toRelativePath("c:/bin/bob/", "c:/bin/"),   'bob\\');
  ut::assert(toRelativePath("c:/bin/bob/", "c:\\bin\\"), 'bob\\');

  ut::assert(toRelativePath("c:/bin/billy/bob", "c:\\bin\\"), 'billy\\bob');
  ut::assert(toRelativePath("c:/bin/billy/bob/", "c:\\bin\\"), 'billy\\bob\\');

  ut::assert(toRelativePath("c:/bin/billy/bob", "c:\\"), 'bin\\billy\\bob');
  ut::assert(toRelativePath("c:/bin/billy/bob/", "c:/"), 'bin\\billy\\bob\\');

  ut::assert(toRelativePath("c:\\bin/billy/bob", "c:\\"), 'bin\\billy\\bob');
  ut::assert(toRelativePath("c:\\bin/billy/bob/", "c:/"), 'bin\\billy\\bob\\');
  }

1;






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010