Mercurial > octave-nkf
annotate scripts/miscellaneous/fullfile.m @ 19248:8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
* fullfile.m: Match documentation name to function variable name. Add seealso
link to filesep.
* fileparts.m: Change seealso link to filesep.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 04 Oct 2014 20:06:27 -0700 |
parents | 1111d2d5ff95 |
children | c304687571c8 |
rev | line source |
---|---|
19219
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2014 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 -*- | |
19248
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19220
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:
19220
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{filenames} =} fullfile (@dots{}, @var{files}) |
19219
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 |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
26 ## between each non empty part. |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
27 ## |
19220
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
28 ## 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:
19219
diff
changeset
|
29 ## 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:
19219
diff
changeset
|
30 ## |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
31 ## @example |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
32 ## @group |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
33 ## 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:
19219
diff
changeset
|
34 ## @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:
19219
diff
changeset
|
35 ## /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:
19219
diff
changeset
|
36 ## /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:
19219
diff
changeset
|
37 ## @end group |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
38 ## @end example |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
39 ## |
19248
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19220
diff
changeset
|
40 ## @seealso{fileparts, filesep} |
4265 | 41 ## @end deftypefn |
42 | |
19219
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
43 ## Author: Carnë Draug <carandraug@octave.org> |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
44 |
4265 | 45 function filename = fullfile (varargin) |
46 | |
19220
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
47 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:
19219
diff
changeset
|
48 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:
19219
diff
changeset
|
49 "UniformOutput", false); |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
50 else |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
51 non_empty = cellfun ("isempty", varargin); |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
52 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:
19219
diff
changeset
|
53 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:
19219
diff
changeset
|
54 endif |
4265 | 55 |
56 endfunction | |
7288 | 57 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 |
7514
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
59 %!shared fs, fsx, xfs, fsxfs, xfsy |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
60 %! fs = filesep (); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
61 %! fsx = [fs "x"]; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
62 %! xfs = ["x" fs]; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
63 %! 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
|
64 %! xfsy = ["x" fs "y"]; |
7288 | 65 %!assert (fullfile (""), "") |
7514
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
66 %!assert (fullfile (fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
67 %!assert (fullfile ("", fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
68 %!assert (fullfile (fs, ""), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
69 %!assert (fullfile ("", fs), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
70 %!assert (fullfile ("x"), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
71 %!assert (fullfile ("", "x"), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
72 %!assert (fullfile ("x", ""), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
73 %!assert (fullfile ("", "x", ""), "x") |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
74 %!assert (fullfile ("x", "y"), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
75 %!assert (fullfile ("x", "", "y"), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
76 %!assert (fullfile ("x", "", "y", ""), xfsy) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
77 %!assert (fullfile ("", "x", "", "y", ""), xfsy) |
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), fs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
80 %!assert (fullfile (fs, "x"), fsx) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
81 %!assert (fullfile (fs, xfs), fsxfs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
82 %!assert (fullfile (fsx, fs), fsxfs) |
4f6a73fd8df9
fullfile: improve handling of args ending with filesep
John W. Eaton <jwe@octave.org>
parents:
7288
diff
changeset
|
83 %!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
|
84 |
19219
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
85 %!assert (fullfile ("a/", "/", "/", "b", "/", "/"), "a/b/") |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
86 %!assert (fullfile ("/", "a/", "/", "/", "b", "/", "/"), "/a/b/") |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
87 %!assert (fullfile ("/a/", "/", "/", "b", "/", "/"), "/a/b/") |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
88 |
8ca827e18349
fullfile: complete rewrite of the function.
Carnë Draug <carandraug@octave.org>
parents:
17744
diff
changeset
|
89 ## 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
|
90 %!assert (fullfile (), "") |
19220
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
91 |
1111d2d5ff95
fullfile.m: add support for multiple paths with cell array for last component.
Carnë Draug <carandraug@octave.org>
parents:
19219
diff
changeset
|
92 %!assert (fullfile ("a", "b", {"c", "d"}), {"a/b/c", "a/b/d"}) |
19248
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19220
diff
changeset
|
93 |