comparison scripts/miscellaneous/fullfile.m @ 19186:1111d2d5ff95

fullfile.m: add support for multiple paths with cell array for last component.
author Carnë Draug <carandraug@octave.org>
date Mon, 29 Sep 2014 19:52:14 +0100
parents 8ca827e18349
children 8cc4a9bb253b
comparison
equal deleted inserted replaced
19185:8ca827e18349 19186:1111d2d5ff95
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{filename} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file}) 20 ## @deftypefn {Function File} {@var{filepath} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file})
21 ## @deftypefnx {Function File} {@var{filepaths} =} fullfile (@dots{}, @var{files})
21 ## Build complete filename from separate parts. 22 ## Build complete filename from separate parts.
22 ## 23 ##
23 ## Joins any number of path components intelligently. The return value 24 ## Joins any number of path components intelligently. The return value
24 ## is the concatenation of each component with exactly one file separator 25 ## is the concatenation of each component with exactly one file separator
25 ## between each non empty part. 26 ## between each non empty part.
27 ##
28 ## If the last component part is a cell array, returns a cell array of
29 ## filepaths, one for each element in the last component, e.g.:
30 ##
31 ## @example
32 ## @group
33 ## fullfile ("/home/username", "data", @{"f1.csv", "f2.csv", "f3.csv"@})
34 ## @result{} /home/username/data/f1.csv
35 ## /home/username/data/f2.csv
36 ## /home/username/data/f3.csv
37 ## @end group
38 ## @end example
26 ## 39 ##
27 ## @seealso{fileparts} 40 ## @seealso{fileparts}
28 ## @end deftypefn 41 ## @end deftypefn
29 42
30 ## Author: Carnë Draug <carandraug@octave.org> 43 ## Author: Carnë Draug <carandraug@octave.org>
31 44
32 function filename = fullfile (varargin) 45 function filename = fullfile (varargin)
33 46
34 non_empty = cellfun ("isempty", varargin); 47 if (nargin && iscell (varargin{end}))
35 filename = strjoin (varargin(! non_empty), filesep); 48 filename = cellfun (@(x) fullfile (varargin{1:end-1}, x), varargin{end},
36 filename(strfind (filename, [filesep filesep])) = ""; 49 "UniformOutput", false);
50 else
51 non_empty = cellfun ("isempty", varargin);
52 filename = strjoin (varargin(! non_empty), filesep);
53 filename(strfind (filename, [filesep filesep])) = "";
54 endif
37 55
38 endfunction 56 endfunction
39 57
40 58
41 %!shared fs, fsx, xfs, fsxfs, xfsy 59 %!shared fs, fsx, xfs, fsxfs, xfsy
68 %!assert (fullfile ("/", "a/", "/", "/", "b", "/", "/"), "/a/b/") 86 %!assert (fullfile ("/", "a/", "/", "/", "b", "/", "/"), "/a/b/")
69 %!assert (fullfile ("/a/", "/", "/", "b", "/", "/"), "/a/b/") 87 %!assert (fullfile ("/a/", "/", "/", "b", "/", "/"), "/a/b/")
70 88
71 ## different on purpose so that "fullfile (c{:})" works for empty c 89 ## different on purpose so that "fullfile (c{:})" works for empty c
72 %!assert (fullfile (), "") 90 %!assert (fullfile (), "")
91
92 %!assert (fullfile ("a", "b", {"c", "d"}), {"a/b/c", "a/b/d"})