/
EnvironmentPaths.pbi
179 lines (155 loc) · 4.52 KB
/
EnvironmentPaths.pbi
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
; +------------------+
; | EnvironmentPaths |
; +------------------+
; | 2016.03.23 . Creation
; | 2017.05.05 . Multiple-include safe, cleaned up code
; | 2018.10.30 . Added ResetEnvironmentPath(), FindEnvironmentFile()
;-
CompilerIf (Not Defined(__EnvironmentPaths_Included, #PB_Constant))
#__EnvironmentPaths_Included = #True
CompilerIf (#PB_Compiler_IsMainFile)
EnableExplicit
CompilerEndIf
;- Constants (Public)
Enumeration
#EnvPaths_Sort = $01
#EnvPaths_NoDuplicates = $02
#EnvPaths_NoMissing = $04
EndEnumeration
;-
;- Lists (Private)
Global NewList _EnvPath.s()
;-
;- Procedures (Public)
Procedure.i ExamineEnvironmentPaths(Flags.i = #Null)
ClearList(_EnvPath())
AddElement(_EnvPath()) : _EnvPath() = GetCurrentDirectory()
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
Protected Raw.s = GetEnvironmentVariable("PATH")
If (Raw)
Protected n.i = 1 + CountString(Raw, ";")
Protected Invalid.i
Protected i.i
For i = 1 To n
Protected Path.s = StringField(Raw, i, ";")
If (Path)
If (Right(Path, 1) <> "\")
Path + "\"
EndIf
Select (FileSize(Path))
Case -2
Invalid = #False
Case -1
Invalid = Bool(Flags & #EnvPaths_NoMissing)
Default
Invalid = #True
EndSelect
If (Flags & #EnvPaths_NoDuplicates)
ForEach (_EnvPath())
If (LCase(_EnvPath()) = LCase(Path))
Invalid = #True
LastElement(_EnvPath())
Break
EndIf
Next
EndIf
If (Not Invalid)
AddElement(_EnvPath())
_EnvPath() = Path
EndIf
EndIf
Next i
If (Flags & #EnvPaths_Sort)
SortList(_EnvPath(), #PB_Sort_Ascending | #PB_Sort_NoCase)
EndIf
EndIf
CompilerElse
;? TODO: Examine paths on Unix-like systems
AddElement(_EnvPath()) : _EnvPath() = "/"
AddElement(_EnvPath()) : _EnvPath() = GetHomeDirectory()
CompilerEndIf
ResetList(_EnvPath())
ProcedureReturn (ListSize(_EnvPath()))
EndProcedure
Procedure ResetEnvironmentPath()
ResetList(_EnvPath())
EndProcedure
Procedure.i NextEnvironmentPath()
ProcedureReturn (NextElement(_EnvPath()))
EndProcedure
Procedure.s EnvironmentPath()
If (ListIndex(_EnvPath()) >= 0)
ProcedureReturn (_EnvPath())
EndIf
EndProcedure
Procedure AddEnvironmentPath(Path.s)
If (Path)
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
Protected PathList.s = ";" + GetEnvironmentVariable("Path") + ";"
Path = RTrim(ReplaceString(Path, "/", "\"), "\")
If (FindString(LCase(PathList), ";" + LCase(Path) + ";") Or
FindString(LCase(PathList), ";" + LCase(Path) + "\;"))
Else
PathList + Path + "\"
EndIf
PathList = Trim(PathList, ";")
SetEnvironmentVariable("Path", PathList)
CompilerEndIf
EndIf
EndProcedure
Procedure.s FindEnvironmentFile(Name.s)
Protected Result.s
If (ExamineEnvironmentPaths(#EnvPaths_NoDuplicates | #EnvPaths_NoMissing))
While (NextEnvironmentPath())
If (FileSize(EnvironmentPath() + Name) >= 0)
Result = EnvironmentPath() + Name
Break
EndIf
Wend
If (Result = "")
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
If (GetExtensionPart(Name) = "")
Protected ExtList.s = GetEnvironmentVariable("PATHEXT")
Protected N.i = 1 + CountString(ExtList, ";")
Protected i.i
ResetEnvironmentPath()
While (NextEnvironmentPath())
For i = 1 To N
Protected Try.s = Name + "." + LCase(Trim(StringField(ExtList, i, ";"), "."))
Try = EnvironmentPath() + Try
If (FileSize(Try) >= 0)
Result = Try
Break
EndIf
Next i
Wend
EndIf
CompilerEndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
;-
;- Demo Program
CompilerIf (#PB_Compiler_IsMainFile)
DisableExplicit
Macro Underline(_Text, _Character = "-")
(_Text + #LF$ + RSet("", 3*Len(_Text)/2, (_Character)))
EndMacro
AddEnvironmentPath("C:\MyFakePath")
Debug Underline("All Paths")
If ExamineEnvironmentPaths()
While NextEnvironmentPath()
Debug EnvironmentPath()
Wend
EndIf
Debug #LF$
Debug Underline("Sort, NoDuplicates, NoMissing")
If ExamineEnvironmentPaths(#EnvPaths_Sort | #EnvPaths_NoDuplicates | #EnvPaths_NoMissing)
While NextEnvironmentPath()
Debug EnvironmentPath()
Wend
EndIf
CompilerEndIf
CompilerEndIf
;-