This is the basis for a class handling data in simple markup notation. It is intended for small databases with tree structures.
'THE MARKUP CLASS
'================
'
'test/demo
'
'15:41 30/03/2011
'Charles Pegge
global string cr=chr(13)+chr(10)
'=================
class MarkupObject
'=================
'
string body
sys idx,xdx,lvl
'
'----------------------
method endbranch() as sys
'======================
'
sys a=idx,b=1,c
do
c=asc body,a
if c=0 then exit do
if c=60 then
a++ : c=asc body,a
if c=47 then
if b=1 then xdx=a-1 : return xdx
b-- : continue do
end if
b++
end if
a++
end do
end method
'
'------------------------------------
method locatei(string n,sys i) as sys
'====================================
'
idx=instr(i,body,"<" n ">")
if idx>0 { idx+=2+len n : endbranch() : return idx }
end method
'
'--------------------------------
method branch(string n) as sys
'================================
'
sys a
if idx>0
a=idx
endbranch()
if locatei n,idx then
if idx>xdx then
idx=a : return 0 'out of bounds restore idx
else
lvl++
return idx 'success
end if
else
idx=a : return 0 'not found. restore idx
end if
end if
end method
'
'----------------------
method append(string s)
'======================
'
if idx=0 then
idx=1+len body
body+=s
else
endbranch()
idx=instr xdx,body,">"
idx++
body=left(body,idx-1) cr space(lvl*2) s mid(body,idx)
end if
endbranch()
end method
'
'----------------------
method findbound(sys i)
'======================
'
sys a,b
idx=i : endbranch()
xdx=1+instr xdx,body,">"
do
i-- : if i<1 then idx=1 : exit do
a=asc body,i
if a=0 then idx=1 : exit do
if a=60 then idx=i : exit do
end do
return idx
end method
'
'-------------
'SHORT METHODS
'=============
'
method constructor(){}
'
method destructor(){}
'
method getfile(string n){body=getfile n}
'
method putfile(string n){putfile n,body}
'
method find(string n) as sys { sys i : i=instr body,n : if i {findbound i} return i }
'
method locate(string n) as sys { lvl=0 : return locatei n,1 }
'
method more(string n) as sys {if idx>0 { endbranch() : return locatei n,idx} }
'
method data() as string {if idx>0 { if xdx>0 { return mid (body,idx,xdx-idx) } } }
'
method data(string s){ if idx>0 { body=left(body,idx-1) s mid(body,xdx) : xdx=idx+len s } }
'
end class
'====
'TEST
'====
'
MarkupObject m
'
'm.getfile "m.txt"
m.body="
<root>
<greeting>Hello!</>
</>
"
'
print m.body
'
if m.locate "root" then
if m.branch "greeting" then
print m.data
m.data="Bonjour!"
print m.data
m.append "<greeting>Hi!</>"
end if
end if
string key="jour!"
if m.find key then
print "found: " key cr cr m.data
end if
'
print m.body
Charles