Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/what.m @ 18060:427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
* what.m: Check for both flavors of file separator at end of input dir name and
remove if found.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 03 Dec 2013 10:49:01 -0800 |
parents | d63878346099 |
children | ff05d5b70411 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1 ## Copyright (C) 2007-2013 David Bateman |
7050 | 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 -*- | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Command} {} what |
7050 | 21 ## @deftypefnx {Command} {} what @var{dir} |
22 ## @deftypefnx {Function File} {w =} what (@var{dir}) | |
12668
e3dc23f7dd54
doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
23 ## List the Octave specific files in directory @var{dir}. If @var{dir} is |
e3dc23f7dd54
doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
24 ## not specified then the current directory is used. If a return argument is |
e3dc23f7dd54
doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
25 ## requested, the files found are returned in the structure @var{w}. |
7050 | 26 ## @seealso{which} |
27 ## @end deftypefn | |
28 | |
29 function ret = what (d) | |
30 | |
31 if (nargin == 0) | |
32 d = pwd (); | |
33 else | |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 dtmp = canonicalize_file_name (d); |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 if (isempty (dtmp)) |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## Search for directory name in path |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
37 if (d(end) == '/' || d(end) == '\') |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 d(end) = []; |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
39 endif |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 dtmp = find_dir_in_path (d); |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 if (isempty (dtmp)) |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 error ("what: could not find the directory %s", d); |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 endif |
7050 | 44 endif |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 d = dtmp; |
7050 | 46 endif |
47 | |
48 files = dir (d); | |
49 w.path = d; | |
50 w.m = cell (0, 1); | |
51 w.mex = cell (0, 1); | |
52 w.oct = cell (0, 1); | |
53 w.mat = cell (0, 1); | |
54 w.mdl = cell (0, 1); | |
55 w.p = cell (0, 1); | |
56 w.classes = cell (0, 1); | |
57 | |
58 for i = 1 : length (files) | |
59 n = files(i).name; | |
60 ## Ignore . and .. | |
61 if (strcmp (n, ".") || strcmp (n, "..")) | |
62 continue; | |
63 else | |
64 ## Ignore mdl and p files | |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 [~, f, e] = fileparts (n); |
7050 | 66 if (strcmp (e, ".m")) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 w.m{end+1} = n; |
7050 | 68 elseif (strcmp (e, ".oct")) |
10549 | 69 w.oct{end+1} = n; |
17386
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
70 elseif (strcmp (e, mexext ())) |
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
71 w.mex{end+1} = n; |
7050 | 72 elseif (strcmp (e, ".mat")) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
73 w.mat{end+1} = n; |
17386
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
74 elseif (strcmp (n(1), "@")) |
10549 | 75 w.classes{end+1} = n; |
7050 | 76 endif |
77 endif | |
78 endfor | |
79 | |
80 if (nargout == 0) | |
81 __display_filenames__ ("M-files in directory", w.path, w.m); | |
82 __display_filenames__ ("\nMEX-files in directory", w.path, w.mex); | |
83 __display_filenames__ ("\nOCT-files in directory", w.path, w.oct); | |
84 __display_filenames__ ("\nMAT-files in directory", w.path, w.mat); | |
85 __display_filenames__ ("\nClasses in directory", w.path, w.classes); | |
86 else | |
87 ret = w; | |
88 endif | |
89 endfunction | |
90 | |
91 function __display_filenames__ (msg, p, f) | |
92 if (length (f) > 0) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
93 printf ("%s %s:\n\n", msg, p); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
94 |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12668
diff
changeset
|
95 maxlen = max (cellfun ("length", f)); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 ncols = max (1, floor (terminal_size ()(2) / (maxlen + 3))); |
7050 | 97 fmt = ""; |
98 for i = 1: ncols | |
99 fmt = sprintf ("%s %%-%ds", fmt, maxlen); | |
100 endfor | |
101 fmt = [fmt, "\n"]; | |
102 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
103 nrows = ceil (length (f) / ncols); |
7050 | 104 for i = 1 : nrows |
105 args = f(i:nrows:end); | |
106 if (length (args) < ncols) | |
10934
f6294203286e
Fix off by one error in the what function (bug #30919)
David Bateman <dbateman@free.fr>
parents:
10635
diff
changeset
|
107 args(end + 1 : ncols) = {""}; |
7050 | 108 endif |
109 printf (fmt, args{:}); | |
110 endfor | |
111 endif | |
112 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
113 |