Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 11th, 2010  |  syntax: C++  |  size: 2.51 KB  |  views: 103  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. //#include "socket.h"
  7.  
  8. void raise(const char* msg) {
  9.   printf("\n\n%s\n\n", msg);
  10.   exit(-1);
  11. }
  12.  
  13. class StringHash {
  14.   public:
  15.     StringHash() {
  16.       allocSize = 20;
  17.       hashSize = 0;
  18.       hkeys = new char*[allocSize];
  19.       if(hkeys == NULL) {raise("Allocation failure in StringHash::StringHash() (1)");}
  20.       values = new char*[allocSize];
  21.       if(values == NULL) {raise("Allocation failure in StringHash::StringHash() (2)");}
  22.     }
  23.    
  24.     ~StringHash() {
  25.       for(int i = 0; i < hashSize; ++i) {
  26.         delete hkeys[i];
  27.         delete values[i];
  28.       }
  29.       delete hkeys;
  30.       delete values;
  31.     }
  32.    
  33.     const char* operator[](const char* key) {
  34.       for(int i = 0; i < hashSize; ++i) {
  35.         if(strcmp(hkeys[i], key) == 0) {return values[i];}
  36.       }
  37.       return NULL;
  38.     }
  39.    
  40.     void addEntry(const char* key, const char* value) {
  41.     //void operator[]=(const char* key, const char* value) {
  42.       if(hashSize == allocSize) {
  43.         allocSize += 20;
  44.         char** tempKeys = new char*[allocSize];
  45.         if(tempKeys == NULL) {raise("Allocation failure in StringHash::addEntry() (1)");}
  46.         char** tempVals = new char*[allocSize];
  47.         if(tempVals == NULL) {raise("Allocation failure in StringHash::addEntry() (2)");}
  48.         for(int i = 0; i < hashSize; ++i) {
  49.           tempKeys[i] = hkeys[i];
  50.           tempVals[i] = values[i];
  51.         }
  52.         delete hkeys;
  53.         hkeys = tempKeys;
  54.         delete values;
  55.         values = tempVals;
  56.       }
  57.       int keyLen = strlen(key) + 1;
  58.       int valLen = strlen(value) + 1;
  59.       hkeys[hashSize] = new char[keyLen];
  60.       if(hkeys[hashSize] == NULL) {raise("Allocation failure in StringHash::addEntry() (3)");}
  61.       memset(hkeys[hashSize], 0, keyLen);
  62.       strcpy(hkeys[hashSize], key);
  63.       values[hashSize] = new char[valLen];
  64.       if(values[hashSize] == NULL) {raise("Allocation failure in StringHash::addEntry() (4)");}
  65.       memset(values[hashSize], 0, valLen);
  66.       strcpy(values[hashSize], value);
  67.       ++hashSize;
  68.     }
  69.    
  70.     const char** keys() {
  71.       return (const char**)hkeys;
  72.     }
  73.    
  74.     int size() {
  75.       return hashSize;
  76.     }
  77.    
  78.   private:
  79.     int allocSize;
  80.     int hashSize;
  81.     char** hkeys;
  82.     char** values;
  83. };
  84.  
  85. int main() {
  86.   StringHash hash;
  87.   hash.addEntry("test", "This is a test\n");
  88.   //hash["test"] = "This is a test";
  89.   printf(hash["test"]);
  90.   return 0;
  91. }
clone this paste RAW Paste Data