Server 0.0.1
HTTP/REST server implementation

src/hash/value.c

Go to the documentation of this file.
00001 
00023 #include <stdarg.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 
00028 #include "class.h"
00029 #include "utils/hash.h"
00030 #include "utils/memory.h"
00031 #include "commons.h"
00032 
00033 #include "hash/value.h"
00034 #include "hash/interface/hashable.h"
00035 
00036 static
00037 int
00038 hashValueCtor(void * _this, va_list * params)
00039 {
00040         HashValue this = _this;
00041         char * key     = va_arg(* params, char*);
00042         void * value;
00043         
00044         this->nkey   = va_arg(* params, size_t);
00045         value        = va_arg(* params, void*);
00046         this->nvalue = va_arg(* params, size_t);
00047 
00048         this->key = malloc(this->nkey + 1);
00049         this->key[this->nkey] = 0;
00050         memcpy(this->key, key, this->nkey);
00051 
00052         this->hash = sdbm((unsigned char *)this->key, this->nkey);
00053 
00054         if (NULL != value) {
00055                 this->value = malloc(this->nvalue + 1);
00056                 ((char*)this->value)[this->nvalue] = 0;
00057                 memcpy(this->value, value, this->nvalue);
00058         }
00059 
00060         return 0;
00061 }
00062 
00063 static
00064 void
00065 hashValueDtor(void * _this)
00066 {
00067         HashValue this = _this;
00068 
00069         FREE(this->key);
00070         FREE(this->value);
00071 }
00072 
00073 static
00074 unsigned long
00075 hashValueGetHash(void * _this)
00076 {
00077         HashValue this = _this;
00078 
00079         return this->hash;
00080 }
00081 
00082 static
00083 void
00084 hashValueHandleDouble(void * _this, void * _double)
00085 {
00086         HashValue this = _this;
00087         HashValue doub = _double;
00088         void * tmp_value;
00089         size_t tmp_nvalue;
00090 
00097         tmp_value   = this->value;
00098         this->value = doub->value;
00099         doub->value = tmp_value;
00100 
00101         tmp_nvalue   = this->nvalue;
00102         this->nvalue = doub->nvalue;
00103         doub->nvalue = tmp_nvalue;
00104 }
00105 
00106 INIT_IFACE(Class, hashValueCtor, hashValueDtor, NULL);
00107 INIT_IFACE(Hashable, hashValueGetHash, hashValueHandleDouble);
00108 CREATE_CLASS(HashValue, NULL, IFACE(Class), IFACE(Hashable));
00109 
00110 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines