Mercurial > octave
annotate scripts/miscellaneous/fileparts.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## Copyright (C) 2003-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
5 ## |
4264 | 6 ## |
7 ## This file is part of Octave. | |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
4264 | 10 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
11 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## (at your option) any later version. |
4264 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
4264 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
7016 | 20 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
21 ## <https://www.gnu.org/licenses/>. |
4264 | 22 |
23 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
24 ## @deftypefn {} {[@var{dir}, @var{name}, @var{ext}] =} fileparts (@var{filename}) |
19205 | 25 ## Return the directory, name, and extension components of @var{filename}. |
26 ## | |
27 ## The input @var{filename} is a string which is parsed. There is no attempt | |
28 ## to check whether the filename or directory specified actually exists. | |
19214
8cc4a9bb253b
fullfile.m: Match documentation name to function variable name.
Rik <rik@octave.org>
parents:
19205
diff
changeset
|
29 ## @seealso{fullfile, filesep} |
4264 | 30 ## @end deftypefn |
31 | |
19205 | 32 function [dir, name, ext] = fileparts (filename) |
4264 | 33 |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
34 if (nargin != 1) |
6046 | 35 print_usage (); |
4264 | 36 endif |
37 | |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
38 if (! ischar (filename) || rows (filename) > 1) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
39 error ("fileparts: FILENAME must be a single string"); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
40 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
41 |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
42 ds = strchr (filename, filesep ("all"), 1, "last"); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
43 if (isempty (ds)) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
44 ds = 0; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
45 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
46 es = rindex (filename, "."); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
47 ## These can be the same if they are both 0 (no dir or ext). |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
48 if (es <= ds) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
49 es = length (filename)+1; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
50 endif |
19205 | 51 |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
52 if (ds == 0) |
19205 | 53 dir = ""; |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
54 elseif (ds == 1) |
19205 | 55 dir = filename(1); |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
56 else |
19205 | 57 dir = filename(1:ds-1); |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
58 endif |
19205 | 59 |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
60 name = filename(ds+1:es-1); |
19205 | 61 if (isempty (name)) |
62 name = ""; | |
63 endif | |
64 | |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
65 if (es > 0 && es <= length (filename)) |
19205 | 66 ext = filename(es:end); |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
67 else |
19205 | 68 ext = ""; |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
69 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
70 |
4264 | 71 endfunction |
8201
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
72 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 |
8201
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
74 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
75 %! [d, n, e] = fileparts ("file"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
76 %! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, "")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
77 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
78 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
79 %! [d, n, e] = fileparts ("file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
80 %! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
81 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
82 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
83 %! [d, n, e] = fileparts ("/file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
84 %! assert (strcmp (d, "/") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
85 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
86 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
87 %! [d, n, e] = fileparts ("dir/file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
88 %! assert (strcmp (d, "dir") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
89 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
90 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
91 %! [d, n, e] = fileparts ("./file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
92 %! assert (strcmp (d, ".") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
94 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
95 %! [d, n, e] = fileparts ("d1/d2/file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
96 %! assert (strcmp (d, "d1/d2") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
97 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
98 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
99 %! [d, n, e] = fileparts ("/d1/d2/file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
100 %! assert (strcmp (d, "/d1/d2") && strcmp (n, "file") && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
101 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
103 %! [d, n, e] = fileparts ("/.ext"); |
19205 | 104 %! assert (strcmp (d, "/") && strcmp (n, "") && strcmp (e, ".ext")); |
8201
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
105 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
106 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 %! [d, n, e] = fileparts (".ext"); |
19205 | 108 %! assert (strcmp (d, "") && strcmp (n, "") && strcmp (e, ".ext")); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
110 ## Test input validation |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
111 %!error fileparts () |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
112 %!error fileparts (1,2) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
113 %!error <FILENAME must be a single string> fileparts (1) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
114 %!error <FILENAME must be a single string> fileparts (["a"; "b"]) |