int sampgdk_array_insert_ordered(struct sampgdk_array *a, void *elem, int (*comp)(const void *x, const void *y)) { int index; for (index = 0; index < a->count; index++) { if (comp(sampgdk_array_get(a, index), elem) >= 0) { return sampgdk_array_insert_single(a, index, elem); } } return sampgdk_array_append(a, elem); }
int sampgdk_callback_register(const char *name, sampgdk_callback_handler handler) { int error; struct sampgdk_callback info; struct sampgdk_callback *ptr; int index; assert(name != NULL); assert(handler != NULL); /* This is rather an exception than a rule. */ sampgdk_callback_init(); ptr = sampgdk_callback_find(name); if (ptr != NULL) { ptr->handler = handler; return 0; } info.name = malloc(strlen(name) + 1); if (info.name == NULL) { return -ENOMEM; } strcpy(info.name, name); info.handler = handler; /* Maintain element order (by name). */ for (index = 0; index < callbacks.count; index++) { ptr = (struct sampgdk_callback *)sampgdk_array_get(&callbacks, index); if (strcmp(ptr->name, name) >= 0) { error = sampgdk_array_insert_single(&callbacks, index, &info); break; } } /* Append to the end. */ if (index == callbacks.count) { error = sampgdk_array_append(&callbacks, &info); } if (error < 0) { free(info.name); return error; } return 0; }
int sampgdk_native_register(const char *name, AMX_NATIVE func) { AMX_NATIVE_INFO info; AMX_NATIVE_INFO *ptr; int index; info.name = name; info.func = func; assert(name != 0); /* Keep natives ordered by name. * This allows us to use binary search in sampgdk_native_find(). */ for (index = 0; index < _sampgdk_natives.count - 1; index++) { ptr = sampgdk_array_get(&_sampgdk_natives, index); if (strcmp(name, ptr->name) <= 0) { break; } } return sampgdk_array_insert_single(&_sampgdk_natives, index, &info); }