-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvepdspointer.pro
138 lines (92 loc) · 3.74 KB
/
resolvepdspointer.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Returns path to file if found, else return ''
function rpp_test, testdir, upperAndLowerBasename
ps = PATH_SEP()
FOR i=0L,N_ELEMENTS(upperAndLowerBasename)-1L DO BEGIN
fnpath = testdir + ps + upperAndLowerBasename[i]
IF NOT FILE_TEST(fnpath,/READ) THEN CONTINUE
IF FILE_TEST(fnpath,/REGULAR) THEN RETURN,fnpath
ENDFOR
RETURN, ''
END
FUNCTION resolvepdspointer,basenameArg,sourcefilepath,pdskeyword $
, debug=debug
doDebug = KEYWORD_SET(debug)
ps = PATH_SEP()
;;; Start in directory with source file that has pointer in it
testdir = FILE_DIRNAME(sourcefilepath)
;;; Build uppercase and lowercase names; put them in an array
upper = STRUPCASE(basenameArg)
lower = STRLOWCASE(basenameArg)
sbn = FILE_BASENAME(sourcefilepath)
ulbn = sbn EQ strupcase(sbn) ? [upper,lower] : [lower,upper]
;;; Test if pointer file is in same dir with source file
rtn = rpp_test(testdir,ulbn)
IF rtn THEN RETURN, rtn
;;; If not, determine name of subdir to look at for pointer file
;;; (default=document) in both upper and lower case
IF KEYWORD_SET(pdskeyword) THEN BEGIN
CASE pdskeyword[0] OF
'^STRUCTURE': tmp = 'label'
'^CATALOG': tmp = 'catalog'
'^DATA_SET_MAP_PROJECTION': tmp = 'catalog*'
'^INDEX_TABLE': tmp = 'index'
ELSE: tmp = 'document'
ENDCASE
subdirup = STRUPCASE(tmp)
subdirlo = STRLOWCASE(tmp)
IF doDebug THEN help, ulbn , upper , subdirup,subdirlo ,subdirlo,subdirup
subdirSave = ulbn[0] EQ upper ? [subdirup,subdirlo] : [subdirlo,subdirup]
ENDIF ELSE BEGIN
;;; Empty string means use no subdir
subdirSave = ''
ENDELSE
;;; Save representation of PWD; will be converted to absolute later
dot = file_dirname('') ;;; Convert relative dir . to absolute
if doDebug then print,'subdirSave:',subdirSave
;;; Save directories which were already tried, and recursively
;;; ascend parent directories, looking for file
testeddirs = [testdir]
WHILE 1b DO BEGIN
;;; Go up one directory level from last dir and try again
testdir = FILE_DIRNAME(testeddirs[0])
IF testdir EQ dot THEN BEGIN
;;; Convert dot to absolute path if and when we get there
testdir = (file_expand_path(dot))[0]
ENDIF ELSE BEGIN
;;; Exit loop if we have checked this dir before
iw = where( testeddirs EQ testdir, iwct)
IF iwct GT 0L THEN BREAK
ENDELSE
;;; Add this testdir to testeddirs so we do not test it again
testeddirs = [testdir,testeddirs]
if doDebug then print,'testeddirs:',testeddirs
;;; Loop over possible subdirs (e.g. [LABEL, label])
FOR iDir=0L,N_ELEMENTS(subdirSave)-1L DO BEGIN
subdir = subdirSave[iDir]
IF subdir EQ '' THEN BEGIN
;;; If subdir is empty string, only check in testdir
dirsToTest = [testdir]
ENDIF ELSE BEGIN
;;; Find subdirs that match subdir, store in dirsToTest array
dirsToTest = FILE_SEARCH(testdir + ps + subdir)
IF dirsToTest[0] EQ '' THEN CONTINUE
;;; Filter dirsToTest:
FOR iTest=0L,N_ELEMENTS(dirsToTest)-1L DO BEGIN
;;; Any results that are not directories are excluded by
;;; setting their array element to ''
IF NOT FILE_TEST(dirsToTest[iTest],/DIR) THEN dirsToTest[iTest] = ''
ENDFOR ;;; dirsToTest
;;; Only keep the non-empty subdirs; CONTINUE to next subdirSave
iw = WHERE( dirsToTest NE '', iwct)
IF iwct EQ 0L THEN CONTINUE
dirsToTest = dirsToTest[iw]
ENDELSE
FOR iTest=0L,N_ELEMENTS(dirsToTest)-1L DO BEGIN
rtn = rpp_test(dirsToTest[iTest],ulbn)
IF rtn THEN RETURN, rtn
ENDFOR
ENDFOR ;;; subdirSave
ENDWHILE
RETURN, ''
END