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);
114 copy->type = orig->type;
115 if (copy->type == MD_TYPE_STRING)
116 copy->value.mv_string = strdup (orig->value.mv_string);
118 copy->value = orig->value;
120 copy->next = md_entry_clone (orig->next);
122 } /* }}} meta_entry_t *md_entry_clone */
124 static void md_entry_free (meta_entry_t *e) /* {{{ */
131 if (e->type == MD_TYPE_STRING)
132 free (e->value.mv_string);
135 md_entry_free (e->next);
138 } /* }}} void md_entry_free */
140 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
145 if ((md == NULL) || (e == NULL))
148 pthread_mutex_lock (&md->lock);
154 if (strcasecmp (e->key, this->key) == 0)
163 /* This key does not exist yet. */
164 if (md->head == NULL)
168 assert (prev != NULL);
174 else /* (this != NULL) */
181 e->next = this->next;
184 pthread_mutex_unlock (&md->lock);
189 md_entry_free (this);
193 } /* }}} int md_entry_insert */
195 /* XXX: The lock on md must be held while calling this function! */
196 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
201 if ((md == NULL) || (key == NULL))
204 for (e = md->head; e != NULL; e = e->next)
205 if (strcasecmp (key, e->key) == 0)
209 } /* }}} meta_entry_t *md_entry_lookup */
214 meta_data_t *meta_data_create (void) /* {{{ */
218 md = (meta_data_t *) malloc (sizeof (*md));
221 ERROR ("meta_data_create: malloc failed.");
224 memset (md, 0, sizeof (*md));
227 pthread_mutex_init (&md->lock, /* attr = */ NULL);
230 } /* }}} meta_data_t *meta_data_create */
232 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
239 copy = meta_data_create ();
243 pthread_mutex_lock (&orig->lock);
244 copy->head = md_entry_clone (orig->head);
245 pthread_mutex_unlock (&orig->lock);
248 } /* }}} meta_data_t *meta_data_clone */
250 void meta_data_destroy (meta_data_t *md) /* {{{ */
255 md_entry_free (md->head);
256 pthread_mutex_destroy (&md->lock);
258 } /* }}} void meta_data_destroy */
260 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
264 if ((md == NULL) || (key == NULL))
267 pthread_mutex_lock (&md->lock);
269 for (e = md->head; e != NULL; e = e->next)
271 if (strcasecmp (key, e->key) == 0)
273 pthread_mutex_unlock (&md->lock);
278 pthread_mutex_unlock (&md->lock);
280 } /* }}} int meta_data_exists */
282 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
286 if ((md == NULL) || (key == NULL))
289 pthread_mutex_lock (&md->lock);
291 for (e = md->head; e != NULL; e = e->next)
293 if (strcasecmp (key, e->key) == 0)
295 pthread_mutex_unlock (&md->lock);
300 pthread_mutex_unlock (&md->lock);
302 } /* }}} int meta_data_type */
304 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
306 int i = 0, count = 0;
309 if ((md == NULL) || (toc == NULL))
312 pthread_mutex_lock (&md->lock);
314 for (e = md->head; e != NULL; e = e->next)
319 pthread_mutex_unlock (&md->lock);
323 *toc = calloc(count, sizeof(**toc));
324 for (e = md->head; e != NULL; e = e->next)
325 (*toc)[i++] = strdup(e->key);
327 pthread_mutex_unlock (&md->lock);
329 } /* }}} int meta_data_toc */
331 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
336 if ((md == NULL) || (key == NULL))
339 pthread_mutex_lock (&md->lock);
345 if (strcasecmp (key, this->key) == 0)
354 pthread_mutex_unlock (&md->lock);
359 md->head = this->next;
361 prev->next = this->next;
363 pthread_mutex_unlock (&md->lock);
366 md_entry_free (this);
369 } /* }}} int meta_data_delete */
374 int meta_data_add_string (meta_data_t *md, /* {{{ */
375 const char *key, const char *value)
379 if ((md == NULL) || (key == NULL) || (value == NULL))
382 e = md_entry_alloc (key);
386 e->value.mv_string = md_strdup (value);
387 if (e->value.mv_string == NULL)
389 ERROR ("meta_data_add_string: md_strdup failed.");
393 e->type = MD_TYPE_STRING;
395 return (md_entry_insert (md, e));
396 } /* }}} int meta_data_add_string */
398 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
399 const char *key, int64_t value)
403 if ((md == NULL) || (key == NULL))
406 e = md_entry_alloc (key);
410 e->value.mv_signed_int = value;
411 e->type = MD_TYPE_SIGNED_INT;
413 return (md_entry_insert (md, e));
414 } /* }}} int meta_data_add_signed_int */
416 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
417 const char *key, uint64_t value)
421 if ((md == NULL) || (key == NULL))
424 e = md_entry_alloc (key);
428 e->value.mv_unsigned_int = value;
429 e->type = MD_TYPE_UNSIGNED_INT;
431 return (md_entry_insert (md, e));
432 } /* }}} int meta_data_add_unsigned_int */
434 int meta_data_add_double (meta_data_t *md, /* {{{ */
435 const char *key, double value)
439 if ((md == NULL) || (key == NULL))
442 e = md_entry_alloc (key);
446 e->value.mv_double = value;
447 e->type = MD_TYPE_DOUBLE;
449 return (md_entry_insert (md, e));
450 } /* }}} int meta_data_add_double */
452 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
453 const char *key, _Bool value)
457 if ((md == NULL) || (key == NULL))
460 e = md_entry_alloc (key);
464 e->value.mv_boolean = value;
465 e->type = MD_TYPE_BOOLEAN;
467 return (md_entry_insert (md, e));
468 } /* }}} int meta_data_add_boolean */
473 int meta_data_get_string (meta_data_t *md, /* {{{ */
474 const char *key, char **value)
479 if ((md == NULL) || (key == NULL) || (value == NULL))
482 pthread_mutex_lock (&md->lock);
484 e = md_entry_lookup (md, key);
487 pthread_mutex_unlock (&md->lock);
491 if (e->type != MD_TYPE_STRING)
493 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
494 pthread_mutex_unlock (&md->lock);
498 temp = md_strdup (e->value.mv_string);
501 pthread_mutex_unlock (&md->lock);
502 ERROR ("meta_data_get_string: md_strdup failed.");
506 pthread_mutex_unlock (&md->lock);
511 } /* }}} int meta_data_get_string */
513 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
514 const char *key, int64_t *value)
518 if ((md == NULL) || (key == NULL) || (value == NULL))
521 pthread_mutex_lock (&md->lock);
523 e = md_entry_lookup (md, key);
526 pthread_mutex_unlock (&md->lock);
530 if (e->type != MD_TYPE_SIGNED_INT)
532 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
533 pthread_mutex_unlock (&md->lock);
537 *value = e->value.mv_signed_int;
539 pthread_mutex_unlock (&md->lock);
541 } /* }}} int meta_data_get_signed_int */
543 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
544 const char *key, uint64_t *value)
548 if ((md == NULL) || (key == NULL) || (value == NULL))
551 pthread_mutex_lock (&md->lock);
553 e = md_entry_lookup (md, key);
556 pthread_mutex_unlock (&md->lock);
560 if (e->type != MD_TYPE_UNSIGNED_INT)
562 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
563 pthread_mutex_unlock (&md->lock);
567 *value = e->value.mv_unsigned_int;
569 pthread_mutex_unlock (&md->lock);
571 } /* }}} int meta_data_get_unsigned_int */
573 int meta_data_get_double (meta_data_t *md, /* {{{ */
574 const char *key, double *value)
578 if ((md == NULL) || (key == NULL) || (value == NULL))
581 pthread_mutex_lock (&md->lock);
583 e = md_entry_lookup (md, key);
586 pthread_mutex_unlock (&md->lock);
590 if (e->type != MD_TYPE_DOUBLE)
592 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
593 pthread_mutex_unlock (&md->lock);
597 *value = e->value.mv_double;
599 pthread_mutex_unlock (&md->lock);
601 } /* }}} int meta_data_get_double */
603 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
604 const char *key, _Bool *value)
608 if ((md == NULL) || (key == NULL) || (value == NULL))
611 pthread_mutex_lock (&md->lock);
613 e = md_entry_lookup (md, key);
616 pthread_mutex_unlock (&md->lock);
620 if (e->type != MD_TYPE_BOOLEAN)
622 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
623 pthread_mutex_unlock (&md->lock);
627 *value = e->value.mv_boolean;
629 pthread_mutex_unlock (&md->lock);
631 } /* }}} int meta_data_get_boolean */
633 /* vim: set sw=2 sts=2 et fdm=marker : */