changeset 11873:e0229beb02c9 release-3-0-x

fileparts.m: handle "/file" correctly; improve compatibilty
author John W. Eaton <jwe@octave.org>
date Fri, 10 Oct 2008 11:35:10 +0200
parents 7802023422e0
children 757ca614e918
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 10 11:25:41 2008 +0200
+++ b/scripts/ChangeLog	Fri Oct 10 11:35:10 2008 +0200
@@ -1,3 +1,8 @@
+2008-10-08  John W. Eaton  <jwe@octave.org>
+
+	* miscellaneous/fileparts.m: Handle "/file" properly.
+	Improve compatibility.
+
 2008-10-02  John W. Eaton  <jwe@octave.org>
 
 	* pkg/pkg.m (configure_make): Handle filenames with spaces.: 
--- a/scripts/miscellaneous/fileparts.m	Fri Oct 10 11:25:41 2008 +0200
+++ b/scripts/miscellaneous/fileparts.m	Fri Oct 10 11:35:10 2008 +0200
@@ -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"));