X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmeta_data.c;h=6a336c4b67c734b62cf1516c3375b649fd826cac;hb=adee81fe60077d03345c94e3780752a8c2c712cf;hp=1412f7effe92a5369995113959025987fc949994;hpb=619f112a584fcd89d2e8df7781a0a01341188189;p=collectd.git diff --git a/src/meta_data.c b/src/meta_data.c index 1412f7ef..6a336c4b 100644 --- a/src/meta_data.c +++ b/src/meta_data.c @@ -1,6 +1,6 @@ /** * collectd - src/meta_data.c - * Copyright (C) 2008 Florian octo Forster + * Copyright (C) 2008,2009 Florian octo Forster * * 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 @@ -19,44 +19,6 @@ * Florian octo Forster **/ -/* - * First tell the compiler to stick to the C99 and POSIX standards as close as - * possible. - */ -#ifndef __STRICT_ANSI__ /* {{{ */ -# define __STRICT_ANSI__ -#endif - -#ifndef _ISOC99_SOURCE -# define _ISOC99_SOURCE -#endif - -#ifdef _POSIX_C_SOURCE -# undef _POSIX_C_SOURCE -#endif -#define _POSIX_C_SOURCE 200112L - -#if 0 -/* Single UNIX needed for strdup. */ -#ifdef _XOPEN_SOURCE -# undef _XOPEN_SOURCE -#endif -#define _XOPEN_SOURCE 500 -#endif - -#ifndef _REENTRANT -# define _REENTRANT -#endif - -#ifndef _THREAD_SAFE -# define _THREAD_SAFE -#endif - -#ifdef _GNU_SOURCE -# undef _GNU_SOURCE -#endif -/* }}} */ - #include "collectd.h" #include "plugin.h" #include "meta_data.h" @@ -64,14 +26,6 @@ #include /* - * Defines - */ -#define MD_TYPE_STRING 1 -#define MD_TYPE_SIGNED_INT 2 -#define MD_TYPE_UNSIGNED_INT 3 -#define MD_TYPE_DOUBLE 4 - -/* * Data types */ union meta_value_u @@ -80,6 +34,7 @@ union meta_value_u int64_t mv_signed_int; uint64_t mv_unsigned_int; double mv_double; + _Bool mv_boolean; }; typedef union meta_value_u meta_value_t; @@ -285,6 +240,49 @@ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */ return (0); } /* }}} int meta_data_exists */ +int meta_data_type (meta_data_t *md, const char *key) /* {{{ */ +{ + meta_entry_t *e; + + if ((md == NULL) || (key == NULL)) + return -EINVAL; + + pthread_mutex_lock (&md->lock); + + for (e = md->head; e != NULL; e = e->next) + { + if (strcasecmp (key, e->key) == 0) + { + pthread_mutex_unlock (&md->lock); + return e->type; + } + } + + pthread_mutex_unlock (&md->lock); + return 0; +} /* }}} int meta_data_type */ + +int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ +{ + int i = 0, count = 0; + meta_entry_t *e; + + if ((md == NULL) || (toc == NULL)) + return -EINVAL; + + pthread_mutex_lock (&md->lock); + + for (e = md->head; e != NULL; e = e->next) + ++count; + + *toc = malloc(count * sizeof(**toc)); + for (e = md->head; e != NULL; e = e->next) + (*toc)[i++] = strdup(e->key); + + pthread_mutex_unlock (&md->lock); + return count; +} /* }}} int meta_data_toc */ + int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */ { meta_entry_t *this; @@ -325,6 +323,9 @@ int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */ return (0); } /* }}} int meta_data_delete */ +/* + * Add functions + */ int meta_data_add_string (meta_data_t *md, /* {{{ */ const char *key, const char *value) { @@ -403,6 +404,27 @@ int meta_data_add_double (meta_data_t *md, /* {{{ */ return (md_entry_insert (md, e)); } /* }}} int meta_data_add_double */ +int meta_data_add_boolean (meta_data_t *md, /* {{{ */ + const char *key, _Bool value) +{ + meta_entry_t *e; + + if ((md == NULL) || (key == NULL)) + return (-EINVAL); + + e = md_entry_alloc (key); + if (e == NULL) + return (-ENOMEM); + + e->value.mv_boolean = value; + e->type = MD_TYPE_BOOLEAN; + + return (md_entry_insert (md, e)); +} /* }}} int meta_data_add_boolean */ + +/* + * Get functions + */ int meta_data_get_string (meta_data_t *md, /* {{{ */ const char *key, char **value) { @@ -421,7 +443,7 @@ int meta_data_get_string (meta_data_t *md, /* {{{ */ return (-ENOENT); } - if (e->type != MD_TYPE_SIGNED_INT) + if (e->type != MD_TYPE_STRING) { ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key); pthread_mutex_unlock (&md->lock); @@ -533,4 +555,34 @@ int meta_data_get_double (meta_data_t *md, /* {{{ */ return (0); } /* }}} int meta_data_get_double */ +int meta_data_get_boolean (meta_data_t *md, /* {{{ */ + const char *key, _Bool *value) +{ + meta_entry_t *e; + + 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_BOOLEAN) + { + ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key); + pthread_mutex_unlock (&md->lock); + return (-ENOENT); + } + + *value = e->value.mv_boolean; + + pthread_mutex_unlock (&md->lock); + return (0); +} /* }}} int meta_data_get_boolean */ + /* vim: set sw=2 sts=2 et fdm=marker : */