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

Last change on this file since 239 was 239, checked in by smasson, 17 years ago

cleaning + minor bugfix related to the path definition

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