# HG changeset patch # User John W. Eaton # Date 1223631310 -7200 # Node ID e0229beb02c9800b2bc1ed1b79549b1aebcc2657 # Parent 7802023422e00e87cce35d9878a3b3ccf4db9f9e fileparts.m: handle "/file" correctly; improve compatibilty diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2008-10-08 John W. Eaton + + * miscellaneous/fileparts.m: Handle "/file" properly. + Improve compatibility. + 2008-10-02 John W. Eaton * pkg/pkg.m (configure_make): Handle filenames with spaces.: diff --git a/scripts/miscellaneous/fileparts.m b/scripts/miscellaneous/fileparts.m --- a/scripts/miscellaneous/fileparts.m +++ b/scripts/miscellaneous/fileparts.m @@ -33,9 +33,15 @@ if (es <= ds) es = length(filename)+1; endif - directory = filename(1:ds-1); + if (ds == 0) + directory = ""; + elseif (ds == 1) + directory = filename(1); + else + directory = filename(1:ds-1); + endif name = filename(ds+1:es-1); - if (es > 0) + if (es > 0 && es <= length (filename)) extension = filename(es:end); else extension = ""; @@ -49,3 +55,39 @@ endif endfunction + +%!test +%! [d, n, e] = fileparts ("file"); +%! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, "")); + +%!test +%! [d, n, e] = fileparts ("file.ext"); +%! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("/file.ext"); +%! assert (strcmp (d, "/") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("dir/file.ext"); +%! assert (strcmp (d, "dir") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("./file.ext"); +%! assert (strcmp (d, ".") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("d1/d2/file.ext"); +%! assert (strcmp (d, "d1/d2") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("/d1/d2/file.ext"); +%! assert (strcmp (d, "/d1/d2") && strcmp (n, "file") && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts ("/.ext"); +%! assert (strcmp (d, "/") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext")); + +%!test +%! [d, n, e] = fileparts (".ext"); +%! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));