annotate scripts/miscellaneous/fileparts.m @ 11933:205f10f5cbe8 release-3-0-x

more fixes to fileparts.m tests
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 14 Feb 2009 21:15:23 +0100
parents 8d1427a14f45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2003, 2004, 2005, 2006, 2007 John W. Eaton
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
2 ##
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
4 ##
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
9 ##
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
14 ##
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
18
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
4350
b4661459eff7 [project @ 2003-02-21 19:29:43 by jwe]
jwe
parents: 4266
diff changeset
20 ## @deftypefn {Function File} {[@var{dir}, @var{name}, @var{ext}, @var{ver}] =} fileparts (@var{filename})
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
21 ## Return the directory, name, extension, and version components of
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
22 ## @var{filename}.
5835
07dd13bfc0ba [project @ 2006-05-31 18:21:49 by jwe]
jwe
parents: 5443
diff changeset
23 ## @seealso{fullfile}
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
24 ## @end deftypefn
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
25
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
26 function [directory, name, extension, version] = fileparts (filename)
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
27
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
28 if (nargin == 1)
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
29 if (ischar (filename))
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
30 if !( findstr (octave_config_info('canonical_host_type'), 'msvc') || findstr (octave_config_info('canonical_host_type'), 'mingw') )
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
31 ds = rindex (filename, filesep);
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
32 else
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
33 ds = max (rindex (filename, "\\"),rindex (filename, "/"));
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
34 endif
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
35 es = rindex (filename, ".");
5073
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
36 ## These can be the same if they are both 0 (no dir or ext).
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
37 if (es <= ds)
4683
a3a306af7292 [project @ 2003-12-23 05:48:02 by jwe]
jwe
parents: 4424
diff changeset
38 es = length(filename)+1;
a3a306af7292 [project @ 2003-12-23 05:48:02 by jwe]
jwe
parents: 4424
diff changeset
39 endif
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
40 if (ds == 0)
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
41 directory = "";
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
42 elseif (ds == 1)
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
43 directory = filename(1);
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
44 else
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
45 directory = filename(1:ds-1);
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
46 endif
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
47 name = filename(ds+1:es-1);
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
48 if (es > 0 && es <= length (filename))
5073
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
49 extension = filename(es:end);
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
50 else
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
51 extension = "";
e71be9c548f2 [project @ 2004-11-09 18:31:25 by jwe]
jwe
parents: 4683
diff changeset
52 endif
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
53 version = "";
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
54 else
4424
ab82d3a27d96 [project @ 2003-06-13 14:35:25 by jwe]
jwe
parents: 4350
diff changeset
55 error ("fileparts: expecting filename argument to be a string");
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
56 endif
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
57 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5835
diff changeset
58 print_usage ();
4264
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
59 endif
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
60
4e2d2516da22 [project @ 2003-01-03 05:30:34 by jwe]
jwe
parents:
diff changeset
61 endfunction
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
62
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
63 %!test
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
64 %! [d, n, e] = fileparts ("file");
e0229beb02c9 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, ""));
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
66
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
67 %!test
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
68 %! [d, n, e] = fileparts ("file.ext");
e0229beb02c9 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"));
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
70
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
71 %!test
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
72 %! [d, n, e] = fileparts ([filesep "file.ext"]);
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
73 %! assert (strcmp (d, filesep) && strcmp (n, "file") && strcmp (e, ".ext"));
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
74
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
75 %!test
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
76 %! [d, n, e] = fileparts (["dir" filesep "file.ext"]);
11873
e0229beb02c9 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"));
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
78
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
79 %!test
11932
8d1427a14f45 fix typo in last patch
Jaroslav Hajek <highegg@gmail.com>
parents: 11931
diff changeset
80 %! [d, n, e] = fileparts (["." filesep "file.ext"]);
11873
e0229beb02c9 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"));
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
82
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
83 %!test
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
84 %! [d, n, e] = fileparts (["d1" filesep "d2" filesep "file.ext"]);
11933
205f10f5cbe8 more fixes to fileparts.m tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11932
diff changeset
85 %! assert (strcmp (d, ["d1" filesep "d2"]) && strcmp (n, "file") && strcmp (e, ".ext"));
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
86
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
87 %!test
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
88 %! [d, n, e] = fileparts ([filesep "d1" filesep "d2" filesep "file.ext"]);
11933
205f10f5cbe8 more fixes to fileparts.m tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11932
diff changeset
89 %! assert (strcmp (d, [filesep "d1" filesep "d2"]) && strcmp (n, "file") && strcmp (e, ".ext"));
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
90
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
91 %!test
11931
d7d30988b3ea fix fileparts tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11873
diff changeset
92 %! [d, n, e] = fileparts ([filesep ".ext"]);
11933
205f10f5cbe8 more fixes to fileparts.m tests
Jaroslav Hajek <highegg@gmail.com>
parents: 11932
diff changeset
93 %! assert (strcmp (d, filesep) && strcmp (n, char (zeros (1, 0))) && strcmp (e, ".ext"));
11873
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
94
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
95 %!test
e0229beb02c9 fileparts.m: handle "/file" correctly; improve compatibilty
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
96 %! [d, n, e] = fileparts (".ext");
e0229beb02c9 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"));