1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/* $Header: /fridge/cvs/xscorch/sgame/sregistry.c,v 1.7 2009-04-26 17:39:44 jacob Exp $ */
/*
xscorch - sregistry.c Copyright(c) 2003-2004 Jacob Luna Lundberg
jacob(at)chaos2.org http://chaos2.org/~jacob
Scorched runtime fast data registry
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2 of the License ONLY.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sregistry.h>
#include <sutil/shashlist.h>
sc_registry *sc_registry_new(void) {
/* sc_registry_new
Allocate and initialize a new registry. */
sc_registry *registry;
/* Allocate the registry. */
registry = (sc_registry *)malloc(sizeof(sc_registry));
if(registry == NULL)
return(NULL);
/* Allocate the storage hashlist. */
registry->hashlist = shashlist_new(SC_REGISTRY_SIZE);
if(registry->hashlist == NULL) {
free(registry);
return(NULL);
}
/* Start numbering custom classes at the first available integer. */
registry->nextclass = SHASHLIST_FIRST_CLASS;
/* Start numbering valid registry keys at 0 (-1 means bad key). */
registry->nextkey = 0;
return(registry);
}
void sc_registry_free(sc_registry **registry) {
/* sc_registry_free
Deallocate a registry (whew). */
if(registry == NULL || *registry == NULL) return;
/* This will also deallocate any registry entries, but not their data... */
shashlist_free(&(*registry)->hashlist);
free(*registry);
*registry = NULL;
}
int sc_registry_get_new_class_id(sc_registry *registry) {
/* sc_registry_get_new_class_id
Return the next available class ID. */
if(registry == NULL) return(-1);
return(registry->nextclass++);
}
int sc_registry_get_next_new_key(sc_registry *registry) {
/* sc_registry_get_next_new_key
Return the next available unique integer key. */
if(registry == NULL) return(-1);
return(registry->nextkey++);
}
bool sc_registry_add_by_int(sc_registry *registry, void *data, int class, int key) {
/* sc_registry_add_by_int
Add data to the registry by integer key. */
if(registry == NULL) return(false);
return(shashlist_insert_by_int(registry->hashlist, data, class, key) != NULL);
}
bool sc_registry_add_by_string(sc_registry *registry, void *data, int class, const char *key) {
/* sc_registry_add_by_string
Add data to the registry by string key. */
if(registry == NULL) return(false);
return(shashlist_insert_by_string(registry->hashlist, data, class, key) != NULL);
}
void *sc_registry_del_by_int(sc_registry *registry, int key) {
/* sc_registry_del_by_int
Delete data from the registry by integer key. */
shashlist_item *item;
void *data = NULL;
if(registry == NULL) return(NULL);
item = shashlist_remove_by_int(registry->hashlist, key);
if(item != NULL) {
data = item->data;
shashlist_item_free(&item);
}
return(data);
}
void *sc_registry_del_by_string(sc_registry *registry, const char *key) {
/* sc_registry_del_by_string
Delete data from the registry by string key. */
shashlist_item *item;
void *data = NULL;
if(registry == NULL) return(NULL);
item = shashlist_remove_by_string(registry->hashlist, key);
if(item != NULL) {
data = item->data;
shashlist_item_free(&item);
}
return(data);
}
void *sc_registry_find_by_int(const sc_registry *registry, int key) {
/* sc_registry_find_by_int
Locate a specific registry item by integer key. */
shashlist_item *item;
if(registry == NULL) return(NULL);
item = shashlist_find_by_int(registry->hashlist, key);
if(item == NULL)
return(NULL);
else
return(item->data);
}
void *sc_registry_find_by_string(const sc_registry *registry, const char *key) {
/* sc_registry_find_by_string
Locate a specific registry item by string key. */
shashlist_item *item;
if(registry == NULL) return(NULL);
item = shashlist_find_by_string(registry->hashlist, key);
if(item == NULL)
return(NULL);
else
return(item->data);
}
inline shashlist_item *_sc_registry_internal_iter(shashlist *list, shashlist_item *item, int class, sc_registry_direction direction, sc_registry_test_func function, long arg) {
/* _sc_registry_internal_iter
Local function to find the next or prev entry in a set. */
switch(direction) {
case SC_REGISTRY_FORWARD:
do {
item = shashlist_find_next(list, item, class);
} while(item != NULL && function != NULL && !function(item->data, arg));
break;
case SC_REGISTRY_REVERSE:
do {
item = shashlist_find_prev(list, item, class);
} while(item != NULL && function != NULL && !function(item->data, arg));
break;
}
return(item);
}
void *sc_registry_find_first(const sc_registry *registry, int class, sc_registry_direction direction, sc_registry_test_func function, long arg) {
/* sc_registry_find_first
Find the first item of a given class. */
shashlist_item *item = NULL;
if(registry == NULL) return(NULL);
/* Search for the first item meeting all the criteria. */
item = _sc_registry_internal_iter(registry->hashlist, item,
class, direction, function, arg);
if(item == NULL)
return(NULL);
else
return(item->data);
}
void *sc_registry_find_next(const sc_registry *registry, int class, int key, sc_registry_direction direction, sc_registry_test_func function, long arg) {
/* sc_registry_find_next
Find the next item of a given class, given an item. */
shashlist_item *item;
if(registry == NULL) return(NULL);
item = shashlist_find_by_int(registry->hashlist, key);
/* Search for the next item meeting all the criteria. */
item = _sc_registry_internal_iter(registry->hashlist, item,
class, direction, function, arg);
if(item == NULL)
return(NULL);
else
return(item->data);
}
sc_registry_iter *sc_registry_iter_new(const sc_registry *registry, int class, sc_registry_direction direction, sc_registry_test_func function, long arg) {
/* sc_registry_iter_new
Allocate an sc_registry_iter struct for fast registry set iteration. */
sc_registry_iter *iter;
if(registry == NULL) return(NULL);
iter = (sc_registry_iter *)malloc(sizeof(sc_registry_iter));
if(iter == NULL) return(NULL);
/* Prep for the iteration. */
iter->arg = arg;
iter->class = class;
iter->current = NULL;
iter->direction = direction;
iter->function = function;
iter->registry = registry;
iter->running = true;
return(iter);
}
void sc_registry_iter_free(sc_registry_iter **iter) {
/* sc_registry_iter_free
Free a registry iteration struct. */
if(iter == NULL || *iter == NULL) return;
(*iter)->running = false;
free(*iter);
*iter = NULL;
}
void *sc_registry_iterate(sc_registry_iter *iter) {
/* sc_registry_iterate
Find and return the next item in the set, if any. */
if(iter == NULL) return(NULL);
/* Make sure we're still iterating happily along. */
if(!iter->running)
return(NULL);
/* Perform the search for the next iteration. */
iter->current = _sc_registry_internal_iter(iter->registry->hashlist, iter->current,
iter->class, iter->direction,
iter->function, iter->arg);
if(iter->current == NULL) {
/* We've reached the end of the set. */
iter->running = false;
return(NULL);
} else {
return(iter->current->data);
}
}
|