Example #1
0
static entry_t *entry_new(const char *key, const char *value)
{
    entry_t *entry = osi_calloc(sizeof(entry_t));
    if (!entry) {
        return NULL;
    }

    entry->key = osi_strdup(key);
    entry->value = osi_strdup(value);
    return entry;
}
Example #2
0
hash_map_t *hash_map_utils_new_from_string_params(const char *params) {
  assert(params != NULL);

  hash_map_t *map = hash_map_new(BUCKETS_NUM, hash_function_string, osi_free,
                                 osi_free, string_equals);
  if (!map)
    return NULL;

  char *str = osi_strdup(params);
  if (!str)
    return NULL;

  LOG_VERBOSE(LOG_TAG, "%s: source string: '%s'", __func__, str);

  // Parse |str| and add extracted key-and-value pair(s) in |map|.
  int items = 0;
  char *tmpstr;
  char *kvpair = strtok_r(str, ";", &tmpstr);
  while (kvpair && *kvpair) {
    char *eq = strchr(kvpair, '=');

    if (eq == kvpair)
      goto next_pair;

    char *key;
    char *value;
    if (eq) {
      key = osi_strndup(kvpair, eq - kvpair);

      // The increment of |eq| moves |eq| to the beginning of the value.
      ++eq;
      value = (*eq != '\0') ? osi_strdup(eq) : osi_strdup("");
    } else {
      key = osi_strdup(kvpair);
      value = osi_strdup("");
    }

    hash_map_set(map, key, value);

    items++;
next_pair:
    kvpair = strtok_r(NULL, ";", &tmpstr);
  }

  if (!items)
    LOG_VERBOSE(LOG_TAG, "%s: no items found in string\n", __func__);

  osi_free(str);
  return map;
}
data_dispatcher_t *data_dispatcher_new(const char *name) {
  assert(name != NULL);

  data_dispatcher_t *ret = osi_calloc(sizeof(data_dispatcher_t));
  if (!ret) {
    LOG_ERROR(LOG_TAG, "%s unable to allocate memory for new data dispatcher.", __func__);
    goto error;
  }

  ret->dispatch_table = hash_map_new(DEFAULT_TABLE_BUCKETS, hash_function_naive, NULL, NULL, NULL);
  if (!ret->dispatch_table) {
    LOG_ERROR(LOG_TAG, "%s unable to create dispatch table.", __func__);
    goto error;
  }

  ret->name = osi_strdup(name);
  if (!ret->name) {
    LOG_ERROR(LOG_TAG, "%s unable to duplicate provided name.", __func__);
    goto error;
  }

  return ret;

error:;
  data_dispatcher_free(ret);
  return NULL;
}
Example #4
0
static section_t *section_new(const char *name)
{
    section_t *section = osi_calloc(sizeof(section_t));
    if (!section) {
        return NULL;
    }

    section->name = osi_strdup(name);
    section->entries = list_new(entry_free);
    return section;
}
Example #5
0
void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back)
{
    section_t *sec = section_find(config, section);
    if (!sec) {
        sec = section_new(section);
        if (insert_back) {
            list_append(config->sections, sec);
        } else {
            list_prepend(config->sections, sec);
        }
    }

    for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
        entry_t *entry = list_node(node);
        if (!strcmp(entry->key, key)) {
            osi_free(entry->value);
            entry->value = osi_strdup(value);
            return;
        }
    }

    entry_t *entry = entry_new(key, value);
    list_append(sec->entries, entry);
}