Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Boolean Search",300,300,250,200, %WS_OverlappedWindow To hDlg
Control Add TextBox, hDlg, 300, "This is the big string of words to search", 10,10,200,20
Control Add TextBox, hDlg, 400, "is and little", 10,40,200,20
Control Add CheckBox, hDlg, 500, "Exact Case Match", 10,70,200,20
Control Add Button, hDlg, 100,"Search", 50,100,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local sTerm$, sMain$, ExactCaseMatch&
Select Case Cb.Msg
Case %WM_INITDIALOG
PostMessage GetDlgItem(hDlg, 300), %EM_SETSEL, -1, 0
Case %WM_Command
Select Case Cb.Ctl
Case 100
Control Get Text hDlg, 400 To sTerm$
Control Get Text hDlg, 300 To sMain$
Control Get Check hDlg, 500 To ExactCaseMatch&
? Str$(BooleanSearch(sTerm$, sMain$, ExactCaseMatch&)) '0=no match 1=match
End Select
End Select
End Function
Function BooleanSearch(ByVal sTerm$, ByVal sMain$, ExactCaseMatch&) As Long
Local i As Long, bMethod$
If InStr(LCase$(sTerm$), " or ") Then bMethod$ = " or " 'boolean OR search
If InStr(LCase$(sTerm$), " and ") Then bMethod$ = " and " 'boolean AND search
If ExactCaseMatch& = 0 Then sMain$ = LCase$(sMain$) : sTerm$ = LCase$(sTerm$) 'upper/lower case as appropriate
'create array of search terms (ignore and/AND, or/OR)
If bMethod$ = " or " Or bMethod$ = " and " Then
While InStr(sTerm$, " ") : Replace " " With " " In sTerm$ : Wend 'remove double spaces
sTerm$ = Trim$(sTerm$) 'remove end spaces
Replace " and " With " " In sTerm$ : Replace " AND " With " " In sTerm$ 'remove and/AND
Replace " or " With " " In sTerm$ : Replace " OR " With " " In sTerm$ 'remove or/OR
ReDim T(ParseCount(sTerm$, " ")-1) As String 'create array of terms
Parse sTerm$, T(), " " 'single word in each array element
Else
ReDim T(0) As String 'non-boolean search. 1 element
T(0) = sTerm$ 'contains entire search string
End If
Select Case bMethod$ 'perform the actual search. 1=match 0=no match
Case " and "
Function = 1
For i = 0 To UBound(T)
If InStr(sMain$,T(i)) = 0 Then Function = 0 : Exit For
Next
Case Else ' OR or non-boolean search
For i = 0 To UBound(T)
If InStr(sMain$,T(i)) Then Function = 1 : Exit For
Next i
End Select
End Function
'gbs_00921
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm