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;
78 memcpy (dest, orig, sz);
81 } /* }}} char *md_strdup */
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
87 e = calloc (1, sizeof (*e));
90 ERROR ("md_entry_alloc: calloc failed.");
94 e->key = md_strdup (key);
98 ERROR ("md_entry_alloc: md_strdup failed.");
106 } /* }}} meta_entry_t *md_entry_alloc */
108 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
115 copy = md_entry_alloc (orig->key);
118 copy->type = orig->type;
119 if (copy->type == MD_TYPE_STRING)
120 copy->value.mv_string = strdup (orig->value.mv_string);
122 copy->value = orig->value;
124 copy->next = md_entry_clone (orig->next);
126 } /* }}} meta_entry_t *md_entry_clone */
128 static void md_entry_free (meta_entry_t *e) /* {{{ */
135 if (e->type == MD_TYPE_STRING)
136 free (e->value.mv_string);
139 md_entry_free (e->next);
142 } /* }}} void md_entry_free */
144 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
149 if ((md == NULL) || (e == NULL))
152 pthread_mutex_lock (&md->lock);
158 if (strcasecmp (e->key, this->key) == 0)
167 /* This key does not exist yet. */
168 if (md->head == NULL)
172 assert (prev != NULL);
178 else /* (this != NULL) */
185 e->next = this->next;
188 pthread_mutex_unlock (&md->lock);
193 md_entry_free (this);
197 } /* }}} int md_entry_insert */
199 /* XXX: The lock on md must be held while calling this function! */
200 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
205 if ((md == NULL) || (key == NULL))
208 for (e = md->head; e != NULL; e = e->next)
209 if (strcasecmp (key, e->key) == 0)
213 } /* }}} meta_entry_t *md_entry_lookup */
218 meta_data_t *meta_data_create (void) /* {{{ */
222 md = calloc (1, sizeof (*md));
225 ERROR ("meta_data_create: calloc failed.");
229 pthread_mutex_init (&md->lock, /* attr = */ NULL);
232 } /* }}} meta_data_t *meta_data_create */
234 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
241 copy = meta_data_create ();
245 pthread_mutex_lock (&orig->lock);
246 copy->head = md_entry_clone (orig->head);
247 pthread_mutex_unlock (&orig->lock);
250 } /* }}} meta_data_t *meta_data_clone */
252 void meta_data_destroy (meta_data_t *md) /* {{{ */
257 md_entry_free (md->head);
258 pthread_mutex_destroy (&md->lock);
260 } /* }}} void meta_data_destroy */
262 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
266 if ((md == NULL) || (key == NULL))
269 pthread_mutex_lock (&md->lock);
271 for (e = md->head; e != NULL; e = e->next)
273 if (strcasecmp (key, e->key) == 0)
275 pthread_mutex_unlock (&md->lock);
280 pthread_mutex_unlock (&md->lock);
282 } /* }}} int meta_data_exists */
284 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
288 if ((md == NULL) || (key == NULL))
291 pthread_mutex_lock (&md->lock);
293 for (e = md->head; e != NULL; e = e->next)
295 if (strcasecmp (key, e->key) == 0)
297 pthread_mutex_unlock (&md->lock);
302 pthread_mutex_unlock (&md->lock);
304 } /* }}} int meta_data_type */
306 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
308 int i = 0, count = 0;
311 if ((md == NULL) || (toc == NULL))
314 pthread_mutex_lock (&md->lock);
316 for (e = md->head; e != NULL; e = e->next)
321 pthread_mutex_unlock (&md->lock);
325 *toc = calloc(count, sizeof(**toc));
326 for (e = md->head; e != NULL; e = e->next)
327 (*toc)[i++] = strdup(e->key);
329 pthread_mutex_unlock (&md->lock);
331 } /* }}} int meta_data_toc */
333 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
338 if ((md == NULL) || (key == NULL))
341 pthread_mutex_lock (&md->lock);
347 if (strcasecmp (key, this->key) == 0)
356 pthread_mutex_unlock (&md->lock);
361 md->head = this->next;
363 prev->next = this->next;
365 pthread_mutex_unlock (&md->lock);
368 md_entry_free (this);
371 } /* }}} int meta_data_delete */
376 int meta_data_add_string (meta_data_t *md, /* {{{ */
377 const char *key, const char *value)
381 if ((md == NULL) || (key == NULL) || (value == NULL))
384 e = md_entry_alloc (key);
388 e->value.mv_string = md_strdup (value);
389 if (e->value.mv_string == NULL)
391 ERROR ("meta_data_add_string: md_strdup failed.");
395 e->type = MD_TYPE_STRING;
397 return (md_entry_insert (md, e));
398 } /* }}} int meta_data_add_string */
400 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
401 const char *key, int64_t value)
405 if ((md == NULL) || (key == NULL))
408 e = md_entry_alloc (key);
412 e->value.mv_signed_int = value;
413 e->type = MD_TYPE_SIGNED_INT;
415 return (md_entry_insert (md, e));
416 } /* }}} int meta_data_add_signed_int */
418 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
419 const char *key, uint64_t value)
423 if ((md == NULL) || (key == NULL))
426 e = md_entry_alloc (key);
430 e->value.mv_unsigned_int = value;
431 e->type = MD_TYPE_UNSIGNED_INT;
433 return (md_entry_insert (md, e));
434 } /* }}} int meta_data_add_unsigned_int */
436 int meta_data_add_double (meta_data_t *md, /* {{{ */
437 const char *key, double value)
441 if ((md == NULL) || (key == NULL))
444 e = md_entry_alloc (key);
448 e->value.mv_double = value;
449 e->type = MD_TYPE_DOUBLE;
451 return (md_entry_insert (md, e));
452 } /* }}} int meta_data_add_double */
454 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
455 const char *key, _Bool value)
459 if ((md == NULL) || (key == NULL))
462 e = md_entry_alloc (key);
466 e->value.mv_boolean = value;
467 e->type = MD_TYPE_BOOLEAN;
469 return (md_entry_insert (md, e));
470 } /* }}} int meta_data_add_boolean */
475 int meta_data_get_string (meta_data_t *md, /* {{{ */
476 const char *key, char **value)
481 if ((md == NULL) || (key == NULL) || (value == NULL))
484 pthread_mutex_lock (&md->lock);
486 e = md_entry_lookup (md, key);
489 pthread_mutex_unlock (&md->lock);
493 if (e->type != MD_TYPE_STRING)
495 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
496 pthread_mutex_unlock (&md->lock);
500 temp = md_strdup (e->value.mv_string);
503 pthread_mutex_unlock (&md->lock);
504 ERROR ("meta_data_get_string: md_strdup failed.");
508 pthread_mutex_unlock (&md->lock);
513 } /* }}} int meta_data_get_string */
515 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
516 const char *key, int64_t *value)
520 if ((md == NULL) || (key == NULL) || (value == NULL))
523 pthread_mutex_lock (&md->lock);
525 e = md_entry_lookup (md, key);
528 pthread_mutex_unlock (&md->lock);
532 if (e->type != MD_TYPE_SIGNED_INT)
534 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
535 pthread_mutex_unlock (&md->lock);
539 *value = e->value.mv_signed_int;
541 pthread_mutex_unlock (&md->lock);
543 } /* }}} int meta_data_get_signed_int */
545 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
546 const char *key, uint64_t *value)
550 if ((md == NULL) || (key == NULL) || (value == NULL))
553 pthread_mutex_lock (&md->lock);
555 e = md_entry_lookup (md, key);
558 pthread_mutex_unlock (&md->lock);
562 if (e->type != MD_TYPE_UNSIGNED_INT)
564 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
565 pthread_mutex_unlock (&md->lock);
569 *value = e->value.mv_unsigned_int;
571 pthread_mutex_unlock (&md->lock);
573 } /* }}} int meta_data_get_unsigned_int */
575 int meta_data_get_double (meta_data_t *md, /* {{{ */
576 const char *key, double *value)
580 if ((md == NULL) || (key == NULL) || (value == NULL))
583 pthread_mutex_lock (&md->lock);
585 e = md_entry_lookup (md, key);
588 pthread_mutex_unlock (&md->lock);
592 if (e->type != MD_TYPE_DOUBLE)
594 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
595 pthread_mutex_unlock (&md->lock);
599 *value = e->value.mv_double;
601 pthread_mutex_unlock (&md->lock);
603 } /* }}} int meta_data_get_double */
605 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
606 const char *key, _Bool *value)
610 if ((md == NULL) || (key == NULL) || (value == NULL))
613 pthread_mutex_lock (&md->lock);
615 e = md_entry_lookup (md, key);
618 pthread_mutex_unlock (&md->lock);
622 if (e->type != MD_TYPE_BOOLEAN)
624 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
625 pthread_mutex_unlock (&md->lock);
629 *value = e->value.mv_boolean;
631 pthread_mutex_unlock (&md->lock);
633 } /* }}} int meta_data_get_boolean */
635 /* vim: set sw=2 sts=2 et fdm=marker : */