changeset 8201:0ab4eed59455

fileparts.m: handle "/file" correctly; improve compatibilty
author John W. Eaton <jwe@octave.org>
date Wed, 08 Oct 2008 14:10:08 -0400
parents 837487bd3450
children cf59d542f33e
files scripts/ChangeLog scripts/miscellaneous/fileparts.m
diffstat 2 files changed, 49 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Oct 03 23:10:09 2008 +0200
+++ b/scripts/ChangeLog	Wed Oct 08 14:10:08 2008 -0400
@@ -1,3 +1,8 @@
+2008-10-08  John W. Eaton  <jwe@octave.org>
+
+	* miscellaneous/fileparts.m: Handle "/file" properly.
+	Improve compatibility.
+
 2008-10-07  Ben Abbott  <bpabbott@mac.com>
 
 	* plot/cla.m: New function.
--- a/scripts/miscellaneous/fileparts.m	Fri Oct 03 23:10:09 2008 +0200
+++ b/scripts/miscellaneous/fileparts.m	Wed Oct 08 14:10:08 2008 -0400
@@ -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"));