changeset 19220: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 b54093acb8fe
files scripts/miscellaneous/fullfile.m
diffstat 1 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/fullfile.m	Mon Sep 29 19:36:22 2014 +0100
+++ b/scripts/miscellaneous/fullfile.m	Mon Sep 29 19:52:14 2014 +0100
@@ -17,13 +17,26 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{filename} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file})
+## @deftypefn  {Function File} {@var{filepath} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file})
+## @deftypefnx {Function File} {@var{filepaths} =} fullfile (@dots{}, @var{files})
 ## Build complete filename from separate parts.
 ##
 ## Joins any number of path components intelligently.  The return value
 ## is the concatenation of each component with exactly one file separator
 ## between each non empty part.
 ##
+## If the last component part is a cell array, returns a cell array of
+## filepaths, one for each element in the last component, e.g.:
+##
+## @example
+## @group
+## fullfile ("/home/username", "data", @{"f1.csv", "f2.csv", "f3.csv"@})
+## @result{}  /home/username/data/f1.csv
+##     /home/username/data/f2.csv
+##     /home/username/data/f3.csv
+## @end group
+## @end example
+##
 ## @seealso{fileparts}
 ## @end deftypefn
 
@@ -31,9 +44,14 @@
 
 function filename = fullfile (varargin)
 
-  non_empty = cellfun ("isempty", varargin);
-  filename = strjoin (varargin(! non_empty), filesep);
-  filename(strfind (filename, [filesep filesep])) = "";
+  if (nargin && iscell (varargin{end}))
+    filename = cellfun (@(x) fullfile (varargin{1:end-1}, x), varargin{end},
+                                       "UniformOutput", false);
+  else
+    non_empty = cellfun ("isempty", varargin);
+    filename = strjoin (varargin(! non_empty), filesep);
+    filename(strfind (filename, [filesep filesep])) = "";
+  endif
 
 endfunction
 
@@ -70,3 +88,5 @@
 
 ## different on purpose so that "fullfile (c{:})" works for empty c
 %!assert (fullfile (), "")
+
+%!assert (fullfile ("a", "b", {"c", "d"}), {"a/b/c", "a/b/d"})