Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/fileparts.m @ 18015:fe59ef0084a6
maint: Periodic merge of stable to default.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 25 Nov 2013 22:25:35 -0800 |
parents | d63878346099 |
children | e172186599ca |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17438
diff
changeset
|
1 ## Copyright (C) 2003-2013 John W. Eaton |
4264 | 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. | |
4264 | 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/>. | |
4264 | 18 |
19 ## -*- texinfo -*- | |
4350 | 20 ## @deftypefn {Function File} {[@var{dir}, @var{name}, @var{ext}, @var{ver}] =} fileparts (@var{filename}) |
4264 | 21 ## Return the directory, name, extension, and version components of |
22 ## @var{filename}. | |
5835 | 23 ## @seealso{fullfile} |
4264 | 24 ## @end deftypefn |
25 | |
26 function [directory, name, extension, version] = fileparts (filename) | |
27 | |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
28 if (nargin != 1) |
6046 | 29 print_usage (); |
4264 | 30 endif |
31 | |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
32 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
|
33 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
|
34 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
35 |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
36 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
|
37 if (isempty (ds)) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
38 ds = 0; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
39 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
40 es = rindex (filename, "."); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
41 ## 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
|
42 if (es <= ds) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
43 es = length (filename)+1; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
44 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
45 if (ds == 0) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
46 directory = ""; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
47 elseif (ds == 1) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
48 directory = filename(1); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
49 else |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
50 directory = filename(1:ds-1); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
51 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
52 name = filename(ds+1:es-1); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
53 if (es > 0 && es <= length (filename)) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
54 extension = filename(es:end); |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
55 else |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
56 extension = ""; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
57 endif |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
58 version = ""; |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
59 |
4264 | 60 endfunction |
8201
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
61 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
62 |
8201
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
63 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
64 %! [d, n, e] = fileparts ("file"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
65 %! 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
|
66 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
67 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
68 %! [d, n, e] = fileparts ("file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
69 %! 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
|
70 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
71 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
72 %! [d, n, e] = fileparts ("/file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
73 %! 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
|
74 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
75 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
76 %! [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
|
77 %! 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
|
78 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
79 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
80 %! [d, n, e] = fileparts ("./file.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
81 %! 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
|
82 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
83 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
84 %! [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
|
85 %! 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
|
86 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
87 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
88 %! [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
|
89 %! 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
|
90 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
91 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
92 %! [d, n, e] = fileparts ("/.ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 %! assert (strcmp (d, "/") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext")); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
94 |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
95 %!test |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
96 %! [d, n, e] = fileparts (".ext"); |
0ab4eed59455
fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
97 %! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext")); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 |
17438
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
99 %% Test input validation |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
100 %!error fileparts () |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
101 %!error fileparts (1,2) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
102 %!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
|
103 %!error <FILENAME must be a single string> fileparts (["a"; "b"]) |
791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
104 |