Mercurial > octave-nkf
changeset 19219:8ca827e18349
fullfile: complete rewrite of the function.
* fullfile.m: complete rewrite of the function for sake of simpler code
(function is down to 3 lines). Also fixes corner case where a filesep
was used in path parts sequentially, e.g., fullfile ("a/", "/", "/", "b").
Removed input check for when there is no input arguments so that an empty
cell array can be used like "fullfile (empty_cell_array{:})".
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Mon, 29 Sep 2014 19:36:22 +0100 |
parents | e0a7718ac085 |
children | 1111d2d5ff95 |
files | scripts/miscellaneous/fullfile.m |
diffstat | 1 files changed, 18 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/miscellaneous/fullfile.m Sun Sep 28 21:43:51 2014 -0400 +++ b/scripts/miscellaneous/fullfile.m Mon Sep 29 19:36:22 2014 +0100 @@ -1,4 +1,4 @@ -## Copyright (C) 2003-2013 John W. Eaton +## Copyright (C) 2014 Carnë Draug ## ## This file is part of Octave. ## @@ -18,38 +18,22 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {@var{filename} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file}) -## Return a complete filename constructed from the given components. +## 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. +## ## @seealso{fileparts} ## @end deftypefn +## Author: Carnë Draug <carandraug@octave.org> + function filename = fullfile (varargin) - if (nargin > 0) - ## Discard all empty arguments - varargin(cellfun ("isempty", varargin)) = []; - nargs = numel (varargin); - if (nargs > 1) - filename = varargin{1}; - if (strcmp (filename(end), filesep)) - filename(end) = ""; - endif - for i = 2:nargs - tmp = varargin{i}; - if (i < nargs && strcmp (tmp(end), filesep)) - tmp(end) = ""; - elseif (i == nargs && strcmp (tmp, filesep)) - tmp = ""; - endif - filename = [filename filesep tmp]; - endfor - elseif (nargs == 1) - filename = varargin{1}; - else - filename = ""; - endif - else - print_usage (); - endif + non_empty = cellfun ("isempty", varargin); + filename = strjoin (varargin(! non_empty), filesep); + filename(strfind (filename, [filesep filesep])) = ""; endfunction @@ -80,3 +64,9 @@ %!assert (fullfile (fsx, fs), fsxfs) %!assert (fullfile (fs, "x", fs), fsxfs) +%!assert (fullfile ("a/", "/", "/", "b", "/", "/"), "a/b/") +%!assert (fullfile ("/", "a/", "/", "/", "b", "/", "/"), "/a/b/") +%!assert (fullfile ("/a/", "/", "/", "b", "/", "/"), "/a/b/") + +## different on purpose so that "fullfile (c{:})" works for empty c +%!assert (fullfile (), "")