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)
315 *toc = malloc(count * sizeof(**toc));
316 for (e = md->head; e != NULL; e = e->next)
317 (*toc)[i++] = strdup(e->key);
319 pthread_mutex_unlock (&md->lock);
321 } /* }}} int meta_data_toc */
323 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
328 if ((md == NULL) || (key == NULL))
331 pthread_mutex_lock (&md->lock);
337 if (strcasecmp (key, this->key) == 0)
346 pthread_mutex_unlock (&md->lock);
351 md->head = this->next;
353 prev->next = this->next;
355 pthread_mutex_unlock (&md->lock);
358 md_entry_free (this);
361 } /* }}} int meta_data_delete */
366 int meta_data_add_string (meta_data_t *md, /* {{{ */
367 const char *key, const char *value)
371 if ((md == NULL) || (key == NULL) || (value == NULL))
374 e = md_entry_alloc (key);
378 e->value.mv_string = md_strdup (value);
379 if (e->value.mv_string == NULL)
381 ERROR ("meta_data_add_string: md_strdup failed.");
385 e->type = MD_TYPE_STRING;
387 return (md_entry_insert (md, e));
388 } /* }}} int meta_data_add_string */
390 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
391 const char *key, int64_t value)
395 if ((md == NULL) || (key == NULL))
398 e = md_entry_alloc (key);
402 e->value.mv_signed_int = value;
403 e->type = MD_TYPE_SIGNED_INT;
405 return (md_entry_insert (md, e));
406 } /* }}} int meta_data_add_signed_int */
408 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
409 const char *key, uint64_t value)
413 if ((md == NULL) || (key == NULL))
416 e = md_entry_alloc (key);
420 e->value.mv_unsigned_int = value;
421 e->type = MD_TYPE_UNSIGNED_INT;
423 return (md_entry_insert (md, e));
424 } /* }}} int meta_data_add_unsigned_int */
426 int meta_data_add_double (meta_data_t *md, /* {{{ */
427 const char *key, double value)
431 if ((md == NULL) || (key == NULL))
434 e = md_entry_alloc (key);
438 e->value.mv_double = value;
439 e->type = MD_TYPE_DOUBLE;
441 return (md_entry_insert (md, e));
442 } /* }}} int meta_data_add_double */
444 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
445 const char *key, _Bool value)
449 if ((md == NULL) || (key == NULL))
452 e = md_entry_alloc (key);
456 e->value.mv_boolean = value;
457 e->type = MD_TYPE_BOOLEAN;
459 return (md_entry_insert (md, e));
460 } /* }}} int meta_data_add_boolean */
465 int meta_data_get_string (meta_data_t *md, /* {{{ */
466 const char *key, char **value)
471 if ((md == NULL) || (key == NULL) || (value == NULL))
474 pthread_mutex_lock (&md->lock);
476 e = md_entry_lookup (md, key);
479 pthread_mutex_unlock (&md->lock);
483 if (e->type != MD_TYPE_STRING)
485 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
486 pthread_mutex_unlock (&md->lock);
490 temp = md_strdup (e->value.mv_string);
493 pthread_mutex_unlock (&md->lock);
494 ERROR ("meta_data_get_string: md_strdup failed.");
498 pthread_mutex_unlock (&md->lock);
503 } /* }}} int meta_data_get_string */
505 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
506 const char *key, int64_t *value)
510 if ((md == NULL) || (key == NULL) || (value == NULL))
513 pthread_mutex_lock (&md->lock);
515 e = md_entry_lookup (md, key);
518 pthread_mutex_unlock (&md->lock);
522 if (e->type != MD_TYPE_SIGNED_INT)
524 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
525 pthread_mutex_unlock (&md->lock);
529 *value = e->value.mv_signed_int;
531 pthread_mutex_unlock (&md->lock);
533 } /* }}} int meta_data_get_signed_int */
535 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
536 const char *key, uint64_t *value)
540 if ((md == NULL) || (key == NULL) || (value == NULL))
543 pthread_mutex_lock (&md->lock);
545 e = md_entry_lookup (md, key);
548 pthread_mutex_unlock (&md->lock);
552 if (e->type != MD_TYPE_UNSIGNED_INT)
554 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
555 pthread_mutex_unlock (&md->lock);
559 *value = e->value.mv_unsigned_int;
561 pthread_mutex_unlock (&md->lock);
563 } /* }}} int meta_data_get_unsigned_int */
565 int meta_data_get_double (meta_data_t *md, /* {{{ */
566 const char *key, double *value)
570 if ((md == NULL) || (key == NULL) || (value == NULL))
573 pthread_mutex_lock (&md->lock);
575 e = md_entry_lookup (md, key);
578 pthread_mutex_unlock (&md->lock);
582 if (e->type != MD_TYPE_DOUBLE)
584 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
585 pthread_mutex_unlock (&md->lock);
589 *value = e->value.mv_double;
591 pthread_mutex_unlock (&md->lock);
593 } /* }}} int meta_data_get_double */
595 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
596 const char *key, _Bool *value)
600 if ((md == NULL) || (key == NULL) || (value == NULL))
603 pthread_mutex_lock (&md->lock);
605 e = md_entry_lookup (md, key);
608 pthread_mutex_unlock (&md->lock);
612 if (e->type != MD_TYPE_BOOLEAN)
614 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
615 pthread_mutex_unlock (&md->lock);
619 *value = e->value.mv_boolean;
621 pthread_mutex_unlock (&md->lock);
623 } /* }}} int meta_data_get_boolean */
625 /* vim: set sw=2 sts=2 et fdm=marker : */