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

Last change on this file since 136 was 136, checked in by pinsard, 18 years ago

some improvements and corrections in some .pro file according to
aspell and idldoc log file

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