Author Topic: The Markup Class  (Read 2854 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
The Markup Class
« on: March 30, 2011, 07:05:39 AM »

This is the basis for a class handling data in simple markup notation. It is intended for small databases with tree structures.

Code: [Select]

  '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

efgee

  • Guest
Re: The Markup Class
« Reply #1 on: November 02, 2011, 12:27:56 PM »
Charles,
the last print command should show:

Code: [Select]
found: jour!

Bonjour!

but shows:

Code: [Select]
found: jour!

<greeting>Bonjour!</>

Thought that the data should show up without the markup stuff...

Am I wrong?



Charles Pegge

  • Guest
Re: The Markup Class
« Reply #2 on: November 02, 2011, 02:08:46 PM »

Hi Frank,

This is quite a simplistic demo, but I have added some more methods here to give the desired behaviour

Code: OxygenBasic
  1.  
  2.   'THE MARKUP CLASS
  3.  '================
  4.  '
  5.  'test/demo
  6.  '
  7.  '15:41 30/03/2011
  8.  '21:36 02/11/2011
  9.  'Charles Pegge
  10.  
  11.  
  12.   global string cr=chr(13)+chr(10)
  13.  
  14.  
  15.   '=================
  16.  class MarkupObject
  17.   '=================
  18.  '
  19.  string body
  20.   sys    idx,xdx,lvl
  21.   '
  22.  '----------------------
  23.  method endbranch() as sys
  24.   '======================
  25.  '
  26.  sys a=idx,b=1,c
  27.   do
  28.     c=asc body,a
  29.     if c=0 then exit do
  30.     if c=60 then
  31.       a++ : c=asc body,a
  32.       if c=47 then
  33.         if b=1 then xdx=a-1 : return xdx
  34.         b-- : continue do
  35.       end if
  36.       b++
  37.     end if
  38.     a++
  39.   end do
  40.   end method
  41.   '
  42.  '------------------------------------
  43.  method locatei(string n,sys i) as sys
  44.   '====================================
  45.  '
  46.  idx=instr(i,body,"<" n ">")
  47.   if idx>0 { idx+=2+len n : endbranch() : return idx }
  48.   end method
  49.   '
  50.  '--------------------------------
  51.  method branch(string n) as sys
  52.   '================================
  53.  '
  54.  sys a
  55.   if idx>0
  56.     a=idx
  57.     endbranch()
  58.     if locatei n,idx then
  59.       if idx>xdx then
  60.         idx=a : return 0 'out of bounds restore idx
  61.      else
  62.         lvl++
  63.         return idx 'success
  64.      end if
  65.     else
  66.       idx=a : return 0 'not found. restore idx
  67.    end if
  68.   end if
  69.   end method
  70.   '
  71.  '----------------------
  72.  method append(string s)
  73.   '======================
  74.  '
  75.  if idx=0 then
  76.     idx=1+len body
  77.     body+=s
  78.   else
  79.     endbranch()
  80.     idx=instr xdx,body,">"
  81.     idx++
  82.     body=left(body,idx-1) cr space(lvl*2) s mid(body,idx)
  83.   end if
  84.   endbranch()
  85.   end method
  86.   '
  87.  '----------------------
  88.  method SpanInner(sys i)
  89.   '======================
  90.  '
  91.  sys a,b
  92.   idx=i : endbranch()
  93.   xdx=instr xdx,body,"<"
  94.   do
  95.     i-- : if i<1 then idx=1 : exit do
  96.     a=asc body,i
  97.     if a=0 then idx=1 : exit do
  98.     if a=62 then idx=i+1 : exit do
  99.   end do
  100.   return idx
  101.   end method
  102.  
  103.   '----------------------
  104.  method SpanOuter(sys i)
  105.   '======================
  106.  '
  107.  sys a,b
  108.   idx=i : endbranch()
  109.   xdx=1+instr xdx,body,">"
  110.   do
  111.     i-- : if i<1 then idx=1 : exit do
  112.     a=asc body,i
  113.     if a=0 then idx=1 : exit do
  114.     if a=60 then idx=i : exit do
  115.   end do
  116.   return idx
  117.   end method
  118.  
  119.   '
  120.  '-------------
  121.  'SHORT METHODS
  122.  '=============
  123.  '
  124.  method constructor(){}
  125.   '
  126.  method destructor(){}
  127.   '
  128.  method getfile(string n){body=getfile n}
  129.   '
  130.  method putfile(string n){putfile n,body}
  131.   '
  132.  method findInner(string n) as sys { sys i : i=instr body,n : if i {SpanInner i} return i }
  133.   '
  134.  method findOuter(string n) as sys { sys i : i=instr body,n : if i {SpanOuter i} return i }
  135.   '
  136.  method locate(string n) as sys { lvl=0 : return locatei n,1 }
  137.   '
  138.  method more(string n) as sys {if idx>0 { endbranch() : return locatei n,idx} }
  139.   '
  140.  method data() as string {if idx>0 { if xdx>0 { return mid (body,idx,xdx-idx) } } }
  141.   '
  142.  method data(string s){ if idx>0 { body=left(body,idx-1) s mid(body,xdx) : xdx=idx+len s } }
  143.   '
  144.  end class
  145.  
  146.  
  147.   '====
  148.  'TEST
  149.  '====
  150.  '
  151.  MarkupObject m
  152.   '
  153.  'm.getfile "m.txt"
  154.   m.body="
  155. <root>
  156.   <greeting>Hello!</>
  157. </>
  158.   "
  159.   '
  160.  print m.body
  161.   '
  162.  if m.locate "root" then
  163.     if m.branch "greeting" then
  164.       print m.data
  165.       m.data="Bonjour!"
  166.       print m.data
  167.       m.append "<greeting>Hi!</>"
  168.     end if
  169.   end if
  170.  
  171.   string key="jour!"
  172.  
  173.   if m.findInner key then
  174.     print "found: " key cr cr m.data
  175.   end if
  176.   '
  177.  print m.body
  178.  
  179.  

Charles

efgee

  • Guest
Re: The Markup Class
« Reply #3 on: November 02, 2011, 02:55:55 PM »
Thanks

Just going through the forum and search for good stuff  ;D