/* * Get functions */ int meta_data_get_string(meta_data_t *md, /* {{{ */ const char *key, char **value) { meta_entry_t *e; char *temp; if ((md == NULL) || (key == NULL) || (value == NULL)) return (-EINVAL); pthread_mutex_lock(&md->lock); e = md_entry_lookup(md, key); if (e == NULL) { pthread_mutex_unlock(&md->lock); return (-ENOENT); } if (e->type != MD_TYPE_STRING) { ERROR("meta_data_get_string: Type mismatch for key `%s'", e->key); pthread_mutex_unlock(&md->lock); return (-ENOENT); } temp = md_strdup(e->value.mv_string); if (temp == NULL) { pthread_mutex_unlock(&md->lock); ERROR("meta_data_get_string: md_strdup failed."); return (-ENOMEM); } pthread_mutex_unlock(&md->lock); *value = temp; return (0); } /* }}} int meta_data_get_string */
static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */ { meta_entry_t *e; e = (meta_entry_t *) malloc (sizeof (*e)); if (e == NULL) { ERROR ("md_entry_alloc: malloc failed."); return (NULL); } memset (e, 0, sizeof (*e)); e->key = md_strdup (key); if (e->key == NULL) { free (e); ERROR ("md_entry_alloc: md_strdup failed."); return (NULL); } e->type = 0; e->next = NULL; return (e); } /* }}} meta_entry_t *md_entry_alloc */
int meta_data_as_string(meta_data_t *md, /* {{{ */ const char *key, char **value) { meta_entry_t *e; const char *actual; char buffer[MD_MAX_NONSTRING_CHARS]; /* For non-string types. */ char *temp; int type; if ((md == NULL) || (key == NULL) || (value == NULL)) return (-EINVAL); pthread_mutex_lock(&md->lock); e = md_entry_lookup(md, key); if (e == NULL) { pthread_mutex_unlock(&md->lock); return (-ENOENT); } type = e->type; switch (type) { case MD_TYPE_STRING: actual = e->value.mv_string; break; case MD_TYPE_SIGNED_INT: ssnprintf(buffer, sizeof(buffer), "%" PRIi64, e->value.mv_signed_int); actual = buffer; break; case MD_TYPE_UNSIGNED_INT: ssnprintf(buffer, sizeof(buffer), "%" PRIu64, e->value.mv_unsigned_int); actual = buffer; break; case MD_TYPE_DOUBLE: ssnprintf(buffer, sizeof(buffer), GAUGE_FORMAT, e->value.mv_double); actual = buffer; break; case MD_TYPE_BOOLEAN: actual = e->value.mv_boolean ? "true" : "false"; break; default: pthread_mutex_unlock(&md->lock); ERROR("meta_data_as_string: unknown type %d for key `%s'", type, key); return (-ENOENT); } pthread_mutex_unlock(&md->lock); temp = md_strdup(actual); if (temp == NULL) { pthread_mutex_unlock(&md->lock); ERROR("meta_data_as_string: md_strdup failed for key `%s'.", key); return (-ENOMEM); } *value = temp; return (0); } /* }}} int meta_data_as_string */
/* * Add functions */ int meta_data_add_string(meta_data_t *md, /* {{{ */ const char *key, const char *value) { meta_entry_t *e; if ((md == NULL) || (key == NULL) || (value == NULL)) return (-EINVAL); e = md_entry_alloc(key); if (e == NULL) return (-ENOMEM); e->value.mv_string = md_strdup(value); if (e->value.mv_string == NULL) { ERROR("meta_data_add_string: md_strdup failed."); md_entry_free(e); return (-ENOMEM); } e->type = MD_TYPE_STRING; return (md_entry_insert(md, e)); } /* }}} int meta_data_add_string */