Mercurial > octave-antonio
annotate scripts/miscellaneous/fullfile.m @ 19697:4197fc428c7d
maint: Update copyright notices for 2015.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Feb 2015 14:19:08 -0500 |
parents | db92e7e28e1f |
children | 9fc020886ae9 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
1 ## Copyright (C) 2014-2015 Carnë Draug |
4265 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4265 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4265 | 18 |
19 ## -*- texinfo -*- | |
19214
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19186
diff
changeset
|
20 ## @deftypefn {Function File} {@var{filename} =} fullfile (@var{dir1}, @var{dir2}, @dots{}, @var{file}) |
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19186
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{filenames} =} fullfile (@dots{}, @var{files}) |
19185
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
22 ## Build complete filename from separate parts. |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
23 ## |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
24 ## Joins any number of path components intelligently. The return value |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
25 ## is the concatenation of each component with exactly one file separator |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
26 ## between each non empty part and at most one leading and/or trailing file |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
27 ## separator. |
19185
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
28 ## |
19186
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
29 ## If the last component part is a cell array, returns a cell array of |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
30 ## filepaths, one for each element in the last component, e.g.: |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
31 ## |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
32 ## @example |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
33 ## @group |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
34 ## fullfile ("/home/username", "data", @{"f1.csv", "f2.csv", "f3.csv"@}) |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
35 ## @result{} /home/username/data/f1.csv |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
36 ## /home/username/data/f2.csv |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
37 ## /home/username/data/f3.csv |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
38 ## @end group |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
39 ## @end example |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
40 ## |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
41 ## On Windows systems, while forward slash file separators do work, they |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
42 ## are replaced by backslashes; in addition drive letters are stripped of |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
43 ## leading file separators to obtain a valid file path. |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
44 ## |
19214
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19186
diff
changeset
|
45 ## @seealso{fileparts, filesep} |
4265 | 46 ## @end deftypefn |
47 | |
19185
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
48 ## Author: Carnë Draug <carandraug@octave.org> |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
49 |
4265 | 50 function filename = fullfile (varargin) |
51 | |
19186
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
52 if (nargin && iscell (varargin{end})) |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
53 filename = cellfun (@(x) fullfile (varargin{1:end-1}, x), varargin{end}, |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
54 "UniformOutput", false); |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
55 else |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
56 non_empty = cellfun ("isempty", varargin); |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
57 if (ispc && ! isempty (varargin)) |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
58 varargin = strrep (varargin, "/", filesep); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
59 varargin(1) = regexprep (varargin{1}, '[\\/]*([a-zA-Z]:[\\/]*)', "$1"); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
60 endif |
19186
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
61 filename = strjoin (varargin(! non_empty), filesep); |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
62 filename(strfind (filename, [filesep filesep])) = ""; |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
63 endif |
4265 | 64 |
65 endfunction | |
7288 | 66 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
67 |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
68 %!shared fs, fsx, xfs, fsxfs, xfsy, xfsyfs |
7514
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
69 %! fs = filesep (); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
70 %! fsx = [fs "x"]; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
71 %! xfs = ["x" fs]; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
72 %! fsxfs = [fs "x" fs]; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
73 %! xfsy = ["x" fs "y"]; |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
74 %! xfsyfs = ["x" fs "y" fs]; |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
75 |
7288 | 76 %!assert (fullfile (""), "") |
7514
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
77 %!assert (fullfile (fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
78 %!assert (fullfile ("", fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
79 %!assert (fullfile (fs, ""), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
80 %!assert (fullfile ("", fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
81 %!assert (fullfile ("x"), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
82 %!assert (fullfile ("", "x"), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
83 %!assert (fullfile ("x", ""), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
84 %!assert (fullfile ("", "x", ""), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
85 %!assert (fullfile ("x", "y"), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
86 %!assert (fullfile ("x", "", "y"), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
87 %!assert (fullfile ("x", "", "y", ""), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
88 %!assert (fullfile ("", "x", "", "y", ""), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
89 %!assert (fullfile (fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
90 %!assert (fullfile (fs, fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
91 %!assert (fullfile (fs, "x"), fsx) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
92 %!assert (fullfile (fs, xfs), fsxfs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
93 %!assert (fullfile (fsx, fs), fsxfs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
94 %!assert (fullfile (fs, "x", fs), fsxfs) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
96 %!assert (fullfile ("x/", "/", "/", "y", "/", "/"), xfsyfs) |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
97 %!assert (fullfile ("/", "x/", "/", "/", "y", "/", "/"), [fs xfsyfs]) |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
98 %!assert (fullfile ("/x/", "/", "/", "y", "/", "/"), [fs xfsyfs]) |
19185
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
99 |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
100 ## different on purpose so that "fullfile (c{:})" works for empty c |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
101 %!assert (fullfile (), "") |
19186
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19185
diff
changeset
|
102 |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
103 %!assert (fullfile ("x", "y", {"c", "d"}), {[xfsyfs "c"], [xfsyfs "d"]}) |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
104 |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
105 %% Windows specific - drive letters and file sep type |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
106 %!test |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
107 %! if (ispc) |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
108 %! assert (fullfile ('\/\/\//A:/\/\', "x/", "/", "/", "y", "/", "/"), ... |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
109 %! ['A:\' xfsyfs]); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
110 %! endif |
19214
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19186
diff
changeset
|
111 |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
112 %% Windows specific - drive letters and file sep type, cell array |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
113 %!test |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19564
diff
changeset
|
114 %! if (ispc) |
19564
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
115 %! tmp = fullfile ({"\\\/B:\//", "A://c", "\\\C:/g/h/i/j\/"}); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
116 %! assert (tmp{1}, 'B:\'); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
117 %! assert (tmp{2}, 'A:\c'); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
118 %! assert (tmp{3}, 'C:\g\h\i\j\'); |
c304687571c8
fullfile.m: properly process file separators and drive letters on Windows systems (bug #43926)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19214
diff
changeset
|
119 %! endif |