]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_hash.h
Patch from Scott McKellar:
[OpenSRF.git] / include / opensrf / osrf_hash.h
1 #ifndef OSRF_HASH_H
2 #define OSRF_HASH_H
3
4 #include <opensrf/utils.h>
5 #include <opensrf/string_array.h>
6 #include <opensrf/osrf_list.h>
7
8 struct _osrfHashStruct {
9         osrfList* hash; /* this hash */
10         void (*freeItem) (char* key, void* item);       /* callback for freeing stored items */
11         unsigned int size;
12         osrfStringArray* keys;
13 };
14 typedef struct _osrfHashStruct osrfHash;
15
16 struct _osrfHashIteratorStruct {
17         char* current;
18         size_t currsize;  // length of "current" buffer
19         int currentIdx;
20         osrfHash* hash;
21         osrfStringArray* keys;
22 };
23 typedef struct _osrfHashIteratorStruct osrfHashIterator;
24
25 /**
26   Allocates a new hash object
27   */
28 osrfHash* osrfNewHash();
29
30 /** Installs a callback function for freeing stored items
31     */
32 void osrfHashSetCallback( osrfHash* hash, void (*callback) (char* key, void* item) );
33
34 /**
35   Sets the given key with the given item
36   if "freeItem" is defined and an item already exists at the given location, 
37   then old item is freed and the new item is put into place.
38   if "freeItem" is not defined and an item already exists, the old item
39   is returned.
40   @return The old item if exists and there is no 'freeItem', returns NULL
41   otherwise
42   */
43 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
44
45 /**
46   Removes an item from the hash.
47   if 'freeItem' is defined it is used and NULL is returned,
48   else the freed item is returned
49   */
50 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
51
52 void* osrfHashGet( osrfHash* hash, const char* key, ... );
53
54
55 /**
56   @return A list of strings representing the keys of the hash. 
57   caller is responsible for freeing the returned string array 
58   with osrfStringArrayFree();
59   */
60 osrfStringArray* osrfHashKeys( osrfHash* hash );
61
62 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
63
64 /**
65   Frees a hash
66   */
67 void osrfHashFree( osrfHash* hash );
68
69 /**
70   @return The number of items in the hash
71   */
72 unsigned long osrfHashGetCount( osrfHash* hash );
73
74
75
76
77 /**
78   Creates a new list iterator with the given list
79   */
80 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
81
82 int osrfHashIteratorHasNext( osrfHashIterator* itr );
83
84 /**
85   Returns the next non-NULL item in the list, return NULL when
86   the end of the list has been reached
87   */
88 void* osrfHashIteratorNext( osrfHashIterator* itr );
89
90 /**
91   Returns a pointer to the key of the current hash item
92  */
93 const char* osrfHashIteratorKey( const osrfHashIterator* itr );
94
95 /**
96   Deallocates the given list
97   */
98 void osrfHashIteratorFree( osrfHashIterator* itr );
99
100 void osrfHashIteratorReset( osrfHashIterator* itr );
101
102 #endif