]> 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 /**
31   Sets the given key with the given item
32   if "freeItem" is defined and an item already exists at the given location, 
33   then old item is freed and the new item is put into place.
34   if "freeItem" is not defined and an item already exists, the old item
35   is returned.
36   @return The old item if exists and there is no 'freeItem', returns NULL
37   otherwise
38   */
39 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
40
41 /**
42   Removes an item from the hash.
43   if 'freeItem' is defined it is used and NULL is returned,
44   else the freed item is returned
45   */
46 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
47
48 void* osrfHashGet( osrfHash* hash, const char* key, ... );
49
50
51 /**
52   @return A list of strings representing the keys of the hash. 
53   caller is responsible for freeing the returned string array 
54   with osrfStringArrayFree();
55   */
56 osrfStringArray* osrfHashKeys( osrfHash* hash );
57
58 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
59
60 /**
61   Frees a hash
62   */
63 void osrfHashFree( osrfHash* hash );
64
65 /**
66   @return The number of items in the hash
67   */
68 unsigned long osrfHashGetCount( osrfHash* hash );
69
70
71
72
73 /**
74   Creates a new list iterator with the given list
75   */
76 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
77
78 int osrfHashIteratorHasNext( osrfHashIterator* itr );
79
80 /**
81   Returns the next non-NULL item in the list, return NULL when
82   the end of the list has been reached
83   */
84 void* osrfHashIteratorNext( osrfHashIterator* itr );
85
86 /**
87   Returns a pointer to the key of the current hash item
88  */
89 const char* osrfHashIteratorKey( const osrfHashIterator* itr );
90
91 /**
92   Deallocates the given list
93   */
94 void osrfHashIteratorFree( osrfHashIterator* itr );
95
96 void osrfHashIteratorReset( osrfHashIterator* itr );
97
98 #endif