Mercurial > hg > octave-lojdl
comparison scripts/miscellaneous/fileparts.m @ 17467:791c117eb2cf
fileparts.m: Check for multi-line char inputs (bug #40062)
* scripts/miscellaneous/fileparts.m: Validate input is not a multi-line
character matrix. Move input validation to front of function. Add %!error
input validation tests.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 18 Sep 2013 20:29:19 -0700 |
parents | 5d3a684236b0 |
children |
comparison
equal
deleted
inserted
replaced
17466:cf5a8fccfc63 | 17467:791c117eb2cf |
---|---|
23 ## @seealso{fullfile} | 23 ## @seealso{fullfile} |
24 ## @end deftypefn | 24 ## @end deftypefn |
25 | 25 |
26 function [directory, name, extension, version] = fileparts (filename) | 26 function [directory, name, extension, version] = fileparts (filename) |
27 | 27 |
28 if (nargin == 1) | 28 if (nargin != 1) |
29 if (ischar (filename)) | |
30 ds = strchr (filename, filesep ("all"), 1, "last"); | |
31 if (isempty (ds)) | |
32 ds = 0; | |
33 endif | |
34 es = rindex (filename, "."); | |
35 ## These can be the same if they are both 0 (no dir or ext). | |
36 if (es <= ds) | |
37 es = length (filename)+1; | |
38 endif | |
39 if (ds == 0) | |
40 directory = ""; | |
41 elseif (ds == 1) | |
42 directory = filename(1); | |
43 else | |
44 directory = filename(1:ds-1); | |
45 endif | |
46 name = filename(ds+1:es-1); | |
47 if (es > 0 && es <= length (filename)) | |
48 extension = filename(es:end); | |
49 else | |
50 extension = ""; | |
51 endif | |
52 version = ""; | |
53 else | |
54 error ("fileparts: expecting FILENAME argument to be a string"); | |
55 endif | |
56 else | |
57 print_usage (); | 29 print_usage (); |
58 endif | 30 endif |
31 | |
32 if (! ischar (filename) || rows (filename) > 1) | |
33 error ("fileparts: FILENAME must be a single string"); | |
34 endif | |
35 | |
36 ds = strchr (filename, filesep ("all"), 1, "last"); | |
37 if (isempty (ds)) | |
38 ds = 0; | |
39 endif | |
40 es = rindex (filename, "."); | |
41 ## These can be the same if they are both 0 (no dir or ext). | |
42 if (es <= ds) | |
43 es = length (filename)+1; | |
44 endif | |
45 if (ds == 0) | |
46 directory = ""; | |
47 elseif (ds == 1) | |
48 directory = filename(1); | |
49 else | |
50 directory = filename(1:ds-1); | |
51 endif | |
52 name = filename(ds+1:es-1); | |
53 if (es > 0 && es <= length (filename)) | |
54 extension = filename(es:end); | |
55 else | |
56 extension = ""; | |
57 endif | |
58 version = ""; | |
59 | 59 |
60 endfunction | 60 endfunction |
61 | 61 |
62 | 62 |
63 %!test | 63 %!test |
94 | 94 |
95 %!test | 95 %!test |
96 %! [d, n, e] = fileparts (".ext"); | 96 %! [d, n, e] = fileparts (".ext"); |
97 %! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext")); | 97 %! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext")); |
98 | 98 |
99 %% Test input validation | |
100 %!error fileparts () | |
101 %!error fileparts (1,2) | |
102 %!error <FILENAME must be a single string> fileparts (1) | |
103 %!error <FILENAME must be a single string> fileparts (["a"; "b"]) | |
104 |