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 pthread_mutex_destroy(&md->lock);
254 md_entry_free (md->head);
255 pthread_mutex_destroy (&md->lock);
257 } /* }}} void meta_data_destroy */
259 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
263 if ((md == NULL) || (key == NULL))
266 pthread_mutex_lock (&md->lock);
268 for (e = md->head; e != NULL; e = e->next)
270 if (strcasecmp (key, e->key) == 0)
272 pthread_mutex_unlock (&md->lock);
277 pthread_mutex_unlock (&md->lock);
279 } /* }}} int meta_data_exists */
281 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
285 if ((md == NULL) || (key == NULL))
288 pthread_mutex_lock (&md->lock);
290 for (e = md->head; e != NULL; e = e->next)
292 if (strcasecmp (key, e->key) == 0)
294 pthread_mutex_unlock (&md->lock);
299 pthread_mutex_unlock (&md->lock);
301 } /* }}} int meta_data_type */
303 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
305 int i = 0, count = 0;
308 if ((md == NULL) || (toc == NULL))
311 pthread_mutex_lock (&md->lock);
313 for (e = md->head; e != NULL; e = e->next)
318 pthread_mutex_unlock (&md->lock);
322 *toc = calloc(count, sizeof(**toc));
323 for (e = md->head; e != NULL; e = e->next)
324 (*toc)[i++] = strdup(e->key);
326 pthread_mutex_unlock (&md->lock);
328 } /* }}} int meta_data_toc */
330 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
335 if ((md == NULL) || (key == NULL))
338 pthread_mutex_lock (&md->lock);
344 if (strcasecmp (key, this->key) == 0)
353 pthread_mutex_unlock (&md->lock);
358 md->head = this->next;
360 prev->next = this->next;
362 pthread_mutex_unlock (&md->lock);
365 md_entry_free (this);
368 } /* }}} int meta_data_delete */
373 int meta_data_add_string (meta_data_t *md, /* {{{ */
374 const char *key, const char *value)
378 if ((md == NULL) || (key == NULL) || (value == NULL))
381 e = md_entry_alloc (key);
385 e->value.mv_string = md_strdup (value);
386 if (e->value.mv_string == NULL)
388 ERROR ("meta_data_add_string: md_strdup failed.");
392 e->type = MD_TYPE_STRING;
394 return (md_entry_insert (md, e));
395 } /* }}} int meta_data_add_string */
397 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
398 const char *key, int64_t value)
402 if ((md == NULL) || (key == NULL))
405 e = md_entry_alloc (key);
409 e->value.mv_signed_int = value;
410 e->type = MD_TYPE_SIGNED_INT;
412 return (md_entry_insert (md, e));
413 } /* }}} int meta_data_add_signed_int */
415 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
416 const char *key, uint64_t value)
420 if ((md == NULL) || (key == NULL))
423 e = md_entry_alloc (key);
427 e->value.mv_unsigned_int = value;
428 e->type = MD_TYPE_UNSIGNED_INT;
430 return (md_entry_insert (md, e));
431 } /* }}} int meta_data_add_unsigned_int */
433 int meta_data_add_double (meta_data_t *md, /* {{{ */
434 const char *key, double value)
438 if ((md == NULL) || (key == NULL))
441 e = md_entry_alloc (key);
445 e->value.mv_double = value;
446 e->type = MD_TYPE_DOUBLE;
448 return (md_entry_insert (md, e));
449 } /* }}} int meta_data_add_double */
451 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
452 const char *key, _Bool value)
456 if ((md == NULL) || (key == NULL))
459 e = md_entry_alloc (key);
463 e->value.mv_boolean = value;
464 e->type = MD_TYPE_BOOLEAN;
466 return (md_entry_insert (md, e));
467 } /* }}} int meta_data_add_boolean */
472 int meta_data_get_string (meta_data_t *md, /* {{{ */
473 const char *key, char **value)
478 if ((md == NULL) || (key == NULL) || (value == NULL))
481 pthread_mutex_lock (&md->lock);
483 e = md_entry_lookup (md, key);
486 pthread_mutex_unlock (&md->lock);
490 if (e->type != MD_TYPE_STRING)
492 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
493 pthread_mutex_unlock (&md->lock);
497 temp = md_strdup (e->value.mv_string);
500 pthread_mutex_unlock (&md->lock);
501 ERROR ("meta_data_get_string: md_strdup failed.");
505 pthread_mutex_unlock (&md->lock);
510 } /* }}} int meta_data_get_string */
512 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
513 const char *key, int64_t *value)
517 if ((md == NULL) || (key == NULL) || (value == NULL))
520 pthread_mutex_lock (&md->lock);
522 e = md_entry_lookup (md, key);
525 pthread_mutex_unlock (&md->lock);
529 if (e->type != MD_TYPE_SIGNED_INT)
531 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
532 pthread_mutex_unlock (&md->lock);
536 *value = e->value.mv_signed_int;
538 pthread_mutex_unlock (&md->lock);
540 } /* }}} int meta_data_get_signed_int */
542 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
543 const char *key, uint64_t *value)
547 if ((md == NULL) || (key == NULL) || (value == NULL))
550 pthread_mutex_lock (&md->lock);
552 e = md_entry_lookup (md, key);
555 pthread_mutex_unlock (&md->lock);
559 if (e->type != MD_TYPE_UNSIGNED_INT)
561 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
562 pthread_mutex_unlock (&md->lock);
566 *value = e->value.mv_unsigned_int;
568 pthread_mutex_unlock (&md->lock);
570 } /* }}} int meta_data_get_unsigned_int */
572 int meta_data_get_double (meta_data_t *md, /* {{{ */
573 const char *key, double *value)
577 if ((md == NULL) || (key == NULL) || (value == NULL))
580 pthread_mutex_lock (&md->lock);
582 e = md_entry_lookup (md, key);
585 pthread_mutex_unlock (&md->lock);
589 if (e->type != MD_TYPE_DOUBLE)
591 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
592 pthread_mutex_unlock (&md->lock);
596 *value = e->value.mv_double;
598 pthread_mutex_unlock (&md->lock);
600 } /* }}} int meta_data_get_double */
602 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
603 const char *key, _Bool *value)
607 if ((md == NULL) || (key == NULL) || (value == NULL))
610 pthread_mutex_lock (&md->lock);
612 e = md_entry_lookup (md, key);
615 pthread_mutex_unlock (&md->lock);
619 if (e->type != MD_TYPE_BOOLEAN)
621 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
622 pthread_mutex_unlock (&md->lock);
626 *value = e->value.mv_boolean;
628 pthread_mutex_unlock (&md->lock);
630 } /* }}} int meta_data_get_boolean */
632 /* vim: set sw=2 sts=2 et fdm=marker : */