From bfae789827f7f5a7c9a8d8c3e350fff5bae3718e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Wed, 30 Apr 2008 14:21:31 +0000 Subject: [PATCH] * added more usefull print hash function, so one can now use the list to render UI stuff * added sortorder to SortByHashKey so one can switch upward/downward sorting --- libcitadel/configure.in | 2 +- libcitadel/lib/hash.c | 64 +++++++++++++++++++++++++++++++++++-- libcitadel/lib/libcitadel.h | 18 ++++++++--- 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/libcitadel/configure.in b/libcitadel/configure.in index 392d61805..9aca0db28 100755 --- a/libcitadel/configure.in +++ b/libcitadel/configure.in @@ -5,7 +5,7 @@ dnl dnl Ensure that libcitadel is configured with autoconf 2.52 or newer AC_PREREQ(2.52) -AC_INIT(libcitadel, 1.10, https://uncensored.citadel.org) +AC_INIT(libcitadel, 1.11, https://uncensored.citadel.org) AC_CONFIG_SRCDIR(Makefile.in) AC_CONFIG_AUX_DIR(conftools) diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index 330483840..f945fd5cb 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -48,6 +48,48 @@ struct HashPos { long Position; }; + +/** + * \brief Iterate over the hash and call PrintEntry. + * \param Hash your Hashlist structure + * \param Trans is called so you could for example print 'A:' if the next entries are like that. + * Must be aware to receive NULL in both pointers. + * \param PrintEntry print entry one by one + * \returns the number of items printed + */ +int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry) +{ + int i; + void *Previous; + void *Next; + const char* KeyStr; + + for (i=0; i < Hash->nMembersUsed; i++) { + if (i==0) { + Previous = NULL; + } + else { + if (Hash->LookupTable[i - 1] == NULL) + Previous = NULL; + else + Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data; + } + if (Hash->LookupTable[i] == NULL) { + KeyStr = ""; + Next = NULL; + } + else { + Next = Hash->Members[Hash->LookupTable[i]->Position]->Data; + KeyStr = Hash->LookupTable[i]->HashKey; + } + + Trans(Previous, Next, i % 2); + PrintEntry(KeyStr, Next, i % 2); + } + return i; +} + + /** * \brief verify the contents of a hash list; here for debugging purposes. * \param Hash your Hashlist structure @@ -55,7 +97,7 @@ struct HashPos { * \param Second Functionpointer to allow you to print your payload * \returns 0 */ -int PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second) +int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second) { const char *foo; const char *bar; @@ -509,6 +551,20 @@ static int SortByKeys(const void *Key1, const void* Key2) return strcasecmp(HKey1->HashKey, HKey2->HashKey); } +/** + * \brief sorting function for sorting the Hash alphabeticaly reverse by their strings + * \param Key1 first item + * \param Key2 second item + */ +static int SortByKeysRev(const void *Key1, const void* Key2) +{ + HashKey *HKey1, *HKey2; + HKey1 = *(HashKey**) Key1; + HKey2 = *(HashKey**) Key2; + + return strcasecmp(HKey2->HashKey, HKey1->HashKey); +} + /** * \brief sorting function to regain hash-sequence and revert tainted status * \param Key1 first item @@ -529,12 +585,14 @@ static int SortByHashKeys(const void *Key1, const void* Key2) * Caution: This taints the hashlist, so accessing it later * will be significantly slower! You can un-taint it by SortByHashKeyStr * \param Hash the list to sort + * \param Order 0/1 Forward/Backward */ -void SortByHashKey(HashList *Hash) +void SortByHashKey(HashList *Hash, int Order) { if (Hash->nMembersUsed < 2) return; - qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByKeys); + qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), + (Order)?SortByKeys:SortByKeysRev); Hash->tainted = 1; } diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index f9b14d9c8..4657ad531 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -9,7 +9,7 @@ */ #include #include -#define LIBCITADEL_VERSION_NUMBER 110 +#define LIBCITADEL_VERSION_NUMBER 111 /* * Here's a bunch of stupid magic to make the MIME parser portable. @@ -270,6 +270,8 @@ typedef void (*DeleteHashDataFunc)(void * Data); typedef const char *(*PrintHashContent)(void * Data); typedef int (*CompareFunc)(const void* Item1, const void*Item2); typedef int (*HashFunc)(const char *Str, long Len); +typedef void (*TransitionFunc) (void *Item1, void *Item2, int Odd); +typedef void (*PrintHashDataFunc) (const char *Key, void *Item, int Odd); HashList *NewHash(int Uniq, HashFunc F); @@ -283,7 +285,10 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Data); int GetHashKeys(HashList *Hash, char ***List); -int PrintHash(HashList *Hash, PrintHashContent first, PrintHashContent Second); +int dbg_PrintHash(HashList *Hash, PrintHashContent first, PrintHashContent Second); + + +int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry); HashPos *GetNewHashPos(void); @@ -291,7 +296,7 @@ void DeleteHashPos(HashPos **DelMe); int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, char **HashKey, void **Data); -void SortByHashKey(HashList *Hash); +void SortByHashKey(HashList *Hash, int Order); void SortByHashKeyStr(HashList *Hash); const void *GetSearchPayload(const void *HashVoid); @@ -299,7 +304,12 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy); void convert_spaces_to_underscores(char *str); - +/* + * Convert 4 bytes char into an Integer. + * usefull for easy inexpensive hashing + * of for char strings. + */ +#define CHAR4TO_INT(a) ((int) (a[0] | (a[1]<<8) | (a[2]<<16) | (a[3]<<24))) /* vNote implementation */ -- 2.30.2