source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/lightspeed/glob.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1function [names,isdirs] = glob(pattern,prefix)
2%GLOB   Filename expansion via wildcards.
3% GLOB(PATTERN) returns a cell array of file/directory names which match the
4% PATTERN. 
5% [NAMES,ISDIRS] = GLOB(PATTERN) also returns a logical vector indicating
6% which are directories.
7%
8% Two types of wildcards are supported:
9% * matches zero or more characters, besides /
10% ** matches zero or more characters, including /, ending with a /
11% *** is interpreted as ** followed by *, which means it matches zero or
12% more characters, including /
13%
14% For example, 'a*b' matches 'ab','acb','acdb', but not 'a/b'.
15% 'a**b' matches 'ab','a/b','ac/b','ac/d/b', but not 'acb' or 'a/cb'.
16% 'a***b' matches all of the above plus 'a/cb','ac/d/cb',etc.
17%
18% 'a//b' is not considered a valid filename, so 'a/*/b' will not return
19% 'a//b' or 'a/b'.
20%
21% Examples:
22%   % if 'work' is a subdirectory, this returns only 'work', not its contents:
23%   glob('work')
24%   % returns 'work/fun.m' (not 'fun.m'):
25%   glob('work/fun.m')
26%   % all m-files in 'work', prefixed with 'work/':
27%   glob('work/*.m')
28%   % all files named 'fun.m' in 'work' or any subdirectory of 'work':
29%   glob('work/**fun.m')
30%   % all m-files in 'work' or any subdirectory of 'work':
31%   glob('work/***.m')
32%   % all files named 'fun.m' any subdirectory of 'work' (but not 'work'):
33%   glob('work/**/fun.m')
34%   % all files named 'fun.m' in any subdirectory of '.':
35%   glob('**fun.m')
36%   % all files in all subdirectories:
37%   glob('***')
38%
39% See also globstrings.
40
41% Written by Tom Minka, 28-Apr-2004
42% (c) Microsoft Corporation. All rights reserved.
43
44if nargin < 2
45  prefix = '';
46end
47
48names = {};
49isdirs = [];
50if isempty(pattern)
51  return
52end
53
54% break the pattern into path components
55[first,rest] = strtok(pattern,'/');
56
57i = strfind(first,'**');
58if ~isempty(i)
59  % double-star pattern
60  i = i(1); % process first occurrence
61  rest = fullfile(first((i+2):end),rest);
62  first = first(1:(i-1));
63  new_pattern = [first rest];
64  % if the pattern was 'a**b/c', new_pattern is 'ab/c'
65  [names,isdirs] = glob(new_pattern,prefix);
66  first = [first '*'];
67  rest = ['**' rest];
68  % if the pattern was 'a**b/c', it is now 'a*/**b/c'
69end
70
71% expand the first component
72fullfirst = fullfile(prefix,first);
73if ~iswild(fullfirst) & isdir(fullfirst)
74  first_files = struct('name',first,'isdir',1);
75else
76  first_files = stripdots(dir(fullfirst));
77end
78% for each match, add it to the results or recurse on the rest of the pattern
79for i = 1:length(first_files)
80  new_prefix = fullfile(prefix,first_files(i).name);
81  if isempty(rest)
82    names{end+1} = new_prefix;
83    isdirs(end+1) = first_files(i).isdir;
84  elseif first_files(i).isdir
85    [new_names, new_isdirs] = glob(rest,new_prefix);
86    names = cellcat(names, new_names);
87    isdirs = [isdirs; new_isdirs];
88  end
89end
90names = names(:);
91isdirs = isdirs(:);
92
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94
95function files = stripdots(files)
96% omit . and .. from the results of DIR
97
98names = {files.name};
99ok = (~strcmp(names,'.') & ~strcmp(names,'..'));
100files = files(ok);
101
102function c = cellcat(c,c2)
103
104c = {c{:} c2{:}};
105
106function tf = iswild(pattern)
107
108tf = ~isempty(strfind(pattern,'*'));
109
110function s = regexp_quote(s)
111
112regexprep(s,'[!#]^','#^');
113regexprep(s,'[!#]$','#$');
Note: See TracBrowser for help on using the repository browser.