changelog shortlog tags changeset files revisions annotate raw

scripts/miscellaneous/fileparts.m

changeset 10289: 4b124317dc38
parent:cbd6545b0d85
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (43 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
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
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
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
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
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
22## @var{filename}.
23## @seealso{fullfile}
24## @end deftypefn
25
26function [directory, name, extension, version] = fileparts (filename)
27
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 ();
58 endif
59
60endfunction
61
62%!test
63%! [d, n, e] = fileparts ("file");
64%! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ""));
65
66%!test
67%! [d, n, e] = fileparts ("file.ext");
68%! assert (strcmp (d, "") && strcmp (n, "file") && strcmp (e, ".ext"));
69
70%!test
71%! [d, n, e] = fileparts ("/file.ext");
72%! assert (strcmp (d, "/") && strcmp (n, "file") && strcmp (e, ".ext"));
73
74%!test
75%! [d, n, e] = fileparts ("dir/file.ext");
76%! assert (strcmp (d, "dir") && strcmp (n, "file") && strcmp (e, ".ext"));
77
78%!test
79%! [d, n, e] = fileparts ("./file.ext");
80%! assert (strcmp (d, ".") && strcmp (n, "file") && strcmp (e, ".ext"));
81
82%!test
83%! [d, n, e] = fileparts ("d1/d2/file.ext");
84%! assert (strcmp (d, "d1/d2") && strcmp (n, "file") && strcmp (e, ".ext"));
85
86%!test
87%! [d, n, e] = fileparts ("/d1/d2/file.ext");
88%! assert (strcmp (d, "/d1/d2") && strcmp (n, "file") && strcmp (e, ".ext"));
89
90%!test
91%! [d, n, e] = fileparts ("/.ext");
92%! assert (strcmp (d, "/") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));
93
94%!test
95%! [d, n, e] = fileparts (".ext");
96%! assert (strcmp (d, "") && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));