2 * collectd - src/meta_data.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
40 /* Single UNIX needed for strdup. */
44 #define _XOPEN_SOURCE 500
62 #include "meta_data.h"
69 #define MD_TYPE_STRING 1
70 #define MD_TYPE_SIGNED_INT 2
71 #define MD_TYPE_UNSIGNED_INT 3
72 #define MD_TYPE_DOUBLE 4
80 int64_t mv_signed_int;
81 uint64_t mv_unsigned_int;
84 typedef union meta_value_u meta_value_t;
87 typedef struct meta_entry_s meta_entry_t;
105 static char *md_strdup (const char *orig) /* {{{ */
113 sz = strlen (orig) + 1;
114 dest = (char *) malloc (sz);
118 memcpy (dest, orig, sz);
121 } /* }}} char *md_strdup */
123 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
127 e = (meta_entry_t *) malloc (sizeof (*e));
130 ERROR ("md_entry_alloc: malloc failed.");
133 memset (e, 0, sizeof (*e));
135 e->key = md_strdup (key);
139 ERROR ("md_entry_alloc: md_strdup failed.");
147 } /* }}} meta_entry_t *md_entry_alloc */
149 static void md_entry_free (meta_entry_t *e) /* {{{ */
156 if (e->type == MD_TYPE_STRING)
157 free (e->value.mv_string);
160 md_entry_free (e->next);
163 } /* }}} void md_entry_free */
165 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
170 if ((md == NULL) || (e == NULL))
173 pthread_mutex_lock (&md->lock);
179 if (strcasecmp (e->key, this->key) == 0)
188 /* This key does not exist yet. */
189 if (md->head == NULL)
193 assert (prev != NULL);
199 else /* (this != NULL) */
206 e->next = this->next;
209 pthread_mutex_unlock (&md->lock);
214 md_entry_free (this);
218 } /* }}} int md_entry_insert */
220 /* XXX: The lock on md must be held while calling this function! */
221 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
226 if ((md == NULL) || (key == NULL))
229 for (e = md->head; e != NULL; e = e->next)
230 if (strcasecmp (key, e->key) == 0)
234 } /* }}} meta_entry_t *md_entry_lookup */
239 meta_data_t *meta_data_create (void) /* {{{ */
243 md = (meta_data_t *) malloc (sizeof (*md));
246 ERROR ("meta_data_create: malloc failed.");
249 memset (md, 0, sizeof (*md));
252 pthread_mutex_init (&md->lock, /* attr = */ NULL);
255 } /* }}} meta_data_t *meta_data_create */
257 void meta_data_destroy (meta_data_t *md) /* {{{ */
262 md_entry_free (md->head);
264 } /* }}} void meta_data_destroy */
266 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
270 if ((md == NULL) || (key == NULL))
273 pthread_mutex_lock (&md->lock);
275 for (e = md->head; e != NULL; e = e->next)
277 if (strcasecmp (key, e->key) == 0)
279 pthread_mutex_unlock (&md->lock);
284 pthread_mutex_unlock (&md->lock);
286 } /* }}} int meta_data_exists */
288 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
293 if ((md == NULL) || (key == NULL))
296 pthread_mutex_lock (&md->lock);
302 if (strcasecmp (key, this->key) == 0)
311 pthread_mutex_unlock (&md->lock);
316 md->head = this->next;
318 prev->next = this->next;
320 pthread_mutex_unlock (&md->lock);
323 md_entry_free (this);
326 } /* }}} int meta_data_delete */
328 int meta_data_add_string (meta_data_t *md, /* {{{ */
329 const char *key, const char *value)
333 if ((md == NULL) || (key == NULL) || (value == NULL))
336 e = md_entry_alloc (key);
340 e->value.mv_string = md_strdup (value);
341 if (e->value.mv_string == NULL)
343 ERROR ("meta_data_add_string: md_strdup failed.");
347 e->type = MD_TYPE_STRING;
349 return (md_entry_insert (md, e));
350 } /* }}} int meta_data_add_string */
352 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
353 const char *key, int64_t value)
357 if ((md == NULL) || (key == NULL))
360 e = md_entry_alloc (key);
364 e->value.mv_signed_int = value;
365 e->type = MD_TYPE_SIGNED_INT;
367 return (md_entry_insert (md, e));
368 } /* }}} int meta_data_add_signed_int */
370 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
371 const char *key, uint64_t value)
375 if ((md == NULL) || (key == NULL))
378 e = md_entry_alloc (key);
382 e->value.mv_unsigned_int = value;
383 e->type = MD_TYPE_UNSIGNED_INT;
385 return (md_entry_insert (md, e));
386 } /* }}} int meta_data_add_unsigned_int */
388 int meta_data_add_double (meta_data_t *md, /* {{{ */
389 const char *key, double value)
393 if ((md == NULL) || (key == NULL))
396 e = md_entry_alloc (key);
400 e->value.mv_double = value;
401 e->type = MD_TYPE_DOUBLE;
403 return (md_entry_insert (md, e));
404 } /* }}} int meta_data_add_double */
406 int meta_data_get_string (meta_data_t *md, /* {{{ */
407 const char *key, char **value)
412 if ((md == NULL) || (key == NULL) || (value == NULL))
415 pthread_mutex_lock (&md->lock);
417 e = md_entry_lookup (md, key);
420 pthread_mutex_unlock (&md->lock);
424 if (e->type != MD_TYPE_SIGNED_INT)
426 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
427 pthread_mutex_unlock (&md->lock);
431 temp = md_strdup (e->value.mv_string);
434 pthread_mutex_unlock (&md->lock);
435 ERROR ("meta_data_get_string: md_strdup failed.");
439 pthread_mutex_unlock (&md->lock);
444 } /* }}} int meta_data_get_string */
446 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
447 const char *key, int64_t *value)
451 if ((md == NULL) || (key == NULL) || (value == NULL))
454 pthread_mutex_lock (&md->lock);
456 e = md_entry_lookup (md, key);
459 pthread_mutex_unlock (&md->lock);
463 if (e->type != MD_TYPE_SIGNED_INT)
465 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
466 pthread_mutex_unlock (&md->lock);
470 *value = e->value.mv_signed_int;
472 pthread_mutex_unlock (&md->lock);
474 } /* }}} int meta_data_get_signed_int */
476 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
477 const char *key, uint64_t *value)
481 if ((md == NULL) || (key == NULL) || (value == NULL))
484 pthread_mutex_lock (&md->lock);
486 e = md_entry_lookup (md, key);
489 pthread_mutex_unlock (&md->lock);
493 if (e->type != MD_TYPE_UNSIGNED_INT)
495 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
496 pthread_mutex_unlock (&md->lock);
500 *value = e->value.mv_unsigned_int;
502 pthread_mutex_unlock (&md->lock);
504 } /* }}} int meta_data_get_unsigned_int */
506 int meta_data_get_double (meta_data_t *md, /* {{{ */
507 const char *key, double *value)
511 if ((md == NULL) || (key == NULL) || (value == NULL))
514 pthread_mutex_lock (&md->lock);
516 e = md_entry_lookup (md, key);
519 pthread_mutex_unlock (&md->lock);
523 if (e->type != MD_TYPE_DOUBLE)
525 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
526 pthread_mutex_unlock (&md->lock);
530 *value = e->value.mv_double;
532 pthread_mutex_unlock (&md->lock);
534 } /* }}} int meta_data_get_double */
536 /* vim: set sw=2 sts=2 et fdm=marker : */