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);
219 } /* }}} void meta_data_destroy */
221 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
225 if ((md == NULL) || (key == NULL))
228 pthread_mutex_lock (&md->lock);
230 for (e = md->head; e != NULL; e = e->next)
232 if (strcasecmp (key, e->key) == 0)
234 pthread_mutex_unlock (&md->lock);
239 pthread_mutex_unlock (&md->lock);
241 } /* }}} int meta_data_exists */
243 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
247 if ((md == NULL) || (key == NULL))
250 pthread_mutex_lock (&md->lock);
252 for (e = md->head; e != NULL; e = e->next)
254 if (strcasecmp (key, e->key) == 0)
256 pthread_mutex_unlock (&md->lock);
261 pthread_mutex_unlock (&md->lock);
263 } /* }}} int meta_data_type */
265 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
267 int i = 0, count = 0;
270 if ((md == NULL) || (toc == NULL))
273 pthread_mutex_lock (&md->lock);
275 for (e = md->head; e != NULL; e = e->next)
278 *toc = malloc(count * sizeof(**toc));
279 for (e = md->head; e != NULL; e = e->next)
280 (*toc)[i++] = strdup(e->key);
282 pthread_mutex_unlock (&md->lock);
284 } /* }}} int meta_data_toc */
286 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
291 if ((md == NULL) || (key == NULL))
294 pthread_mutex_lock (&md->lock);
300 if (strcasecmp (key, this->key) == 0)
309 pthread_mutex_unlock (&md->lock);
314 md->head = this->next;
316 prev->next = this->next;
318 pthread_mutex_unlock (&md->lock);
321 md_entry_free (this);
324 } /* }}} int meta_data_delete */
329 int meta_data_add_string (meta_data_t *md, /* {{{ */
330 const char *key, const char *value)
334 if ((md == NULL) || (key == NULL) || (value == NULL))
337 e = md_entry_alloc (key);
341 e->value.mv_string = md_strdup (value);
342 if (e->value.mv_string == NULL)
344 ERROR ("meta_data_add_string: md_strdup failed.");
348 e->type = MD_TYPE_STRING;
350 return (md_entry_insert (md, e));
351 } /* }}} int meta_data_add_string */
353 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
354 const char *key, int64_t value)
358 if ((md == NULL) || (key == NULL))
361 e = md_entry_alloc (key);
365 e->value.mv_signed_int = value;
366 e->type = MD_TYPE_SIGNED_INT;
368 return (md_entry_insert (md, e));
369 } /* }}} int meta_data_add_signed_int */
371 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
372 const char *key, uint64_t value)
376 if ((md == NULL) || (key == NULL))
379 e = md_entry_alloc (key);
383 e->value.mv_unsigned_int = value;
384 e->type = MD_TYPE_UNSIGNED_INT;
386 return (md_entry_insert (md, e));
387 } /* }}} int meta_data_add_unsigned_int */
389 int meta_data_add_double (meta_data_t *md, /* {{{ */
390 const char *key, double value)
394 if ((md == NULL) || (key == NULL))
397 e = md_entry_alloc (key);
401 e->value.mv_double = value;
402 e->type = MD_TYPE_DOUBLE;
404 return (md_entry_insert (md, e));
405 } /* }}} int meta_data_add_double */
407 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
408 const char *key, _Bool value)
412 if ((md == NULL) || (key == NULL))
415 e = md_entry_alloc (key);
419 e->value.mv_boolean = value;
420 e->type = MD_TYPE_BOOLEAN;
422 return (md_entry_insert (md, e));
423 } /* }}} int meta_data_add_boolean */
428 int meta_data_get_string (meta_data_t *md, /* {{{ */
429 const char *key, char **value)
434 if ((md == NULL) || (key == NULL) || (value == NULL))
437 pthread_mutex_lock (&md->lock);
439 e = md_entry_lookup (md, key);
442 pthread_mutex_unlock (&md->lock);
446 if (e->type != MD_TYPE_STRING)
448 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
449 pthread_mutex_unlock (&md->lock);
453 temp = md_strdup (e->value.mv_string);
456 pthread_mutex_unlock (&md->lock);
457 ERROR ("meta_data_get_string: md_strdup failed.");
461 pthread_mutex_unlock (&md->lock);
466 } /* }}} int meta_data_get_string */
468 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
469 const char *key, int64_t *value)
473 if ((md == NULL) || (key == NULL) || (value == NULL))
476 pthread_mutex_lock (&md->lock);
478 e = md_entry_lookup (md, key);
481 pthread_mutex_unlock (&md->lock);
485 if (e->type != MD_TYPE_SIGNED_INT)
487 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
488 pthread_mutex_unlock (&md->lock);
492 *value = e->value.mv_signed_int;
494 pthread_mutex_unlock (&md->lock);
496 } /* }}} int meta_data_get_signed_int */
498 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
499 const char *key, uint64_t *value)
503 if ((md == NULL) || (key == NULL) || (value == NULL))
506 pthread_mutex_lock (&md->lock);
508 e = md_entry_lookup (md, key);
511 pthread_mutex_unlock (&md->lock);
515 if (e->type != MD_TYPE_UNSIGNED_INT)
517 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
518 pthread_mutex_unlock (&md->lock);
522 *value = e->value.mv_unsigned_int;
524 pthread_mutex_unlock (&md->lock);
526 } /* }}} int meta_data_get_unsigned_int */
528 int meta_data_get_double (meta_data_t *md, /* {{{ */
529 const char *key, double *value)
533 if ((md == NULL) || (key == NULL) || (value == NULL))
536 pthread_mutex_lock (&md->lock);
538 e = md_entry_lookup (md, key);
541 pthread_mutex_unlock (&md->lock);
545 if (e->type != MD_TYPE_DOUBLE)
547 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
548 pthread_mutex_unlock (&md->lock);
552 *value = e->value.mv_double;
554 pthread_mutex_unlock (&md->lock);
556 } /* }}} int meta_data_get_double */
558 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
559 const char *key, _Bool *value)
563 if ((md == NULL) || (key == NULL) || (value == NULL))
566 pthread_mutex_lock (&md->lock);
568 e = md_entry_lookup (md, key);
571 pthread_mutex_unlock (&md->lock);
575 if (e->type != MD_TYPE_BOOLEAN)
577 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
578 pthread_mutex_unlock (&md->lock);
582 *value = e->value.mv_boolean;
584 pthread_mutex_unlock (&md->lock);
586 } /* }}} int meta_data_get_boolean */
588 /* vim: set sw=2 sts=2 et fdm=marker : */