Advertisement
jargon

TIMOTHY2

Jun 16th, 2013
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Timothy2.bas
  2. 'Advanced "Timothy" associative array FreeBASIC source code 0x001
  3. '
  4. 'Created December 22nd 2009 - May 6th 2010 by T.R.Keal
  5. 'Modified June 15th 2013 by T.R.Keal
  6. 'Released under the Gnu Public License 2.0
  7. '
  8. 'Please review the Gnu Public License, Thank you.
  9. 'The GPL can be located online at http://www.gnu.org/copyleft/gpl.html
  10.  
  11.  
  12. type TIMOTHY_NODE
  13.     written as byte
  14.     address as unsigned long
  15. end type
  16. type TIMOTHY_CELL
  17.     leaf as TIMOTHY_NODE
  18.     child(0 to 255) as TIMOTHY_NODE
  19.     prior as TIMOTHY_NODE
  20.     latter as TIMOTHY_NODE
  21. end type
  22.  
  23. sub TIMOTHY_CHKTREE(tree() as TIMOTHY_CELL, address as unsigned long)
  24.     if tree(address).leaf.written then
  25.        
  26.     endif
  27.    
  28. end sub
  29.  
  30. redim as TIMOTHY_CELL Tree()
  31.    
  32.    
  33.  
  34. const TIMOTHY_ACCESS_RESET=1         'bit 0, yes reset error
  35. const TIMOTHY_ACCESS_READ=2          'bit 1, yes read
  36. const TIMOTHY_ACCESS_ALLOCATION=4    'bit 2, yes allocation
  37. const TIMOTHY_ACCESS_WRITE=8         'bit 3, yes write
  38. const TIMOTHY_ACCESS_OVERWRITE=16    'bit 4, yes overwrite
  39. const TIMOTHY_ACCESS_DELETE=32       'bit 5, yes delete
  40. const TIMOTHY_ACCESS_DEBUG=64        'bit 6, yes debug
  41. const TIMOTHY_ACCESS_TRACE=128       'bit 7, yes trace
  42. 'const TIMOTHY_ACCESS_DUMP=256        'bit 8, yes dump
  43.  
  44. const TIMOTHY_MEMORY_BASELINE=8      'lower limit of node doubling as power of two
  45. const TIMOTHY_MEMORY_PREFIX=chr(1)   'label prefiX
  46.  
  47. const TIMOTHY_VALIDATE_ZERO=1        'bit 0, zero capacity
  48. const TIMOTHY_VALIDATE_ALLOCATION=2  'bit 1, within allocated capacity
  49. const TIMOTHY_VALIDATE_WRITTEN=4     'bit 2, within used capacity
  50. const TIMOTHY_VALIDATE_CAPACITY=8    'bit 4, equal to used capacity
  51.  
  52. const TIMOTHY_FORK_WRITTEN=15        'bits 0..3, fork is-written states
  53. const TIMOTHY_FORK_ADDRESS=240       'bits 4..7, forked address is-valid states
  54.  
  55. const TIMOTHY_IS_WRITTEN=1           'bit 0, is-written state
  56. const TIMOTHY_IS_VALID=2             'bit 1, is-valid state (by address)
  57.  
  58. const TIMOTHY_VALUE_WRITTEN=4        'bit 2, value is-written state
  59.  
  60. const TIMOTHY_DEFAULT_ORIGIN=0       '(default) fork of origination address
  61. const TIMOTHY_DEFAULT_WRITTEN=0      '(default) is-written state
  62. const TIMOTHY_DEFAULT_ADDRESS=0      '(default) address
  63. const TIMOTHY_DEFAULT_VALUE=""       '(default) cell value
  64. const TIMOTHY_DEFAULT_RESET=0        '(default) access failmask as masked after reset
  65. const TIMOTHY_DEFAULT_INDEX=1        '(default) index for memory seek
  66.  
  67. const TIMOTHY_CODEC_DECODE=0
  68. const TIMOTHY_CODEC_ENCODE=1
  69.  
  70. type Timothy_fork_type
  71.     origin as uinteger
  72.     written as uinteger
  73.     address as uinteger
  74. end type
  75.  
  76. type Timothy_cell_type
  77.     written as uinteger
  78.     value as string
  79. end type
  80.  
  81. type Timothy_node_type
  82.     parent as Timothy_fork_type
  83.     child as Timothy_fork_type
  84.     sibling(0 to 1) as Timothy_fork_type
  85.     fork(0 to 3) as Timothy_fork_type
  86.     cell as Timothy_cell_type
  87. end type
  88.  
  89. type Timothy_queue_type
  90.     label as string
  91.     value as string
  92.     request as uinteger
  93.     waccess as uinteger
  94.     fail as uinteger
  95.     index as uinteger
  96.     octet as uinteger
  97.     biet as uinteger
  98.     ret as string
  99. end type
  100.  
  101. declare function Timothy_check_allocation(Timothy_db() as Timothy_node_type, byval i as uinteger, byval waccess as uinteger) as uinteger
  102. declare function Timothy_check_validate(Timothy_db() as Timothy_node_type, byval i as uinteger) as uinteger
  103. declare function Timothy_check_fork(node as Timothy_node_type) as uinteger
  104. declare sub Timothy_memory_ini(Timothy_db() as Timothy_node_type, byval waccess as uinteger)
  105. declare sub Timothy_memory_cpy(Timothy_db() as Timothy_node_type, src as uinteger, dst as uinteger)
  106. declare sub Timothy_memory_mov(Timothy_db() as Timothy_node_type, src as uinteger, dst as uinteger)
  107. declare function Timothy_memory_pop(Timothy_db() as Timothy_node_type, byval i as uinteger) as Timothy_node_type
  108. declare function Timothy_memory(Timothy_db() as Timothy_node_type, byval label as string, byval value as string, byval request as uinteger, byval waccess as uinteger, byref fail as uinteger, byref index as uinteger, byref octet as uinteger, byref biet as uinteger) as string
  109. declare sub Timothy_memory_save(dat() as string, filename as string)
  110. declare sub Timothy_memory_load(queue() as Timothy_queue_type, filename as string, byval request as uinteger, byval waccess as uinteger)
  111. declare sub Timothy_memory_dump(Timothy_db() as Timothy_node_type, ret() as string, label as string, byref i as uinteger, byval oo as uinteger, byref bo as uinteger)
  112. declare sub Timothy_memory_dump_loop(Timothy_db() as Timothy_node_type, ret() as string, label as string, byref index as uinteger, byval octet as uinteger, byref biet as uinteger)
  113. declare sub Timothy_memory_dump_save(dump() as string, filename as string)
  114. declare sub Timothy_memory_dump_load(dump() as string, filename as string)
  115. declare sub Timothy_memory_dump_queue(dump() as string, queue() as Timothy_queue_type)
  116. declare sub Timothy_memory_queue(Timothy_db() as Timothy_node_type,queue() as Timothy_queue_type,waccess as uinteger)
  117. declare function Timothy_codec_tag(haystack as string, direction as uinteger) as string
  118.  
  119. function Timothy_check_allocation(Timothy_db() as Timothy_node_type, byval i as uinteger, byval waccess as uinteger) as uinteger
  120.     dim as uinteger tmp=(1 shl (fix(log(cdbl(i+(1 shl TIMOTHY_MEMORY_BASELINE)))/log(2))+1))-1
  121.     if waccess and TIMOTHY_ACCESS_ALLOCATION then
  122.         if ubound(Timothy_db,1)<>tmp then
  123.             redim preserve Timothy_db(0 to tmp)
  124.         endif
  125.     endif
  126.     return tmp
  127. end function
  128.  
  129. function Timothy_check_validate(Timothy_db() as Timothy_node_type, byval i as uinteger) as uinteger
  130.     dim as uinteger ret=0
  131.     ret=ret or (TIMOTHY_VALIDATE_ZERO and (ubound(Timothy_db,1)=0))
  132.     ret=ret or (TIMOTHY_VALIDATE_ALLOCATION and (i<=ubound(Timothy_db,1)))
  133.     ret=ret or (TIMOTHY_VALIDATE_WRITTEN and (i<=Timothy_db(0).parent.written))
  134.     ret=ret or (TIMOTHY_VALIDATE_CAPACITY and (i=Timothy_db(0).parent.written))
  135.     return ret
  136. end function
  137.  
  138. function Timothy_check_fork(node as Timothy_node_type) as uinteger
  139.     dim as uinteger bi=0,ret=0
  140.     for bi=0 to 3
  141.         ret=ret or ((((node.fork(bi).written>0) and 1) shl bi) and TIMOTHY_FORK_WRITTEN)
  142.         ret=ret or ((((node.fork(bi).address>0) and 1) shl (bi or 4)) and TIMOTHY_FORK_ADDRESS)
  143.     next
  144.     return ret
  145. end function
  146.  
  147. sub Timothy_memory_ini(Timothy_db() as Timothy_node_type, byval waccess as uinteger)
  148.     dim as uinteger biet=0,i=0
  149.     if TIMOTHY_ACCESS_ALLOCATION=(waccess and TIMOTHY_ACCESS_ALLOCATION) then
  150.         redim Timothy_db(0 to 0) as Timothy_node_type
  151.         Timothy_db(0).parent.origin=TIMOTHY_DEFAULT_ORIGIN
  152.         Timothy_db(0).parent.written=TIMOTHY_DEFAULT_WRITTEN
  153.         Timothy_db(0).parent.address=TIMOTHY_DEFAULT_ADDRESS
  154.         for biet=0 to 3
  155.             Timothy_db(0).fork(biet).written=TIMOTHY_DEFAULT_WRITTEN
  156.             Timothy_db(0).fork(biet).address=TIMOTHY_DEFAULT_ADDRESS
  157.         next
  158.         Timothy_db(0).cell.written=TIMOTHY_DEFAULT_WRITTEN
  159.         Timothy_db(0).cell.value=TIMOTHY_DEFAULT_VALUE
  160.     endif
  161.     i=Timothy_check_allocation(Timothy_db(), 0, waccess)
  162. end sub
  163.  
  164. sub Timothy_memory_cpy(Timothy_db() as Timothy_node_type, src as uinteger, dst as uinteger)
  165.     dim as uinteger biet=0
  166.     Timothy_db(dst).parent.origin=Timothy_db(src).parent.origin
  167.     Timothy_db(dst).parent.written=Timothy_db(src).parent.written
  168.     Timothy_db(dst).parent.address=Timothy_db(src).parent.address
  169.     for biet=0 to 3
  170.         Timothy_db(dst).fork(biet).written=Timothy_db(src).fork(biet).written
  171.         Timothy_db(dst).fork(biet).address=Timothy_db(src).fork(biet).address
  172.     next
  173.     Timothy_db(dst).cell.written=Timothy_db(src).cell.written
  174.     Timothy_db(dst).cell.value=Timothy_db(src).cell.value
  175. end sub
  176.  
  177. sub Timothy_memory_mov(Timothy_db() as Timothy_node_type, src as uinteger, dst as uinteger)
  178.     dim as uinteger fp=0,fi=0
  179.     dim as Timothy_node_type dummy
  180.     if Timothy_db(src).parent.address then
  181.         Timothy_db(Timothy_db(src).parent.address).fork(Timothy_db(src).parent.origin).address=dst
  182.     endif
  183.     Timothy_memory_cpy Timothy_db(),src,dst
  184.     dummy=Timothy_memory_pop(Timothy_db(),src)
  185. end sub
  186.  
  187. function Timothy_memory_pop(Timothy_db() as Timothy_node_type, byval i as uinteger) as Timothy_node_type
  188.     dim as uinteger biet=0
  189.     dim as Timothy_node_type ret,dummy
  190.     ret=Timothy_db(i)
  191.     Timothy_db(i).parent.origin=TIMOTHY_DEFAULT_ORIGIN
  192.     Timothy_db(i).parent.written=TIMOTHY_DEFAULT_WRITTEN
  193.     Timothy_db(i).parent.address=TIMOTHY_DEFAULT_ADDRESS
  194.     for biet=0 to 3
  195.         Timothy_db(i).fork(biet).written=TIMOTHY_DEFAULT_WRITTEN
  196.         Timothy_db(i).fork(biet).address=TIMOTHY_DEFAULT_ADDRESS
  197.     next
  198.     Timothy_db(i).cell.written=TIMOTHY_DEFAULT_WRITTEN
  199.     Timothy_db(i).cell.value=TIMOTHY_DEFAULT_VALUE
  200.     if TIMOTHY_VALIDATE_CAPACITY=(TIMOTHY_VALIDATE_CAPACITY and Timothy_check_validate(Timothy_db(),i)) then
  201.         if Timothy_db(0).parent.written then
  202.             Timothy_db(0).parent.written=Timothy_db(0).parent.written-1
  203.         endif
  204.     else
  205.         Timothy_memory_mov Timothy_db(),Timothy_db(0).parent.written,i
  206.     endif    
  207.     if ret.parent.address<>i then
  208.         if (TIMOTHY_FORK_WRITTEN and Timothy_check_fork(Timothy_db(ret.parent.address)))=0 then
  209.             dummy=Timothy_memory_pop(Timothy_db(),ret.parent.address)
  210.         endif
  211.     endif
  212.     return ret
  213. end function
  214.  
  215. sub Timothy_memory_push(Timothy_db() as Timothy_node_type, byval i as uinteger, byval value as string)
  216.     'if TIMOTHY_DEFAULT_WRITTEN=(TIMOTHY_VALUE_WRITTEN and Timothy_db(i).cell.written) then
  217.     'endif
  218.     Timothy_db(i).cell.written=(Timothy_db(i).cell.written or TIMOTHY_VALUE_WRITTEN)
  219.     Timothy_db(i).cell.value=value
  220. end sub
  221.  
  222. function Timothy_memory(Timothy_db() as Timothy_node_type, byval label as string, byval value as string, byval request as uinteger, byval waccess as uinteger, byref fail as uinteger, _
  223.     byref index as uinteger, byref octet as uinteger, byref biet as uinteger) as string
  224.     dim as string label2=TIMOTHY_MEMORY_PREFIX+label,value2=value
  225.     dim as string ret=TIMOTHY_DEFAULT_VALUE     'return value
  226.     dim as ubyte bo=0,bv=0,bx=3                 'biet offset,   biet value,     max biet
  227.     dim as uinteger oo=0,ov=0,ox=len(label2)-1  'octet offset,  octet value,    max octet
  228.     dim as uinteger i=TIMOTHY_DEFAULT_INDEX     'node address
  229.     dim as uinteger pa=0,po=0                   '               parent address, parent origin
  230.     dim as uinteger t=0                         'node target
  231.     dim as uinteger si(0 to 1)                  'sibling index
  232.     dim as uinteger so(0 to 1)                  'sibling octet
  233.     dim as uinteger sb(0 to 1)      '           'sibling biet
  234.     dim as Timothy_node_type node,dummy         'node cache,    node dummy
  235.     if TIMOTHY_ACCESS_RESET=(TIMOTHY_ACCESS_RESET and waccess) then
  236.         fail=(TIMOTHY_DEFAULT_RESET and fail)
  237.     endif
  238.     ov=0
  239.     oo=0
  240.     while (fail=0) and (oo<=ox)
  241.         ov=asc(mid(label2,oo+1,1))
  242.         bv=0
  243.         bo=0
  244.         while (fail=0) and (bo<=bx)
  245.             bv=(ov shr (bo shl 1)) and 3
  246.             if TIMOTHY_VALIDATE_WRITTEN=(TIMOTHY_VALIDATE_WRITTEN and Timothy_check_validate(Timothy_db(),i)) then
  247.                 if Timothy_db(i).fork(po).written=TIMOTHY_DEFAULT_WRITTEN then
  248.                     if TIMOTHY_ACCESS_WRITE=(TIMOTHY_ACCESS_WRITE and request and waccess) then
  249.                         t=Timothy_db(0).parent.written+1
  250.                         Timothy_db(i).fork(po).written=TIMOTHY_IS_WRITTEN
  251.                         Timothy_db(i).fork(po).address=t                    
  252.                     else
  253.                         if not(TIMOTHY_ACCESS_WRITE=(TIMOTHY_ACCESS_WRITE and waccess)) then
  254.                             fail=(TIMOTHY_ACCESS_WRITE or fail)
  255.                         endif
  256.                     endif
  257.                 else
  258.                     t=Timothy_db(0).fork(po).address
  259.                 endif
  260.             else
  261.                 if TIMOTHY_ACCESS_ALLOCATION=(TIMOTHY_ACCESS_ALLOCATION and request and waccess) then
  262.                     Timothy_db(0).parent.written=i
  263.                     Timothy_check_allocation Timothy_db(),Timothy_db(0).parent.written,waccess
  264.                     Timothy_db(i).parent.address=pa
  265.                     Timothy_db(i).parent.origin=po
  266.                     Timothy_db(pa).fork(po).written=TIMOTHY_IS_WRITTEN
  267.                     Timothy_db(pa).fork(po).address=i
  268.                     t=i+1
  269.                 else
  270.                     if not(TIMOTHY_ACCESS_ALLOCATION=(TIMOTHY_ACCESS_ALLOCATION and waccess)) then
  271.                         fail=(TIMOTHY_ACCESS_ALLOCATION or fail)
  272.                     endif
  273.                 endif
  274.             endif
  275.             if (fail=0) then
  276.                 pa=i
  277.                 po=bv
  278.                 i=t
  279.                 if (bo>=bx) then
  280.                     exit while
  281.                 else
  282.                     bo=bo+1
  283.                 endif
  284.             else
  285.                 exit while
  286.             endif
  287.         wend
  288.         if (fail=0) then
  289.             if (oo>=ox) then
  290.                 exit while
  291.             else
  292.                 oo=oo+1
  293.             endif
  294.         else
  295.             exit while
  296.         endif
  297.     wend
  298.     if (TIMOTHY_ACCESS_WRITE=(TIMOTHY_ACCESS_WRITE and request and waccess)) and (fail=0) then
  299.         if TIMOTHY_VALUE_WRITTEN=(TIMOTHY_VALUE_WRITTEN and Timothy_db(i).cell.written) then
  300.             if (TIMOTHY_ACCESS_OVERWRITE=(TIMOTHY_ACCESS_OVERWRITE and request and waccess)) and (fail=0) then
  301.                 Timothy_db(i).cell.written=(TIMOTHY_VALUE_WRITTEN or Timothy_db(i).cell.written)
  302.                 Timothy_db(i).cell.value=value2
  303.             else
  304.                 fail=(TIMOTHY_ACCESS_OVERWRITE or fail)
  305.             endif
  306.         else
  307.             Timothy_memory_push Timothy_db(),i,value2
  308.         endif
  309.     else
  310.         if not(TIMOTHY_ACCESS_WRITE=(TIMOTHY_ACCESS_WRITE and waccess)) then
  311.             fail=(TIMOTHY_ACCESS_WRITE or fail)
  312.         endif
  313.     endif        
  314.     if (TIMOTHY_ACCESS_DELETE=(TIMOTHY_ACCESS_DELETE and request and waccess)) and (fail=0) then
  315.         node=Timothy_memory_pop(Timothy_db(),i)
  316.     else
  317.         if not(TIMOTHY_ACCESS_DELETE=(TIMOTHY_ACCESS_DELETE and waccess)) then
  318.             fail=(TIMOTHY_ACCESS_DELETE or fail)
  319.         endif    
  320.     endif
  321.     if (TIMOTHY_ACCESS_READ=(TIMOTHY_ACCESS_READ and waccess)) and (fail=0) then
  322.         if (TIMOTHY_ACCESS_DELETE=(TIMOTHY_ACCESS_DELETE and request and waccess)) then
  323.             ret=node.cell.value
  324.         else
  325.             ret=Timothy_db(i).cell.value
  326.         endif
  327.     else
  328.         if not(TIMOTHY_ACCESS_READ=(TIMOTHY_ACCESS_READ and waccess)) then
  329.             fail=(TIMOTHY_ACCESS_READ or fail)
  330.         endif
  331.     endif
  332.     if TIMOTHY_ACCESS_TRACE=(TIMOTHY_ACCESS_TRACE and request) then
  333.         if TIMOTHY_ACCESS_TRACE=(TIMOTHY_ACCESS_TRACE and waccess) then
  334.             index=i
  335.             octet=oo
  336.             biet=bo        
  337.         else
  338.             fail=(TIMOTHY_ACCESS_TRACE or fail)
  339.         endif
  340.     endif
  341.     if not(TIMOTHY_ACCESS_DEBUG=(TIMOTHY_ACCESS_DEBUG and waccess)) and (fail>0) then
  342.         if (TIMOTHY_ACCESS_DEBUG=(TIMOTHY_ACCESS_DEBUG and request)) then
  343.             fail=TIMOTHY_ACCESS_DEBUG
  344.         else
  345.             fail=0
  346.         endif
  347.     endif
  348.     return ret
  349. end function
  350.  
  351. sub Timothy_memory_save(dat() as string, filename as string)
  352.     dim as uinteger fileslot=freefile, oo=0, index=0, o2=0
  353.     dim as string label=space(0), value=space(0)
  354.     open filename for output as #fileslot
  355.     for index=lbound(dat(1)) to ubound(dat(1))
  356.         o2=val("&h"+mid(dat(index,0),8))
  357.         if o2<len(label) then
  358.             label=left(label,o2)
  359.         else
  360.             label=label+mid(dat(index,0),9)
  361.         endif
  362.         print #fileslot, chr(34)+Timothy_codec_tag(label,TIMOTHY_CODEC_ENCODE)+chr(34)+"="+chr(34)+Timothy_codec_tag(dat(index,1),TIMOTHY_CODEC_ENCODE)+chr(34)
  363.     next
  364.     close #fileslot
  365. end sub
  366.  
  367. sub Timothy_memory_load(queue() as Timothy_queue_type, filename as string, byval request as uinteger, byval waccess as uinteger)
  368.     dim as uinteger fileslot=freefile, tmp=0, ct=0
  369.     dim as string label=space(0), value=space(0), dat=space(0)
  370.     open filename for input as #fileslot
  371.     while not eof(fileslot)
  372.         line input #fileslot, dat
  373.         tmp=instr(dat,"=")
  374.         if tmp>0 then
  375.             ct=ct+1
  376.             label=left(dat,tmp-1)
  377.             value=mid(dat,tmp+1)
  378.             if left(label,1)=chr(34) and right(label,1)=chr(34) then
  379.                 label=Timothy_codec_tag(mid(label,2,len(label)-2),TIMOTHY_CODEC_DECODE)
  380.             endif
  381.             if left(value,1)=chr(34) and right(value,1)=chr(34) then
  382.                 value=Timothy_codec_tag(mid(value,2,len(value)-2),TIMOTHY_CODEC_DECODE)
  383.             endif
  384.             redim preserve queue(0 to ct-1) as Timothy_queue_type
  385.             queue(ubound(queue,1)).label=label
  386.             queue(ubound(queue,1)).value=value
  387.             queue(ubound(queue,1)).request=request
  388.             queue(ubound(queue,1)).waccess=waccess
  389.             queue(ubound(queue,1)).fail=0
  390.             queue(ubound(queue,1)).index=0
  391.             queue(ubound(queue,1)).octet=0
  392.             queue(ubound(queue,1)).biet=0
  393.         endif
  394.     wend
  395.     close #fileslot
  396. end sub
  397.  
  398. sub Timothy_memory_dump(Timothy_db() as Timothy_node_type, ret() as string, label as string, byref index as uinteger, byval octet as uinteger, byref biet as uinteger)
  399.     dim as uinteger octet2=0
  400.     dim as string label2=hex(octet2)+label
  401.     redim dump(0 to 0,0 to 1) as string
  402.     Timothy_memory_dump_loop Timothy_db(), ret(), label2, index, octet, biet
  403. end sub
  404.  
  405. sub Timothy_memory_dump_loop(Timothy_db() as Timothy_node_type, ret() as string, label as string, byref index as uinteger, byval octet as uinteger, byref biet as uinteger)
  406.     dim as uinteger f=0
  407.     dim as uinteger nextindex=0,nextoctet=0,nextbiet=0
  408.     dim as string nextlabel=space(0)
  409.     dim as string label2=mid(label,9)
  410.     dim as uinteger octet2=val("&h"+left(label2,8))+octet
  411.     if biet=3 then
  412.         nextoctet=nextoctet+1
  413.         nextbiet=0
  414.         nextlabel=hex(octet2)+mid(nextlabel,9)+chr(0)
  415.     else
  416.         nextoctet=octet
  417.         nextbiet=biet+1
  418.         nextlabel=label
  419.     endif
  420.     if Timothy_db(index).cell.written and biet=0 and len(label2)>0 then
  421.         redim preserve ret(lbound(ret,1) to ubound(ret,1)+1,0 to 1) as string
  422.         ret(ubound(ret,1),0)=hex(octet)+left(label2,len(label2)-1)
  423.         ret(ubound(ret,1),1)=Timothy_db(index).cell.value
  424.         nextoctet=0
  425.         nextlabel=hex(octet2+1)+chr(0)
  426.     endif
  427.     for f=0 to 3
  428.         if Timothy_db(index).fork(f).written then
  429.             nextindex=Timothy_db(index).fork(f).address
  430.             nextlabel=left(label,len(label)-1)+chr((((asc(right(label,1)) shl (biet shl 1)) and &hff) shr (biet shl 1)) or (f shl (biet shl 1)))
  431.             Timothy_memory_dump_loop Timothy_db(),ret(),nextlabel,nextindex,nextoctet,nextbiet
  432.         endif
  433.     next
  434. end sub
  435.  
  436. sub Timothy_memory_dump_save(dump() as string, filename as string)
  437.     dim as uinteger mode=freefile,t=0
  438.     open filename for output as mode
  439.     for t=lbound(dump,1) to ubound(dump,1)
  440.         if len(dump(t,0))>0 then
  441.             print #mode,chr(34)+Timothy_codec_tag(dump(t,0),TIMOTHY_CODEC_ENCODE)+chr(34)+"="+chr(34)+Timothy_codec_tag(dump(t,1),TIMOTHY_CODEC_ENCODE)+chr(34)
  442.         endif
  443.     next
  444.     close mode
  445. end sub
  446.  
  447. sub Timothy_memory_dump_load(dump() as string, filename as string)
  448.     dim as uinteger mode=freefile,t=0,o=0,octet=0,octet2=0
  449.     dim as string i=space(0),label=space(0),value=space(0),label2=space(0)
  450.     redim dump(0 to 0,0 to 1)
  451.     open filename for input as mode
  452.     do while not(eof(mode))
  453.         line input #mode,i
  454.         if len(i)>0 then
  455.             o=instr(i,chr(34)+"="+chr(34))
  456.             if left(i,1)=chr(34) and right(i,1)=chr(34) and o>1 and o<len(i)-3 then
  457.                 label=mid(i,2,instr(2,i,chr(34))-2)
  458.                 value=mid(i,instr(2,i,chr(34)+"="+chr(34))+3)
  459.                 value=mid(value,1,len(value)-1)
  460.                 label=Timothy_codec_tag(label,TIMOTHY_CODEC_DECODE)
  461.                 value=Timothy_codec_tag(value,TIMOTHY_CODEC_DECODE)
  462.                 'octet=val("&h"+left(label,8))
  463.                 'label=mid(label,9)
  464.                 'label2=left(label2,octet+1)+label
  465.                 redim preserve dump(lbound(dump,1) to ubound(dump,1)+1,0 to 1)
  466.                 dump(ubound(dump,1),0)=label                
  467.                 dump(ubound(dump,1),1)=value
  468.             endif
  469.         endif
  470.     loop
  471.     close mode
  472. end sub
  473.  
  474. sub Timothy_memory_dump_queue(dump() as string, queue() as Timothy_queue_type)
  475.     dim as uinteger i=0,o=0,octet=0,octet2=0
  476.     dim as string label=space(0),value=space(0),label2=space(0)
  477.     for i=lbound(dump,1) to ubound(dump,1)
  478.         if len(dump(i,0))>0 then
  479.             label=dump(i,0)
  480.             value=dump(i,1)
  481.             octet=val("&h"+left(label,8))
  482.             label=mid(label,9)
  483.             label2=left(label2,octet+1)+label
  484.             redim preserve queue(lbound(queue,0) to ubound(queue,1))
  485.             queue(ubound(queue,1)).label=label2
  486.             queue(ubound(queue,1)).value=value
  487.             queue(ubound(queue,1)).request=(TIMOTHY_ACCESS_ALLOCATION or TIMOTHY_ACCESS_WRITE or TIMOTHY_ACCESS_OVERWRITE)
  488.             queue(ubound(queue,1)).waccess=(TIMOTHY_ACCESS_ALLOCATION or TIMOTHY_ACCESS_WRITE or TIMOTHY_ACCESS_OVERWRITE)
  489.             queue(ubound(queue,1)).fail=0
  490.             queue(ubound(queue,1)).index=0
  491.             queue(ubound(queue,1)).octet=0
  492.             queue(ubound(queue,1)).biet=0
  493.             queue(ubound(queue,1)).ret=space(0)
  494.         endif
  495.     next
  496. end sub
  497.  
  498. sub Timothy_memory_queue(Timothy_db() as Timothy_node_type,queue() as Timothy_queue_type,waccess as uinteger)
  499.     dim as uinteger index=0
  500.     for index=lbound(queue,1) to ubound(queue,1)
  501.         queue(index).ret=Timothy_memory(Timothy_db(),queue(index).label,queue(index).value,queue(index).request,waccess and queue(index).waccess,queue(index).fail,queue(index).index,queue(index).octet,queue(index).biet)
  502.     next
  503. end sub
  504.  
  505. function Timothy_codec_tag(haystack as string, direction as uinteger) as string
  506.     dim as string tmp=haystack,cur=space(0),ret=space(0)
  507.     dim as string hesc="%"
  508.     do
  509.         if len(tmp)=0 then exit do
  510.         cur=left(tmp,1)
  511.         tmp=mid(tmp,2)
  512.         select case direction
  513.         case TIMOTHY_CODEC_ENCODE
  514.             if (asc(cur)<48) or (asc(cur)>122) or ((asc(cur)>90) and (asc(cur)<97)) then
  515.                 ret=ret+hesc+right(hex(asc(cur)),2)
  516.             else
  517.                 ret=ret+cur
  518.             endif
  519.         case TIMOTHY_CODEC_DECODE
  520.             if cur=hesc then
  521.                 cur=cur+left(tmp,2)
  522.                 tmp=mid(tmp,3)
  523.                 ret=ret+chr(val("&h"+mid(cur,2)))
  524.             else
  525.                 ret=ret+cur
  526.             endif
  527.         end select
  528.     loop
  529.     return ret
  530. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement