1 ## Copyright (C) 2003-2012 John W. Eaton
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{dir}, @var{name}, @var{ext}, @var{ver}] =} fileparts (@var{filename})
21 ## Return the directory, name, extension, and version components of
26 function [directory, name, extension, version] = fileparts (filename)
29 if (ischar (filename))
30 ds = strchr (filename, filesep ("all"), 1, "last");
34 es = rindex (filename, ".");
35 ## These can be the same if they are both 0 (no dir or ext).
37 es = length(filename)+1;
42 directory = filename(1);
44 directory = filename(1:ds-1);
46 name = filename(ds+1:es-1);
47 if (es > 0 && es <= length (filename))
48 extension = filename(es:end);
54 error ("fileparts: expecting FILENAME argument to be a string");
63 %! [d, n, e] = fileparts ("file");
64 %! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ""));
67 %! [d, n, e] = fileparts ("file.ext");
68 %! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ".ext"));
71 %! [d, n, e] = fileparts ("/file.ext");
72 %! assert (strcmp (d, "/") && strcmp (n, "file") && strcmp (e, ".ext"));
75 %! [d, n, e] = fileparts ("dir/file.ext");
76 %! assert (strcmp (d, "dir") && strcmp (n, "file") && strcmp (e, ".ext"));
79 %! [d, n, e] = fileparts ("./file.ext");
80 %! assert (strcmp (d, ".") && strcmp (n, "file") && strcmp (e, ".ext"));
83 %! [d, n, e] = fileparts ("d1/d2/file.ext");
84 %! assert (strcmp (d, "d1/d2") && strcmp (n, "file") && strcmp (e, ".ext"));
87 %! [d, n, e] = fileparts ("/d1/d2/file.ext");
88 %! assert (strcmp (d, "/d1/d2") && strcmp (n, "file") && strcmp (e, ".ext"));
91 %! [d, n, e] = fileparts ("/.ext");
92 %! assert (strcmp (d, "/") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));
95 %! [d, n, e] = fileparts (".ext");
96 %! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));