* \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
*/
long Position;
+ int StepWidth;
};
void *Next;
const char* KeyStr;
+ if (Hash == NULL)
+ return 0;
+
for (i=0; i < Hash->nMembersUsed; i++) {
if (i==0) {
Previous = NULL;
const char *bla = "";
long key;
long i;
+
+ if (Hash == NULL)
+ return 0;
+
if (Hash->MyKeys != NULL)
free (Hash->MyKeys);
foo = Hash->LookupTable[i]->HashKey;
if (First != NULL)
bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+ else
+ bar = "";
if (Second != NULL)
bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+ else
+ bla = "";
}
#ifdef DEBUG
printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
/**
* \brief private destructor for one hash element.
+ * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
* \param Data an element to free using the user provided destructor, or just plain free
*/
static void DeleteHashPayload (Payload *Data)
free(Data->Data);
}
+/**
+ * \brief Destructor for nested hashes
+ */
+void HDeleteHash(void *vHash)
+{
+ HashList *FreeMe = (HashList*)vHash;
+ DeleteHash(&FreeMe);
+}
+
/**
* \brief destroy a hashlist and all of its members
+ * Crashing? do 'print *FreeMe->LookupTable[i]'
* \param Hash Hash to destroy. Is NULL'ed so you are shure its done.
*/
void DeleteHash(HashList **Hash)
Payload **NewPayloadArea;
HashKey **NewTable;
+ if (Hash == NULL)
+ return ;
+
/** double our payload area */
NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
Payload *NewPayloadItem;
HashKey *NewHashKey;
+ if (Hash == NULL)
+ return;
+
if (Hash->nMembersUsed >= Hash->MemberSize)
IncreaseHashSize (Hash);
{
long SearchPos;
+ if (Hash == NULL)
+ return 0;
+
for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
return SearchPos;
long SearchPos;
long StepWidth;
+ if (Hash == NULL)
+ return 0;
+
if (Hash->tainted)
return FindInTaintedHash(Hash, HashBinKey);
return SearchPos;
}
+
+/**
+ * \brief another hashing algorithm; treat it as just a pointer to long.
+ * \param str Our pointer to the long value
+ * \param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
+ * \returns the calculated hash value
+ */
+int Flathash(const char *str, long len)
+{
+ if (len != sizeof (int))
+ return 0;
+ else return *(int*)str;
+}
+
/**
* \brief private abstract wrapper around the hashing algorithm
* \param HKey the hash string
*/
inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
{
+ if (Hash == NULL)
+ return 0;
+
if (Hash->Algorithm == NULL)
return hashlittle(HKey, HKLen, 9283457);
else
long HashBinKey;
long HashAt;
+ if (Hash == NULL)
+ return;
+
/** first, find out were we could fit in... */
HashBinKey = CalcHashKey(Hash, HKey, HKLen);
HashAt = FindInHash(Hash, HashBinKey);
long HashBinKey;
long HashAt;
+ if (Hash == NULL)
+ return 0;
+
if (HKLen <= 0) {
*Data = NULL;
return 0;
int GetHashKeys(HashList *Hash, char ***List)
{
long i;
+ if (Hash == NULL)
+ return 0;
if (Hash->MyKeys != NULL)
free (Hash->MyKeys);
/**
* \brief creates a hash-linear iterator object
+ * \param Hash the list we reference
+ * \param in which step width should we iterate?
+ * If negative, the last position matching the
+ * step-raster is provided.
* \returns the hash iterator
*/
-HashPos *GetNewHashPos(void)
+HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
{
HashPos *Ret;
Ret = (HashPos*)malloc(sizeof(HashPos));
- Ret->Position = 0;
+ if (StepWidth != 0)
+ Ret->StepWidth = StepWidth;
+ else
+ Ret->StepWidth = 1;
+ if (Ret->StepWidth < 0) {
+ Ret->Position = (Hash->nMembersUsed % (-Ret->StepWidth))
+ * (-Ret->StepWidth);
+ }
+ else {
+ Ret->Position = 0;
+ }
return Ret;
}
+/**
+ * \brief retrieve the counter from the itteratoor
+ * \param the Iterator to analyze
+ * \returns the n'th hashposition we point at
+ */
+int GetHashPosCounter(HashPos *At)
+{
+ return At->Position;
+}
+
/**
* \brief frees a linear hash iterator
*/
void DeleteHashPos(HashPos **DelMe)
{
- free(*DelMe);
- *DelMe = NULL;
+ if (*DelMe != NULL)
+ {
+ free(*DelMe);
+ *DelMe = NULL;
+ }
}
* \param Data returns the Data found at HashPos
* \returns whether the item was found or not.
*/
-int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, char **HashKey, void **Data)
+int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
{
long PayloadPos;
- if (Hash->nMembersUsed <= At->Position)
+ if ((Hash == NULL) || (At->Position >= Hash->nMembersUsed) || (At->Position < 0))
return 0;
*HKLen = Hash->LookupTable[At->Position]->HKLen;
*HashKey = Hash->LookupTable[At->Position]->HashKey;
PayloadPos = Hash->LookupTable[At->Position]->Position;
*Data = Hash->Members[PayloadPos]->Data;
- At->Position++;
+
+ At->Position += At->StepWidth;
+ if (At->Position > Hash->nMembersUsed) {
+ At->Position = Hash->nMembersUsed;
+ return 0;
+ }
+ return 1;
+}
+
+/**
+ * \brief Get the data located where At points to
+ * note: you should prefer iterator operations instead of using me.
+ * \param Hash your Hashlist peek from
+ * \param HKLen returns Length of Hashkey Returned
+ * \param HashKey returns the Hashkey corrosponding to HashPos
+ * \param Data returns the Data found at HashPos
+ * \returns whether the item was found or not.
+ */
+int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
+{
+ long PayloadPos;
+
+ if ((Hash == NULL) || (At >= Hash->nMembersUsed))
+ return 0;
+ *HKLen = Hash->LookupTable[At]->HKLen;
+ *HashKey = Hash->LookupTable[At]->HashKey;
+ PayloadPos = Hash->LookupTable[At]->Position;
+ *Data = Hash->Members[PayloadPos]->Data;
return 1;
}
* return strcmp (a, b);
* }
*/
+
+
+/*
+ * Generic function to free a pointer. This can be used as a callback with the
+ * hash table, even on systems where free() is defined as a macro or has had other
+ * horrible things done to it.
+ */
+void generic_free_handler(void *ptr) {
+ free(ptr);
+}
+
+/*
+ * Generic function to free a reference.
+ * since a reference actualy isn't needed to be freed, do nothing.
+ */
+void reference_free_handler(void *ptr)
+{
+ return;
+}
+
+
+