Пример #1
0
int sampgdk_callback_register(const char *name,
                              sampgdk_callback handler) {
  int error;
  int i;
  struct _sampgdk_callback_info callback;
  struct _sampgdk_callback_info *ptr;

  assert(name != NULL);

  ptr = _sampgdk_callback_find(name);
  if (ptr != NULL) {
    return sampgdk_array_get_index(&_sampgdk_callbacks, ptr);
  }

  callback.name = malloc(strlen(name) + 1);
  if (callback.name == NULL) {
    return -ENOMEM;
  }

  callback.handler = handler;
  strcpy(callback.name, name);

  /* Keep callbacks ordered by name.
   * This allows us to use binary search in sampgdk_callback_find().
   */
  for (i = 0; i < _sampgdk_callbacks.count - 1; i++) {
    ptr = sampgdk_array_get(&_sampgdk_callbacks, i);
    if (strcmp(name, ptr->name) <= 0) {
      break;
    }
  }

  error = sampgdk_array_insert(&_sampgdk_callbacks, i, 1, &callback);
  if (error < 0) {
    free(callback.name);
    return error;
  }

  return error; /* index */
}
Пример #2
0
int sampgdk_array_insert_single(struct sampgdk_array *a, int index,
                                void *elem) {
  assert(a != NULL);
  assert(elem != NULL);
  return sampgdk_array_insert(a, index, 1, elem);
}