home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Sylia / VariableTable.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  1.6 KB  |  82 lines

  1. #include <string.h>
  2. #include <stddef.h>
  3.  
  4. #include "ScriptError.h"
  5. #include "StringHeap.h"
  6.  
  7. #include "VariableTable.h"
  8.  
  9. VariableTable::VariableTable(int ht_size) : varheap(16384) {
  10.     long i;
  11.  
  12.     lHashTableSize    = ht_size;
  13.     lpHashTable        = new VariableTableEntry *[ht_size];
  14.  
  15.     for(i=0; i<lHashTableSize; i++)
  16.         lpHashTable[i] = 0;
  17. }
  18.  
  19. VariableTable::~VariableTable() {
  20.     delete[] lpHashTable;
  21. }
  22.  
  23.  
  24. long VariableTable::Hash(const char *szName) {
  25.     unsigned hc = 0;
  26.     char c;
  27.  
  28.     while(c=*szName++)
  29.         hc = (hc + 17) * (unsigned char)c;
  30.  
  31.     return (long)(hc % lHashTableSize);
  32. }
  33.  
  34. void VariableTable::MarkStrings(VDScriptStringHeap& heap) {
  35.     for(long hc=0; hc<lHashTableSize; ++hc) {
  36.         VariableTableEntry *vte = lpHashTable[hc];
  37.  
  38.         while(vte) {
  39.             const VDScriptValue& val = vte->v;
  40.  
  41.             if (val.isString())
  42.                 heap.Mark(*val.asString());
  43.  
  44.             vte = vte->next;
  45.         }
  46.     }
  47. }
  48.  
  49. VariableTableEntry *VariableTable::Lookup(const char *szName) {
  50.     long lHashVal = Hash(szName);
  51.     VariableTableEntry *vte = lpHashTable[lHashVal];
  52.  
  53.     while(vte) {
  54.         if (!strcmp(vte->szName, szName))
  55.             return vte;
  56.  
  57.         vte = vte->next;
  58.     }
  59.  
  60.     return NULL;
  61. }
  62.  
  63. VariableTableEntry *VariableTable::Declare(const char *szName) {
  64.     VariableTableEntry *vte;
  65.     long lHashVal = Hash(szName);
  66.     long lNameLen;
  67.  
  68.     lNameLen    = strlen(szName);
  69.  
  70.     vte            = (VariableTableEntry *)varheap.Allocate(sizeof(VariableTableEntry) + lNameLen);
  71.     if (!vte)
  72.         SCRIPT_ERROR(OUT_OF_MEMORY);
  73.  
  74.     vte->next    = lpHashTable[lHashVal];
  75.     vte->v        = VDScriptValue();
  76.     strcpy(vte->szName, szName);
  77.  
  78.     lpHashTable[lHashVal] = vte;
  79.  
  80.     return vte;
  81. }
  82.