]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_hash.c
819b979c04f639651de3b03dde9ec04112c587b4
[Evergreen.git] / OpenSRF / src / libstack / osrf_hash.c
1 #include "osrf_hash.h"
2
3 osrfHash* osrfNewHash() {
4         osrfHash* hash = safe_malloc(sizeof(osrfHash));
5         hash->hash = (Pvoid_t) NULL;
6         hash->freeItem = NULL;
7         return hash;
8 }
9
10 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... ) {
11         if(!(hash && item && key )) return NULL;
12
13         Word_t* value;
14         VA_LIST_TO_STRING(key);
15         uint8_t idx[strlen(VA_BUF) + 1];
16         strcpy( idx, VA_BUF );
17
18         void* olditem = osrfHashRemove( hash, VA_BUF );
19
20         JSLI(value, hash->hash, idx);
21         if(value) *value = (Word_t) item;
22         return olditem;
23         
24 }
25
26 void* osrfHashRemove( osrfHash* hash, const char* key, ... ) {
27         if(!(hash && key )) return NULL;
28
29         VA_LIST_TO_STRING(key);
30
31         Word_t* value;
32         uint8_t idx[strlen(VA_BUF) + 1];
33         strcpy( idx, VA_BUF );
34         void* item = NULL;
35         int retcode;
36
37         JSLG( value, hash->hash,  idx);
38
39         if( value ) {
40                 item = (void*) *value;
41                 if(item) {
42                         if( hash->freeItem ) {
43                                 hash->freeItem( (char*) idx, item ); 
44                                 item = NULL;
45                         }
46                 }
47         }
48
49
50         JSLD( retcode, hash->hash, idx );
51
52         return item;
53 }
54
55
56 void* osrfHashGet( osrfHash* hash, const char* key, ... ) {
57         if(!(hash && key )) return NULL;
58
59         VA_LIST_TO_STRING(key);
60
61         Word_t* value;
62         uint8_t idx[strlen(VA_BUF) + 1];
63         strcpy( idx, VA_BUF );
64
65         JSLG( value, hash->hash, idx );
66         if(value) return (void*) *value;
67         return NULL;
68 }
69
70
71 osrfStringArray* osrfHashKeys( osrfHash* hash ) {
72         if(!hash) return NULL;
73
74         Word_t* value;
75         uint8_t idx[OSRF_HASH_MAXKEY];
76         strcpy(idx, "");
77         char* key;
78         osrfStringArray* strings = osrfNewStringArray(8);
79
80         JSLF( value, hash->hash, idx );
81
82         while( value ) {
83                 key = (char*) idx;
84                 osrfStringArrayAdd( strings, key );
85                 JSLN( value, hash->hash, idx );
86         }
87
88         return strings;
89 }
90
91
92 unsigned long osrfHashGetCount( osrfHash* hash ) {
93         if(!hash) return -1;
94
95         Word_t* value;
96         unsigned long count = 0;
97         uint8_t idx[OSRF_HASH_MAXKEY];
98
99         strcpy( (char*) idx, "");
100         JSLF(value, hash->hash, idx);
101
102         while(value) {
103                 count++;
104                 JSLN( value, hash->hash, idx );
105         }
106
107         return count;
108 }
109
110 void osrfHashFree( osrfHash* hash ) {
111         if(!hash) return;
112
113         int i;
114         osrfStringArray* keys = osrfHashKeys( hash );
115
116         for( i = 0; i != keys->size; i++ )  {
117                 char* key = (char*) osrfStringArrayGetString( keys, i );
118                 osrfHashRemove( hash, key );
119         }
120
121         osrfStringArrayFree(keys);
122         free(hash);
123 }
124
125
126
127 osrfHashIterator* osrfNewHashIterator( osrfHash* hash ) {
128         if(!hash) return NULL;
129         osrfHashIterator* itr = safe_malloc(sizeof(osrfHashIterator));
130         itr->hash = hash;
131         itr->current = NULL;
132         return itr;
133 }
134
135 void* osrfHashIteratorNext( osrfHashIterator* itr ) {
136         if(!(itr && itr->hash)) return NULL;
137
138         Word_t* value;
139         uint8_t idx[OSRF_HASH_MAXKEY];
140
141         if( itr->current == NULL ) { /* get the first item in the list */
142                 strcpy(idx, "");
143                 JSLF( value, itr->hash->hash, idx );
144
145         } else {
146                 strcpy(idx, itr->current);
147                 JSLN( value, itr->hash->hash, idx );
148         }
149
150         if(value) {
151                 free(itr->current);
152                 itr->current = strdup((char*) idx);
153                 return (void*) *value;
154         }
155
156         return NULL;
157
158 }
159
160 void osrfHashIteratorFree( osrfHashIterator* itr ) {
161         if(!itr) return;
162         free(itr->current);
163         free(itr);
164 }
165
166 void osrfHashIteratorReset( osrfHashIterator* itr ) {
167         if(!itr) return;
168         free(itr->current);
169         itr->current = NULL;
170 }
171
172
173