'===========================================================================
' Subject: QHASH COMPONENT                    Date: 06-23-00 (18:31)       
'  Author: Daitau Aaron                       Code: RAPIDQ                 
'  Origin: daitau@hotmail.com               Packet: RAPIDQ.ABC
'===========================================================================
' QHash Component
' ------------------------------------------------------
' by Daitau Aaron (daitau@hotmail.com)
'
' This tries to imitate perl hash using QStringList
' Currently you may only assign simple strings as values

$TYPECHECK ON

TYPE QHash EXTENDS QObject

  keys as QStringList
  values as QStringList
  pointer as integer

  sub set (k as string, v as string)
    WITH QHash
      dim l as integer
      l=.keys.indexof(k)
      if l>0 then
        .keys.delitems l
        .values.delitems l
      end if
      .keys.additems k
      .values.additems v
    END WITH
  end sub

  function itemcount as integer
    WITH QHash
      Result=.keys.itemcount
    END WITH
  end function

  function get (k as string) as string
    WITH QHash
      dim l as integer
      l=.keys.indexof(k)
      if l>0 then
        Result=.values.item(l)
      else
        Result=""
      end if
    END WITH
  end function

  function exists (k as string) as integer
    WITH QHash
      Result=(.keys.indexof(k)>0)
    END WITH
  end function

  sub del (k as string)
    WITH QHash
      dim l as integer
      l=.keys.indexof(k)
      if l>0 then
        .keys.delitems l
        .values.delitems l
      end if
    END WITH
  end sub

  sub clear
    WITH QHash
      .keys.clear
      .values.clear
    END WITH
  end sub

  function key (i as integer) as string
    WITH QHash
      Result=.keys.item(i)
    END WITH
  end function

  function value (i as integer) as string
    WITH QHash
      Result=.values.item(i)
    END WITH
  end function

END TYPE


' ------------------------------------------------------
' example on how to use QHash
' ------------------------------------------------------

dim header as QHash

' set some keys and values
header.set("Host","www.yahoo.com")
header.set("Date","12:37:44 GMT")
header.set("X-cache","yes")

' print out the whole thing
dim i as integer
for i = 0 to header.itemcount-1
  print header.key(i)+"="+header.value(i)
next i

' retrieve value by key
print
print "X-cache="+header.get("X-cache")
' remove from hash
print "delete X-cache..."
header.del "X-cache"
' check for existence
if header.exists("X-cache") then
  print header.get("X-cache")
else
  print "X-cache does not exist"
end if
' clean the whole hash
print "Clean the whole hash"
header.clear
print "Host="+header.get("Host")
