source: trunk/SRC/Utilities/find.pro @ 378

Last change on this file since 378 was 375, checked in by pinsard, 16 years ago

improvements of headers (paragraphs and alignments)

  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1;+
2;
3; @file_comments
4; based on <proidl>FILE_SEARCH</proidl>, but it is possible to specify
5; a set of possibles names and a different set of
6; possibles directories names.
7;
8; By default look for files included in !path
9;
10; all <proidl>FILE_SEARCH</proidl> keywords can be used.
11;
12; @categories
13; find a file
14;
15; @param FILEIN {in}{required} {type=scalar string or array of strings}
16; File name[s] to match. Input names specifications may contain
17; wildcard characters, enabling them to match multiple files
18; (see <proidl>FILE_SEARCH</proidl> for more informations). By default and if
19; necessary, <pro>find</pro> is looking for filename and also for filename
20; completed with '.pro'
21;
22; @keyword FIRSTFOUND
23; activate this keyword to stop looking for the file as soon as we
24; found one. Return a scalar string containing the first file found
25;
26; @keyword IODIRECTORY {type=scalar string or array of strings} {default=['.',!path]}
27; Directories names where we are looking for the file.
28; Different directories can be separated by
29; path_sep(/search_path) (':' on unix type machine) as it is done
30; to define !path.
31; Note that if filename's dirname is different from '.', this
32; keyword is not taken into account.
33;
34; @keyword LOOKALLDIR
35; activate to look for the file (with a recursive search if needed)
36; in . iodir, homedir, !path + the DATA:TestsData directory if it exists.
37;
38; @keyword NOPRO
39; activate to avoid the automatic search of filename completed with '.pro'
40;
41; @keyword ONLYPRO
42; force to look only at file ending with .pro
43;
44; @keyword ONLYNC
45; force to look only at file ending with .nc
46;
47; @keyword RECURSIVE
48; performs recursive searching of directory hierarchies.
49; In a recursive search, <pro>find</pro> looks recursively for any and all
50; subdirectories in the file hierarchy rooted at the IODIRECTORY argument.
51;
52; @keyword REPERTOIRE
53; obsolete. keep for compatibility, use IODIRECTORY keyword
54;
55; @keyword UNIQUE
56; activate to make sure that each element of the output vector is unique.
57;
58; @keyword TRYFIND
59; if the file was not found and this keyword is activated, <pro>find</pro>
60; will call
61; itself with the keywords /LOOKALLDIR and /FIRSTFOUND to try to find
62; the file we are looking for. Note that if the file was found at the
63; first try this keyword as no effect (which is not the case with LOOKALLDIR)
64;
65; @keyword _EXTRA
66; Used to pass keywords
67;
68; @returns
69; A scalar or array variable of string type, containing the
70; name (with the full path of the matching files. If no files
71; exist with names matching the input arguments, <pro>find</pro> returns
72; the scalar string : 'NOT FOUND'
73;
74; @examples
75;
76;   IDL> print, find('*loadct')
77;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
78;   /usr/local/rsi/idl_6.0/lib/loadct.pro
79;   IDL> print, find('*loadct', iodir=!dir,/recursive)
80;   /usr/local/rsi/idl_6.0/lib/loadct.pro
81;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
82;   IDL> print, find('*loadct.pro')
83;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
84;   /usr/local/rsi/idl_6.0/lib/loadct.pro
85;   IDL> print, find('*loadct',/nopro)
86;   NOT FOUND
87;   IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib')
88;   /usr/local/rsi/idl_6.0/lib/loadct.pro
89;   IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /test_write)
90;   NOT FOUND
91;   IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /recursive)
92;   /usr/local/rsi/idl_6.0/lib/loadct.pro
93;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
94;   IDL> print, find('mesh*', iodirectory = [iodir, !path])
95;   /Users/sebastie/DATA/ORCA2/meshmaskORCA2closea.nc
96;   /Users/sebastie/IDL/meshmaskclosesea.pro
97;   /Users/sebastie/IDL/meshmaskclosesea.pro~
98;   /Users/sebastie/SAXO_RD/Obsolete/meshlec.pro
99;   /usr/local/rsi/idl_6.0/lib/mesh_obj.pro
100;
101; @history
102; Sebastien Masson (smasson\@lodyc.jussieu.fr)
103;  - 28/4/1999
104;  - 6/7/1999: compatibility mac and windows
105;  - June 2005: Sebastien Masson: cleaning, use for file_* functions
106;
107; @version
108; $Id$
109;
110;-
111FUNCTION find, filein, IODIRECTORY=iodirectory, RECURSIVE=recursive $
112             , REPERTOIRE=repertoire, NOPRO=nopro, ONLYPRO=onlypro $
113             , ONLYNC=onlync, UNIQUE=unique, FIRSTFOUND=firstfound $
114             , LOOKALLDIR=LOOKALLDIR, TRYFIND=tryfind, _EXTRA=ex
115;
116  compile_opt idl2, strictarrsubs
117;
118; define where we look for the file
119  cd, current = current
120  current = current + path_sep()
121  CASE 1 OF
122    keyword_set(lookalldir):BEGIN
123@cm_general
124      dirnames = [current, iodir, homedir, !path]
125      tstdtadir= file_dirname(find('find', /onlypro), /mark_directory)
126      parent = path_sep(/parent_directory)+path_sep()
127      tstdtadir = (file_search(tstdtadir+parent+parent+'DATA/TestsData'))[0]
128      IF tstdtadir NE '' THEN dirnames = [tstdtadir, dirnames]
129    END
130    keyword_set(iodirectory): dirnames = iodirectory
131    keyword_set(repertoire): dirnames = repertoire
132    ELSE: dirnames = [current, !path]
133  ENDCASE
134  tmp = dirnames
135  dirnames = 'dummy'
136  FOR i = 0, n_elements(tmp)-1 DO $
137    dirnames = [dirnames, strsplit(tmp[i], path_sep(/search_path), /extract)]
138  dirnames = dirnames[1:*]
139;
140  fileout = 'dummy'
141  FOR i = 0, n_elements(filein)-1 DO BEGIN
142    dir = file_dirname(filein[i])
143    base = file_basename(filein[i])
144; try to complete the file name with .pro or .nc if needed...
145    CASE 1 OF
146      keyword_set(onlypro):BEGIN
147        promiss = strpos(base, '.pro', /reverse_search)
148        promiss = promiss - (strlen(base) - 4)
149        bad = where(promiss NE 0 OR strlen(base) LE 4, cnt)
150        IF cnt NE 0 THEN base[bad] = base[bad] + '.pro'
151      end
152      keyword_set(onlync):BEGIN
153        ncmiss = strpos(base, '.nc', /reverse_search)
154        ncmiss = ncmiss - (strlen(base) - 3)
155        bad = where(ncmiss NE 0 OR strlen(base) LE 3, cnt)
156        IF cnt NE 0 THEN base[bad] = base[bad] + '.nc'
157      END
158      ELSE:if strmid(base, 0, 1, /reverse_offset) NE '*' $
159        AND NOT keyword_set(nopro) THEN base = base + '{.pro,}'
160    ENDCASE
161; use dirnames only if dir eq '.'
162    IF dir EQ  '.' THEN BEGIN
163      if keyword_set(recursive) THEN $
164        found = file_search(dirnames, base, _extra = ex) $
165        ELSE found = file_search(dirnames + '/' + base, _extra = ex)
166    ENDIF ELSE found = file_search(dir + '/' + base, _extra = ex)
167    IF found[0] NE '' THEN BEGIN
168      IF keyword_set(firstfound) THEN return, found[0]
169      fileout = [fileout, found]
170    ENDIF
171  ENDFOR
172
173  IF n_elements(fileout) EQ 1 THEN fileout = 'NOT FOUND' $
174  ELSE fileout = fileout[1:*]
175;
176  IF n_elements(fileout) GT 1 THEN BEGIN
177    IF keyword_set(unique) THEN fileout = fileout[uniq(fileout, sort(fileout))]
178  ENDIF ELSE fileout = fileout[0]
179;
180  IF keyword_set(lookalldir) AND fileout[0] EQ 'NOT FOUND' $
181     AND NOT keyword_set(recursive) THEN $
182        filout = find(file_basename(filein[0]), /lookalldir $
183                      , /recursive, _extra = ex)
184;
185  IF keyword_set(tryfind) AND fileout[0] EQ 'NOT FOUND' THEN BEGIN
186    fileout = find(file_basename(filein[0]), /lookalldir, /firstfound, _extra = ex)
187    fileout = fileout[0]
188  ENDIF
189;
190  RETURN, fileout
191END
Note: See TracBrowser for help on using the repository browser.