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

Last change on this file since 277 was 260, checked in by pinsard, 17 years ago

only uppercase between <proidl>...</proidl> in perspective of links to /usr/local_macosx/idl/idl_6.2/idl_6.2/

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