2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 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 meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
111 copy = md_entry_alloc (orig->key);
112 copy->type = orig->type;
113 if (copy->type == MD_TYPE_STRING)
114 copy->value.mv_string = strdup (orig->value.mv_string);
116 copy->value = orig->value;
118 copy->next = md_entry_clone (orig->next);
120 } /* }}} meta_entry_t *md_entry_clone */
122 static void md_entry_free (meta_entry_t *e) /* {{{ */
129 if (e->type == MD_TYPE_STRING)
130 free (e->value.mv_string);
133 md_entry_free (e->next);
136 } /* }}} void md_entry_free */
138 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
143 if ((md == NULL) || (e == NULL))
146 pthread_mutex_lock (&md->lock);
152 if (strcasecmp (e->key, this->key) == 0)
161 /* This key does not exist yet. */
162 if (md->head == NULL)
166 assert (prev != NULL);
172 else /* (this != NULL) */
179 e->next = this->next;
182 pthread_mutex_unlock (&md->lock);
187 md_entry_free (this);
191 } /* }}} int md_entry_insert */
193 /* XXX: The lock on md must be held while calling this function! */
194 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
199 if ((md == NULL) || (key == NULL))
202 for (e = md->head; e != NULL; e = e->next)
203 if (strcasecmp (key, e->key) == 0)
207 } /* }}} meta_entry_t *md_entry_lookup */
212 meta_data_t *meta_data_create (void) /* {{{ */
216 md = (meta_data_t *) malloc (sizeof (*md));
219 ERROR ("meta_data_create: malloc failed.");
222 memset (md, 0, sizeof (*md));
225 pthread_mutex_init (&md->lock, /* attr = */ NULL);
228 } /* }}} meta_data_t *meta_data_create */
230 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
237 copy = meta_data_create ();
241 pthread_mutex_lock (&orig->lock);
242 copy->head = md_entry_clone (orig->head);
243 pthread_mutex_unlock (&orig->lock);
246 } /* }}} meta_data_t *meta_data_clone */
248 void meta_data_destroy (meta_data_t *md) /* {{{ */
253 md_entry_free (md->head);
254 pthread_mutex_destroy (&md->lock);
256 } /* }}} void meta_data_destroy */
258 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
262 if ((md == NULL) || (key == NULL))
265 pthread_mutex_lock (&md->lock);
267 for (e = md->head; e != NULL; e = e->next)
269 if (strcasecmp (key, e->key) == 0)
271 pthread_mutex_unlock (&md->lock);
276 pthread_mutex_unlock (&md->lock);
278 } /* }}} int meta_data_exists */
280 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
284 if ((md == NULL) || (key == NULL))
287 pthread_mutex_lock (&md->lock);
289 for (e = md->head; e != NULL; e = e->next)
291 if (strcasecmp (key, e->key) == 0)
293 pthread_mutex_unlock (&md->lock);
298 pthread_mutex_unlock (&md->lock);
300 } /* }}} int meta_data_type */
302 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
304 int i = 0, count = 0;
307 if ((md == NULL) || (toc == NULL))
310 pthread_mutex_lock (&md->lock);
312 for (e = md->head; e != NULL; e = e->next)
317 pthread_mutex_unlock (&md->lock);
321 *toc = calloc(count, sizeof(**toc));
322 for (e = md->head; e != NULL; e = e->next)
323 (*toc)[i++] = strdup(e->key);
325 pthread_mutex_unlock (&md->lock);
327 } /* }}} int meta_data_toc */
329 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
334 if ((md == NULL) || (key == NULL))
337 pthread_mutex_lock (&md->lock);
343 if (strcasecmp (key, this->key) == 0)
352 pthread_mutex_unlock (&md->lock);
357 md->head = this->next;
359 prev->next = this->next;
361 pthread_mutex_unlock (&md->lock);
364 md_entry_free (this);
367 } /* }}} int meta_data_delete */
372 int meta_data_add_string (meta_data_t *md, /* {{{ */
373 const char *key, const char *value)
377 if ((md == NULL) || (key == NULL) || (value == NULL))
380 e = md_entry_alloc (key);
384 e->value.mv_string = md_strdup (value);
385 if (e->value.mv_string == NULL)
387 ERROR ("meta_data_add_string: md_strdup failed.");
391 e->type = MD_TYPE_STRING;
393 return (md_entry_insert (md, e));
394 } /* }}} int meta_data_add_string */
396 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
397 const char *key, int64_t value)
401 if ((md == NULL) || (key == NULL))
404 e = md_entry_alloc (key);
408 e->value.mv_signed_int = value;
409 e->type = MD_TYPE_SIGNED_INT;
411 return (md_entry_insert (md, e));
412 } /* }}} int meta_data_add_signed_int */
414 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
415 const char *key, uint64_t value)
419 if ((md == NULL) || (key == NULL))
422 e = md_entry_alloc (key);
426 e->value.mv_unsigned_int = value;
427 e->type = MD_TYPE_UNSIGNED_INT;
429 return (md_entry_insert (md, e));
430 } /* }}} int meta_data_add_unsigned_int */
432 int meta_data_add_double (meta_data_t *md, /* {{{ */
433 const char *key, double value)
437 if ((md == NULL) || (key == NULL))
440 e = md_entry_alloc (key);
444 e->value.mv_double = value;
445 e->type = MD_TYPE_DOUBLE;
447 return (md_entry_insert (md, e));
448 } /* }}} int meta_data_add_double */
450 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
451 const char *key, _Bool value)
455 if ((md == NULL) || (key == NULL))
458 e = md_entry_alloc (key);
462 e->value.mv_boolean = value;
463 e->type = MD_TYPE_BOOLEAN;
465 return (md_entry_insert (md, e));
466 } /* }}} int meta_data_add_boolean */
471 int meta_data_get_string (meta_data_t *md, /* {{{ */
472 const char *key, char **value)
477 if ((md == NULL) || (key == NULL) || (value == NULL))
480 pthread_mutex_lock (&md->lock);
482 e = md_entry_lookup (md, key);
485 pthread_mutex_unlock (&md->lock);
489 if (e->type != MD_TYPE_STRING)
491 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
492 pthread_mutex_unlock (&md->lock);
496 temp = md_strdup (e->value.mv_string);
499 pthread_mutex_unlock (&md->lock);
500 ERROR ("meta_data_get_string: md_strdup failed.");
504 pthread_mutex_unlock (&md->lock);
509 } /* }}} int meta_data_get_string */
511 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
512 const char *key, int64_t *value)
516 if ((md == NULL) || (key == NULL) || (value == NULL))
519 pthread_mutex_lock (&md->lock);
521 e = md_entry_lookup (md, key);
524 pthread_mutex_unlock (&md->lock);
528 if (e->type != MD_TYPE_SIGNED_INT)
530 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
531 pthread_mutex_unlock (&md->lock);
535 *value = e->value.mv_signed_int;
537 pthread_mutex_unlock (&md->lock);
539 } /* }}} int meta_data_get_signed_int */
541 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
542 const char *key, uint64_t *value)
546 if ((md == NULL) || (key == NULL) || (value == NULL))
549 pthread_mutex_lock (&md->lock);
551 e = md_entry_lookup (md, key);
554 pthread_mutex_unlock (&md->lock);
558 if (e->type != MD_TYPE_UNSIGNED_INT)
560 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
561 pthread_mutex_unlock (&md->lock);
565 *value = e->value.mv_unsigned_int;
567 pthread_mutex_unlock (&md->lock);
569 } /* }}} int meta_data_get_unsigned_int */
571 int meta_data_get_double (meta_data_t *md, /* {{{ */
572 const char *key, double *value)
576 if ((md == NULL) || (key == NULL) || (value == NULL))
579 pthread_mutex_lock (&md->lock);
581 e = md_entry_lookup (md, key);
584 pthread_mutex_unlock (&md->lock);
588 if (e->type != MD_TYPE_DOUBLE)
590 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
591 pthread_mutex_unlock (&md->lock);
595 *value = e->value.mv_double;
597 pthread_mutex_unlock (&md->lock);
599 } /* }}} int meta_data_get_double */
601 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
602 const char *key, _Bool *value)
606 if ((md == NULL) || (key == NULL) || (value == NULL))
609 pthread_mutex_lock (&md->lock);
611 e = md_entry_lookup (md, key);
614 pthread_mutex_unlock (&md->lock);
618 if (e->type != MD_TYPE_BOOLEAN)
620 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
621 pthread_mutex_unlock (&md->lock);
625 *value = e->value.mv_boolean;
627 pthread_mutex_unlock (&md->lock);
629 } /* }}} int meta_data_get_boolean */
631 /* vim: set sw=2 sts=2 et fdm=marker : */