-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoxmatch.PRG
243 lines (182 loc) · 4.72 KB
/
foxmatch.PRG
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "foxref.h"
#include "foxpro.h"
* object we use to populate our collection of Matches
DEFINE CLASS Match AS Custom
Name = "Match"
MatchPos = 0
MatchLineNo = 0
MatchLen = 0
ENDDEFINE
* Abtract Match Engine Class
DEFINE CLASS MatchEngine AS Custom
Name = "MatchEngine"
Pattern = ''
PatternLen = 0
MatchCase = .T.
WholeWordsOnly = .F.
oMatches = .NULL.
nMatchCnt = 0
FUNCTION InitEngine()
RETURN .T.
ENDFUNC
FUNCTION Execute(cText AS String) AS Number
RETURN 0
ENDFUNC
FUNCTION CreateMatch()
LOCAL oMatch
m.oMatch = CREATEOBJECT("Empty")
ADDPROPERTY(m.oMatch, "MatchOffset", 0)
ADDPROPERTY(m.oMatch, "MatchPos", 0)
ADDPROPERTY(m.oMatch, "MatchLineNo", 0)
ADDPROPERTY(m.oMatch, "MatchLen", 0)
RETURN m.oMatch
ENDFUNC
* utility function for dermining if Whole Word
FUNCTION IsAlphaNumeric(ch)
RETURN ISALPHA(ch) OR BETWEEN(ch, '0', '9') OR ch == '_'
ENDFUNC
FUNCTION SetPattern(cPattern)
IF VARTYPE(cPattern) <> 'C'
RETURN .F.
ENDIF
THIS.PatternLen = LENC(m.cPattern)
THIS.Pattern = cPattern
RETURN .T.
ENDFUNC
ENDDEFINE
* Standard Match Engine
DEFINE CLASS MatchDefault AS MatchEngine
Name = "MatchDefault"
FUNCTION Execute(cText AS String)
LOCAL nPos
LOCAL nOccurrence
LOCAL i
LOCAL oMatch
nOccurrence = 1
i = 0
THIS.oMatches = CREATEOBJECT("Collection")
IF THIS.MatchCase
nPos = AT_C(THIS.Pattern, cText, nOccurrence)
ELSE
nPos = ATCC(THIS.Pattern, cText, nOccurrence)
ENDIF
* don't bother converting CR/LF to LF only unless there are matches
IF nPos > 0
cText = STRTRAN(cText, CHR(13) + CHR(10), CHR(10))
cText = STRTRAN(cText, CHR(13), CHR(10))
IF THIS.MatchCase
nPos = AT_C(THIS.Pattern, cText, nOccurrence)
ELSE
nPos = ATCC(THIS.Pattern, cText, nOccurrence)
ENDIF
ENDIF
DO WHILE nPos <> 0
* if Whole Word Only is selected, then we must
* make sure the word we found comprises a whole word
IF (!THIS.WholeWordsOnly OR (m.nPos > 1 AND !THIS.IsAlphaNumeric(SUBSTRC(m.cText, m.nPos - 1, 1)) AND (m.nPos == LENC(m.cText) OR !THIS.IsAlphaNumeric(SUBSTRC(m.cText, m.nPos + THIS.PatternLen, 1)))))
i = i + 1
oMatch = THIS.CreateMatch()
oMatch.MatchLen = THIS.PatternLen
oMatch.MatchPos = nPos - RATC(CHR(10), LEFTC(cText, nPos))
oMatch.MatchLineNo = OCCURS(CHR(10), LEFTC(cText, nPos)) + 1
THIS.oMatches.Add(oMatch)
ENDIF
nOccurrence = nOccurrence + 1
IF THIS.MatchCase
nPos = AT_C(THIS.Pattern, cText, nOccurrence)
ELSE
nPos = ATCC(THIS.Pattern, cText, nOccurrence)
ENDIF
ENDDO
THIS.nMatchCnt = i
RETURN THIS.nMatchCnt
ENDFUNC
ENDDEFINE
* Regular Expression search engine (relies on VBScript)
DEFINE CLASS MatchWildcard AS MatchEngine
Name = "MatchWildcard"
oRegExpr = .NULL.
FUNCTION InitEngine()
LOCAL lSuccess
m.lSuccess = .T.
TRY
THIS.oRegExpr = CreateObject("VBScript.RegExp")
THIS.oRegExpr.Global = .T.
CATCH
m.lSuccess = .F.
THIS.oRegExpr = .NULL.
MessageBox(WSH_REQUIRED_LOC, MB_ICONEXCLAMATION, APPNAME_LOC)
ENDTRY
RETURN m.lSuccess
ENDFUNC
FUNCTION SetPattern(cPattern)
LOCAL lSuccess
LOCAL oException
IF VARTYPE(cPattern) <> 'C'
RETURN .F.
ENDIF
IF EMPTY(cPattern)
RETURN .T.
ENDIF
lSuccess = .T.
IF THIS.WholeWordsOnly
cPattern = "\b" + cPattern + "\b"
ENDIF
TRY
THIS.oRegExpr.Pattern = cPattern
CATCH TO oException
m.lSuccess = .F.
ENDTRY
* now check the pattern
TRY
THIS.oRegExpr.Test("abcdefg")
CATCH TO oException
m.lSuccess = .F.
ENDTRY
IF lSuccess
THIS.PatternLen = LENC(m.cPattern)
THIS.Pattern = cPattern
ENDIF
RETURN lSuccess
ENDFUNC
FUNCTION Execute(cText AS String)
LOCAL oMatches
LOCAL oMatch
LOCAL oRegExprMatch
LOCAL nPos
LOCAL lSuccess
LOCAL oException
LOCAL nOccurs
LOCAL cFindChar
THIS.oMatches = CREATEOBJECT("Collection")
THIS.nMatchCnt = 0
lSuccess = .T.
TRY
THIS.oRegExpr.IgnoreCase = !THIS.MatchCase
THIS.oRegExpr.MultiLine = .T.
oMatches = THIS.oRegExpr.Execute(cText)
CATCH TO oException
lSuccess = .F.
ENDTRY
IF m.lSuccess
THIS.nMatchCnt = oMatches.Count
FOR EACH oRegExprMatch IN oMatches
nPos = oRegExprMatch.FirstIndex + 1
oMatch = THIS.CreateMatch()
oMatch.MatchLen = LENC(oRegExprMatch.Value)
cFindChar = CHR(10)
nOccurs = OCCURS(cFindChar, LEFTC(cText, nPos))
IF nOccurs == 0
cFindChar = CHR(13)
nOccurs = OCCURS(cFindChar, LEFTC(cText, nPos))
ENDIF
oMatch.MatchLineNo = nOccurs + 1
oMatch.MatchPos = nPos - RATC(cFindChar, LEFTC(cText, nPos))
THIS.oMatches.Add(oMatch)
ENDFOR
ELSE
RETURN -1 && error
ENDIF
RETURN THIS.nMatchCnt
ENDFUNC
ENDDEFINE