2 * collectd - src/meta_data.c
3 * Copyright (C) 2008,2009 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>
24 #include "meta_data.h"
34 int64_t mv_signed_int;
35 uint64_t mv_unsigned_int;
39 typedef union meta_value_u meta_value_t;
42 typedef struct meta_entry_s meta_entry_t;
60 static char *md_strdup (const char *orig) /* {{{ */
68 sz = strlen (orig) + 1;
69 dest = (char *) malloc (sz);
73 memcpy (dest, orig, sz);
76 } /* }}} char *md_strdup */
78 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
82 e = (meta_entry_t *) malloc (sizeof (*e));
85 ERROR ("md_entry_alloc: malloc failed.");
88 memset (e, 0, sizeof (*e));
90 e->key = md_strdup (key);
94 ERROR ("md_entry_alloc: md_strdup failed.");
102 } /* }}} meta_entry_t *md_entry_alloc */
104 static void md_entry_free (meta_entry_t *e) /* {{{ */
111 if (e->type == MD_TYPE_STRING)
112 free (e->value.mv_string);
115 md_entry_free (e->next);
118 } /* }}} void md_entry_free */
120 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
125 if ((md == NULL) || (e == NULL))
128 pthread_mutex_lock (&md->lock);
134 if (strcasecmp (e->key, this->key) == 0)
143 /* This key does not exist yet. */
144 if (md->head == NULL)
148 assert (prev != NULL);
154 else /* (this != NULL) */
161 e->next = this->next;
164 pthread_mutex_unlock (&md->lock);
169 md_entry_free (this);
173 } /* }}} int md_entry_insert */
175 /* XXX: The lock on md must be held while calling this function! */
176 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
181 if ((md == NULL) || (key == NULL))
184 for (e = md->head; e != NULL; e = e->next)
185 if (strcasecmp (key, e->key) == 0)
189 } /* }}} meta_entry_t *md_entry_lookup */
194 meta_data_t *meta_data_create (void) /* {{{ */
198 md = (meta_data_t *) malloc (sizeof (*md));
201 ERROR ("meta_data_create: malloc failed.");
204 memset (md, 0, sizeof (*md));
207 pthread_mutex_init (&md->lock, /* attr = */ NULL);
210 } /* }}} meta_data_t *meta_data_create */
212 void meta_data_destroy (meta_data_t *md) /* {{{ */
217 md_entry_free (md->head);
218 pthread_mutex_destroy (&md->lock);
220 } /* }}} void meta_data_destroy */
222 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
226 if ((md == NULL) || (key == NULL))
229 pthread_mutex_lock (&md->lock);
231 for (e = md->head; e != NULL; e = e->next)
233 if (strcasecmp (key, e->key) == 0)
235 pthread_mutex_unlock (&md->lock);
240 pthread_mutex_unlock (&md->lock);
242 } /* }}} int meta_data_exists */
244 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
248 if ((md == NULL) || (key == NULL))
251 pthread_mutex_lock (&md->lock);
253 for (e = md->head; e != NULL; e = e->next)
255 if (strcasecmp (key, e->key) == 0)
257 pthread_mutex_unlock (&md->lock);
262 pthread_mutex_unlock (&md->lock);
264 } /* }}} int meta_data_type */
266 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
268 int i = 0, count = 0;
271 if ((md == NULL) || (toc == NULL))
274 pthread_mutex_lock (&md->lock);
276 for (e = md->head; e != NULL; e = e->next)
279 *toc = malloc(count * sizeof(**toc));
280 for (e = md->head; e != NULL; e = e->next)
281 (*toc)[i++] = strdup(e->key);
283 pthread_mutex_unlock (&md->lock);
285 } /* }}} int meta_data_toc */
287 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
292 if ((md == NULL) || (key == NULL))
295 pthread_mutex_lock (&md->lock);
301 if (strcasecmp (key, this->key) == 0)
310 pthread_mutex_unlock (&md->lock);
315 md->head = this->next;
317 prev->next = this->next;
319 pthread_mutex_unlock (&md->lock);
322 md_entry_free (this);
325 } /* }}} int meta_data_delete */
330 int meta_data_add_string (meta_data_t *md, /* {{{ */
331 const char *key, const char *value)
335 if ((md == NULL) || (key == NULL) || (value == NULL))
338 e = md_entry_alloc (key);
342 e->value.mv_string = md_strdup (value);
343 if (e->value.mv_string == NULL)
345 ERROR ("meta_data_add_string: md_strdup failed.");
349 e->type = MD_TYPE_STRING;
351 return (md_entry_insert (md, e));
352 } /* }}} int meta_data_add_string */
354 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
355 const char *key, int64_t value)
359 if ((md == NULL) || (key == NULL))
362 e = md_entry_alloc (key);
366 e->value.mv_signed_int = value;
367 e->type = MD_TYPE_SIGNED_INT;
369 return (md_entry_insert (md, e));
370 } /* }}} int meta_data_add_signed_int */
372 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
373 const char *key, uint64_t value)
377 if ((md == NULL) || (key == NULL))
380 e = md_entry_alloc (key);
384 e->value.mv_unsigned_int = value;
385 e->type = MD_TYPE_UNSIGNED_INT;
387 return (md_entry_insert (md, e));
388 } /* }}} int meta_data_add_unsigned_int */
390 int meta_data_add_double (meta_data_t *md, /* {{{ */
391 const char *key, double value)
395 if ((md == NULL) || (key == NULL))
398 e = md_entry_alloc (key);
402 e->value.mv_double = value;
403 e->type = MD_TYPE_DOUBLE;
405 return (md_entry_insert (md, e));
406 } /* }}} int meta_data_add_double */
408 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
409 const char *key, _Bool value)
413 if ((md == NULL) || (key == NULL))
416 e = md_entry_alloc (key);
420 e->value.mv_boolean = value;
421 e->type = MD_TYPE_BOOLEAN;
423 return (md_entry_insert (md, e));
424 } /* }}} int meta_data_add_boolean */
429 int meta_data_get_string (meta_data_t *md, /* {{{ */
430 const char *key, char **value)
435 if ((md == NULL) || (key == NULL) || (value == NULL))
438 pthread_mutex_lock (&md->lock);
440 e = md_entry_lookup (md, key);
443 pthread_mutex_unlock (&md->lock);
447 if (e->type != MD_TYPE_STRING)
449 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
450 pthread_mutex_unlock (&md->lock);
454 temp = md_strdup (e->value.mv_string);
457 pthread_mutex_unlock (&md->lock);
458 ERROR ("meta_data_get_string: md_strdup failed.");
462 pthread_mutex_unlock (&md->lock);
467 } /* }}} int meta_data_get_string */
469 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
470 const char *key, int64_t *value)
474 if ((md == NULL) || (key == NULL) || (value == NULL))
477 pthread_mutex_lock (&md->lock);
479 e = md_entry_lookup (md, key);
482 pthread_mutex_unlock (&md->lock);
486 if (e->type != MD_TYPE_SIGNED_INT)
488 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
489 pthread_mutex_unlock (&md->lock);
493 *value = e->value.mv_signed_int;
495 pthread_mutex_unlock (&md->lock);
497 } /* }}} int meta_data_get_signed_int */
499 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
500 const char *key, uint64_t *value)
504 if ((md == NULL) || (key == NULL) || (value == NULL))
507 pthread_mutex_lock (&md->lock);
509 e = md_entry_lookup (md, key);
512 pthread_mutex_unlock (&md->lock);
516 if (e->type != MD_TYPE_UNSIGNED_INT)
518 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
519 pthread_mutex_unlock (&md->lock);
523 *value = e->value.mv_unsigned_int;
525 pthread_mutex_unlock (&md->lock);
527 } /* }}} int meta_data_get_unsigned_int */
529 int meta_data_get_double (meta_data_t *md, /* {{{ */
530 const char *key, double *value)
534 if ((md == NULL) || (key == NULL) || (value == NULL))
537 pthread_mutex_lock (&md->lock);
539 e = md_entry_lookup (md, key);
542 pthread_mutex_unlock (&md->lock);
546 if (e->type != MD_TYPE_DOUBLE)
548 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
549 pthread_mutex_unlock (&md->lock);
553 *value = e->value.mv_double;
555 pthread_mutex_unlock (&md->lock);
557 } /* }}} int meta_data_get_double */
559 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
560 const char *key, _Bool *value)
564 if ((md == NULL) || (key == NULL) || (value == NULL))
567 pthread_mutex_lock (&md->lock);
569 e = md_entry_lookup (md, key);
572 pthread_mutex_unlock (&md->lock);
576 if (e->type != MD_TYPE_BOOLEAN)
578 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
579 pthread_mutex_unlock (&md->lock);
583 *value = e->value.mv_boolean;
585 pthread_mutex_unlock (&md->lock);
587 } /* }}} int meta_data_get_boolean */
589 /* vim: set sw=2 sts=2 et fdm=marker : */