2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
29 #include "meta_data.h"
39 int64_t mv_signed_int;
40 uint64_t mv_unsigned_int;
44 typedef union meta_value_u meta_value_t;
47 typedef struct meta_entry_s meta_entry_t;
65 static char *md_strdup (const char *orig) /* {{{ */
73 sz = strlen (orig) + 1;
74 dest = (char *) malloc (sz);
78 memcpy (dest, orig, sz);
81 } /* }}} char *md_strdup */
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
87 e = (meta_entry_t *) malloc (sizeof (*e));
90 ERROR ("md_entry_alloc: malloc failed.");
93 memset (e, 0, sizeof (*e));
95 e->key = md_strdup (key);
99 ERROR ("md_entry_alloc: md_strdup failed.");
107 } /* }}} meta_entry_t *md_entry_alloc */
109 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
116 copy = md_entry_alloc (orig->key);
117 copy->type = orig->type;
118 if (copy->type == MD_TYPE_STRING)
119 copy->value.mv_string = strdup (orig->value.mv_string);
121 copy->value = orig->value;
123 copy->next = md_entry_clone (orig->next);
125 } /* }}} meta_entry_t *md_entry_clone */
127 static void md_entry_free (meta_entry_t *e) /* {{{ */
134 if (e->type == MD_TYPE_STRING)
135 free (e->value.mv_string);
138 md_entry_free (e->next);
141 } /* }}} void md_entry_free */
143 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
148 if ((md == NULL) || (e == NULL))
151 pthread_mutex_lock (&md->lock);
157 if (strcasecmp (e->key, this->key) == 0)
166 /* This key does not exist yet. */
167 if (md->head == NULL)
171 assert (prev != NULL);
177 else /* (this != NULL) */
184 e->next = this->next;
187 pthread_mutex_unlock (&md->lock);
192 md_entry_free (this);
196 } /* }}} int md_entry_insert */
198 /* XXX: The lock on md must be held while calling this function! */
199 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
204 if ((md == NULL) || (key == NULL))
207 for (e = md->head; e != NULL; e = e->next)
208 if (strcasecmp (key, e->key) == 0)
212 } /* }}} meta_entry_t *md_entry_lookup */
217 meta_data_t *meta_data_create (void) /* {{{ */
221 md = (meta_data_t *) malloc (sizeof (*md));
224 ERROR ("meta_data_create: malloc failed.");
227 memset (md, 0, sizeof (*md));
230 pthread_mutex_init (&md->lock, /* attr = */ NULL);
233 } /* }}} meta_data_t *meta_data_create */
235 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
242 copy = meta_data_create ();
246 pthread_mutex_lock (&orig->lock);
247 copy->head = md_entry_clone (orig->head);
248 pthread_mutex_unlock (&orig->lock);
251 } /* }}} meta_data_t *meta_data_clone */
253 void meta_data_destroy (meta_data_t *md) /* {{{ */
258 md_entry_free (md->head);
259 pthread_mutex_destroy (&md->lock);
261 } /* }}} void meta_data_destroy */
263 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
267 if ((md == NULL) || (key == NULL))
270 pthread_mutex_lock (&md->lock);
272 for (e = md->head; e != NULL; e = e->next)
274 if (strcasecmp (key, e->key) == 0)
276 pthread_mutex_unlock (&md->lock);
281 pthread_mutex_unlock (&md->lock);
283 } /* }}} int meta_data_exists */
285 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
289 if ((md == NULL) || (key == NULL))
292 pthread_mutex_lock (&md->lock);
294 for (e = md->head; e != NULL; e = e->next)
296 if (strcasecmp (key, e->key) == 0)
298 pthread_mutex_unlock (&md->lock);
303 pthread_mutex_unlock (&md->lock);
305 } /* }}} int meta_data_type */
307 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
309 int i = 0, count = 0;
312 if ((md == NULL) || (toc == NULL))
315 pthread_mutex_lock (&md->lock);
317 for (e = md->head; e != NULL; e = e->next)
322 pthread_mutex_unlock (&md->lock);
326 *toc = calloc(count, sizeof(**toc));
327 for (e = md->head; e != NULL; e = e->next)
328 (*toc)[i++] = strdup(e->key);
330 pthread_mutex_unlock (&md->lock);
332 } /* }}} int meta_data_toc */
334 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
339 if ((md == NULL) || (key == NULL))
342 pthread_mutex_lock (&md->lock);
348 if (strcasecmp (key, this->key) == 0)
357 pthread_mutex_unlock (&md->lock);
362 md->head = this->next;
364 prev->next = this->next;
366 pthread_mutex_unlock (&md->lock);
369 md_entry_free (this);
372 } /* }}} int meta_data_delete */
377 int meta_data_add_string (meta_data_t *md, /* {{{ */
378 const char *key, const char *value)
382 if ((md == NULL) || (key == NULL) || (value == NULL))
385 e = md_entry_alloc (key);
389 e->value.mv_string = md_strdup (value);
390 if (e->value.mv_string == NULL)
392 ERROR ("meta_data_add_string: md_strdup failed.");
396 e->type = MD_TYPE_STRING;
398 return (md_entry_insert (md, e));
399 } /* }}} int meta_data_add_string */
401 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
402 const char *key, int64_t value)
406 if ((md == NULL) || (key == NULL))
409 e = md_entry_alloc (key);
413 e->value.mv_signed_int = value;
414 e->type = MD_TYPE_SIGNED_INT;
416 return (md_entry_insert (md, e));
417 } /* }}} int meta_data_add_signed_int */
419 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
420 const char *key, uint64_t value)
424 if ((md == NULL) || (key == NULL))
427 e = md_entry_alloc (key);
431 e->value.mv_unsigned_int = value;
432 e->type = MD_TYPE_UNSIGNED_INT;
434 return (md_entry_insert (md, e));
435 } /* }}} int meta_data_add_unsigned_int */
437 int meta_data_add_double (meta_data_t *md, /* {{{ */
438 const char *key, double value)
442 if ((md == NULL) || (key == NULL))
445 e = md_entry_alloc (key);
449 e->value.mv_double = value;
450 e->type = MD_TYPE_DOUBLE;
452 return (md_entry_insert (md, e));
453 } /* }}} int meta_data_add_double */
455 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
456 const char *key, _Bool value)
460 if ((md == NULL) || (key == NULL))
463 e = md_entry_alloc (key);
467 e->value.mv_boolean = value;
468 e->type = MD_TYPE_BOOLEAN;
470 return (md_entry_insert (md, e));
471 } /* }}} int meta_data_add_boolean */
476 int meta_data_get_string (meta_data_t *md, /* {{{ */
477 const char *key, char **value)
482 if ((md == NULL) || (key == NULL) || (value == NULL))
485 pthread_mutex_lock (&md->lock);
487 e = md_entry_lookup (md, key);
490 pthread_mutex_unlock (&md->lock);
494 if (e->type != MD_TYPE_STRING)
496 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
497 pthread_mutex_unlock (&md->lock);
501 temp = md_strdup (e->value.mv_string);
504 pthread_mutex_unlock (&md->lock);
505 ERROR ("meta_data_get_string: md_strdup failed.");
509 pthread_mutex_unlock (&md->lock);
514 } /* }}} int meta_data_get_string */
516 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
517 const char *key, int64_t *value)
521 if ((md == NULL) || (key == NULL) || (value == NULL))
524 pthread_mutex_lock (&md->lock);
526 e = md_entry_lookup (md, key);
529 pthread_mutex_unlock (&md->lock);
533 if (e->type != MD_TYPE_SIGNED_INT)
535 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
536 pthread_mutex_unlock (&md->lock);
540 *value = e->value.mv_signed_int;
542 pthread_mutex_unlock (&md->lock);
544 } /* }}} int meta_data_get_signed_int */
546 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
547 const char *key, uint64_t *value)
551 if ((md == NULL) || (key == NULL) || (value == NULL))
554 pthread_mutex_lock (&md->lock);
556 e = md_entry_lookup (md, key);
559 pthread_mutex_unlock (&md->lock);
563 if (e->type != MD_TYPE_UNSIGNED_INT)
565 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
566 pthread_mutex_unlock (&md->lock);
570 *value = e->value.mv_unsigned_int;
572 pthread_mutex_unlock (&md->lock);
574 } /* }}} int meta_data_get_unsigned_int */
576 int meta_data_get_double (meta_data_t *md, /* {{{ */
577 const char *key, double *value)
581 if ((md == NULL) || (key == NULL) || (value == NULL))
584 pthread_mutex_lock (&md->lock);
586 e = md_entry_lookup (md, key);
589 pthread_mutex_unlock (&md->lock);
593 if (e->type != MD_TYPE_DOUBLE)
595 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
596 pthread_mutex_unlock (&md->lock);
600 *value = e->value.mv_double;
602 pthread_mutex_unlock (&md->lock);
604 } /* }}} int meta_data_get_double */
606 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
607 const char *key, _Bool *value)
611 if ((md == NULL) || (key == NULL) || (value == NULL))
614 pthread_mutex_lock (&md->lock);
616 e = md_entry_lookup (md, key);
619 pthread_mutex_unlock (&md->lock);
623 if (e->type != MD_TYPE_BOOLEAN)
625 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
626 pthread_mutex_unlock (&md->lock);
630 *value = e->value.mv_boolean;
632 pthread_mutex_unlock (&md->lock);
634 } /* }}} int meta_data_get_boolean */
636 /* vim: set sw=2 sts=2 et fdm=marker : */