Mercurial > octave-nkf
comparison scripts/miscellaneous/fullfile.m @ 20276:8b1884214798
fullfile.m: allow UNC (\srv\share) paths on Windows systems (bug #44682).
* fullfile.m: Use strncmp to detect '\\' at start of path and preserve UNC
portion. Recode BIST test to check for elimination of multilple file
separators to apply to non-Windows systems only.
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Mon, 25 May 2015 20:04:45 +0200 |
parents | df437a52bcaf |
children |
comparison
equal
deleted
inserted
replaced
20275:35fc5ea83030 | 20276:8b1884214798 |
---|---|
40 ## | 40 ## |
41 ## On Windows systems, while forward slash file separators do work, they are | 41 ## On Windows systems, while forward slash file separators do work, they are |
42 ## replaced by backslashes; in addition drive letters are stripped of leading | 42 ## replaced by backslashes; in addition drive letters are stripped of leading |
43 ## file separators to obtain a valid file path. | 43 ## file separators to obtain a valid file path. |
44 ## | 44 ## |
45 ## Note: @code{fullfile} does not perform any validation of the resulting full | |
46 ## filename. | |
45 ## @seealso{fileparts, filesep} | 47 ## @seealso{fileparts, filesep} |
46 ## @end deftypefn | 48 ## @end deftypefn |
47 | 49 |
48 ## Author: Carnë Draug <carandraug@octave.org> | 50 ## Author: Carnë Draug <carandraug@octave.org> |
49 | 51 |
52 if (nargin && iscell (varargin{end})) | 54 if (nargin && iscell (varargin{end})) |
53 filename = cellfun (@(x) fullfile (varargin{1:end-1}, x), varargin{end}, | 55 filename = cellfun (@(x) fullfile (varargin{1:end-1}, x), varargin{end}, |
54 "UniformOutput", false); | 56 "UniformOutput", false); |
55 else | 57 else |
56 non_empty = cellfun ("isempty", varargin); | 58 non_empty = cellfun ("isempty", varargin); |
59 unc = 0; | |
57 if (ispc && ! isempty (varargin)) | 60 if (ispc && ! isempty (varargin)) |
58 varargin = strrep (varargin, "/", filesep); | 61 varargin = strrep (varargin, '/', filesep); |
62 unc = strncmp (varargin{1}, '\\', 2); | |
59 varargin(1) = regexprep (varargin{1}, '[\\/]*([a-zA-Z]:[\\/]*)', "$1"); | 63 varargin(1) = regexprep (varargin{1}, '[\\/]*([a-zA-Z]:[\\/]*)', "$1"); |
60 endif | 64 endif |
61 filename = strjoin (varargin(! non_empty), filesep); | 65 filename = strjoin (varargin(! non_empty), filesep); |
62 filename(strfind (filename, [filesep filesep])) = ""; | 66 filename(unc + strfind (filename(1+unc : end), [filesep filesep])) = ""; |
63 endif | 67 endif |
64 | 68 |
65 endfunction | 69 endfunction |
66 | 70 |
67 | 71 |
85 %!assert (fullfile ("x", "y"), xfsy) | 89 %!assert (fullfile ("x", "y"), xfsy) |
86 %!assert (fullfile ("x", "", "y"), xfsy) | 90 %!assert (fullfile ("x", "", "y"), xfsy) |
87 %!assert (fullfile ("x", "", "y", ""), xfsy) | 91 %!assert (fullfile ("x", "", "y", ""), xfsy) |
88 %!assert (fullfile ("", "x", "", "y", ""), xfsy) | 92 %!assert (fullfile ("", "x", "", "y", ""), xfsy) |
89 %!assert (fullfile (fs), fs) | 93 %!assert (fullfile (fs), fs) |
90 %!assert (fullfile (fs, fs), fs) | |
91 %!assert (fullfile (fs, "x"), fsx) | 94 %!assert (fullfile (fs, "x"), fsx) |
92 %!assert (fullfile (fs, xfs), fsxfs) | 95 %!assert (fullfile (fs, xfs), fsxfs) |
93 %!assert (fullfile (fsx, fs), fsxfs) | 96 %!assert (fullfile (fsx, fs), fsxfs) |
94 %!assert (fullfile (fs, "x", fs), fsxfs) | 97 %!assert (fullfile (fs, "x", fs), fsxfs) |
95 | 98 |
107 %! if (ispc) | 110 %! if (ispc) |
108 %! assert (fullfile ('\/\/\//A:/\/\', "x/", "/", "/", "y", "/", "/"), ... | 111 %! assert (fullfile ('\/\/\//A:/\/\', "x/", "/", "/", "y", "/", "/"), ... |
109 %! ['A:\' xfsyfs]); | 112 %! ['A:\' xfsyfs]); |
110 %! endif | 113 %! endif |
111 | 114 |
115 ## *nix specific - double backslash | |
116 %!test | |
117 %! if (isunix || ismac) | |
118 %! assert (fullfile (fs, fs), fs) | |
119 %! endif | |
120 | |
112 ## Windows specific - drive letters and file sep type, cell array | 121 ## Windows specific - drive letters and file sep type, cell array |
113 %!test | 122 %!test |
114 %! if (ispc) | 123 %! if (ispc) |
115 %! tmp = fullfile ({"\\\/B:\//", "A://c", "\\\C:/g/h/i/j\/"}); | 124 %! tmp = fullfile ({"\\\/B:\//", "A://c", "\\\C:/g/h/i/j\/"}); |
116 %! assert (tmp{1}, 'B:\'); | 125 %! assert (tmp{1}, 'B:\'); |