'=================================
'SEARCH USING ONE OR MORE KEYWORDS
'=================================
'when all the keywords are found within close range
'of each other, the region of text is displayed
string cr,tab
cr=chr(13)+chr(10)
tab=chr(9)
function sample(string s, sys a,l,*b,*e)
'=======================================
b=a-l
if b<1 then b=1
e=a+l
end function
function display(string s,sys a)
'===============================
sys b,e
sample s,a,200,b,e
print a cr cr mid s,b,e-b
end function
function search(string s, sys pk, sys n) as sys
'==============================================
sys b,c,d,e,n,p,t
if not s then return 0
if pk=0 then return 0
n=1
p=pk
string k at p 'array of keywords
'
NewSearch:
'
a=instr n,s,k 'first keyword
n=a+1
t=0
if a then
sample s,a,200,b,e
'
'the other keywords nearby
'
do
p+=4 'advance pointer for next keyword string
if ++t>n then return a 'max keywords
if not k then return a 'no more keywords
c=instr b,s,k
if c=0 or c>e then
p=pk : goto NewSearch
end if
end do
end if
end function
function getword(string s, sys *i) as string
'===========================================
sys a,j,le
le=len s
j=i
i--
do
i++
if i>le then exit do
a=asc s,i
if a=44 then continue do 'comma
if a>32 then exit do 'non white space
end do
'
j=i
'
do
if i>le then exit do
a=asc s,i
if a=44 then exit do 'comma
if a<=32 then exit do 'white space
i++
end do
'
if i>j then return mid s,j,i-j
'
end function
function wordlist(string s, sys pk, sys n) as sys
'================================================
sys c,i
i=1
string k at pk 'string array
do
k=getword s,i
if not k then exit do
if ++c>=n then exit do 'max list length
pk+=4 'advance string array pointer
end do
return c
end function
function splitstring(string s, string d, sys pk, sys n) as sys
'=============================================================
'
'itr not sensitive to quoted text
'
sys a,b,c,i,ld,ls,ex
string k at pk 'string array
ls=len s : if ls=0 then return 0
ld=len d : if ld=0 then return 0
b=1
do
a=instr b,s,d
if a=0 then
a=ls+1
ex=1
end if
k=mid s,b,a-b
if ++c>=n then return c
if ex then return c
b=a+ld
pk+=4 'advance pointer for next string element
end do
end function
'======
'TEST
'======
sys a,c
string f="notes.txt"
string s=lcase getfile f
if not s then
print f " file not found"
jmp fwd done
end if
'
def maxwords 100
string k[maxwords]
c=wordlist "gabriel faur", @k, maxwords
if a:=search s,@k,c then
display s,a
end if
'
done:
'====