comparison scripts/miscellaneous/fullfile.m @ 7514:4f6a73fd8df9

fullfile: improve handling of args ending with filesep
author John W. Eaton <jwe@octave.org>
date Fri, 22 Feb 2008 13:47:38 -0500
parents 1885f4c7e4b3
children 3422f39573b1 72830070a17b
comparison
equal deleted inserted replaced
7513:05eb3486f650 7514:4f6a73fd8df9
23 ## @end deftypefn 23 ## @end deftypefn
24 24
25 function filename = fullfile (varargin) 25 function filename = fullfile (varargin)
26 26
27 if (nargin > 0) 27 if (nargin > 0)
28 filename = ""; 28 ## Discard all empty arguments
29 for first = 1:nargin 29 varargin(cellfun (@isempty, varargin)) = [];
30 tmp = varargin{first}; 30 nargs = numel (varargin);
31 if (! isempty (tmp)) 31 if (nargs > 1)
32 filename = tmp; 32 filename = varargin{1};
33 break; 33 if (strcmp (filename(end), filesep))
34 filename(end) = "";
34 endif 35 endif
35 endfor 36 for i = 2:nargs
36 for i = first+1:nargin 37 tmp = varargin{i};
37 tmp = varargin{i}; 38 if (i < nargs && strcmp (tmp(end), filesep))
38 if (! isempty (tmp)) 39 tmp(end) = "";
39 if (strcmp (tmp(1), filesep)) 40 elseif (i == nargs && strcmp (tmp, filesep))
40 tmp(1) = ""; 41 tmp = "";
41 endif 42 endif
42 if (i < nargin && strcmp (tmp(end), filesep)) 43 filename = strcat (filename, filesep, tmp);
43 tmp(end) = ""; 44 endfor
44 endif 45 elseif (nargs == 1)
45 filename = strcat (filename, filesep, tmp); 46 filename = varargin{1};
46 endif 47 else
47 endfor 48 filename = "";
49 endif
48 else 50 else
49 print_usage (); 51 print_usage ();
50 endif 52 endif
51 53
52 endfunction 54 endfunction
53 55
56 %!shared fs, fsx, xfs, fsxfs, xfsy
57 %! fs = filesep ();
58 %! fsx = strcat (fs, "x");
59 %! xfs = strcat ("x", fs);
60 %! fsxfs = strcat (fs, "x", fs);
61 %! xfsy = strcat ("x", fs, "y");
54 %!assert (fullfile (""), "") 62 %!assert (fullfile (""), "")
55 %!assert (fullfile (filesep ()), filesep ()) 63 %!assert (fullfile (fs), fs)
56 %!assert (fullfile ("", filesep ()), filesep ()) 64 %!assert (fullfile ("", fs), fs)
57 %!assert (fullfile (filesep (), ""), filesep ()) 65 %!assert (fullfile (fs, ""), fs)
58 %!assert (fullfile ("", filesep ()), filesep ()) 66 %!assert (fullfile ("", fs), fs)
59 %!assert (fullfile ("foo"), "foo") 67 %!assert (fullfile ("x"), "x")
60 %!assert (fullfile ("", "foo"), "foo") 68 %!assert (fullfile ("", "x"), "x")
61 %!assert (fullfile ("foo", ""), "foo") 69 %!assert (fullfile ("x", ""), "x")
62 %!assert (fullfile ("", "foo", ""), "foo") 70 %!assert (fullfile ("", "x", ""), "x")
63 %!assert (fullfile ("foo", "bar"), strcat ("foo", filesep (), "bar")) 71 %!assert (fullfile ("x", "y"), xfsy)
64 %!assert (fullfile ("foo", "", "bar"), strcat ("foo", filesep (), "bar")) 72 %!assert (fullfile ("x", "", "y"), xfsy)
65 %!assert (fullfile ("foo", "", "bar", ""), strcat ("foo", filesep (), "bar")) 73 %!assert (fullfile ("x", "", "y", ""), xfsy)
66 %!assert (fullfile ("", "foo", "", "bar", ""), strcat ("foo", filesep (), "bar")) 74 %!assert (fullfile ("", "x", "", "y", ""), xfsy)
75 %!assert (fullfile (fs), fs)
76 %!assert (fullfile (fs, fs), fs)
77 %!assert (fullfile (fs, "x"), fsx)
78 %!assert (fullfile (fs, xfs), fsxfs)
79 %!assert (fullfile (fsx, fs), fsxfs)
80 %!assert (fullfile (fs, "x", fs), fsxfs)