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 pthread_mutex_destroy(&md->lock);
259 md_entry_free (md->head);
260 pthread_mutex_destroy (&md->lock);
262 } /* }}} void meta_data_destroy */
264 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
268 if ((md == NULL) || (key == NULL))
271 pthread_mutex_lock (&md->lock);
273 for (e = md->head; e != NULL; e = e->next)
275 if (strcasecmp (key, e->key) == 0)
277 pthread_mutex_unlock (&md->lock);
282 pthread_mutex_unlock (&md->lock);
284 } /* }}} int meta_data_exists */
286 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
290 if ((md == NULL) || (key == NULL))
293 pthread_mutex_lock (&md->lock);
295 for (e = md->head; e != NULL; e = e->next)
297 if (strcasecmp (key, e->key) == 0)
299 pthread_mutex_unlock (&md->lock);
304 pthread_mutex_unlock (&md->lock);
306 } /* }}} int meta_data_type */
308 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
310 int i = 0, count = 0;
313 if ((md == NULL) || (toc == NULL))
316 pthread_mutex_lock (&md->lock);
318 for (e = md->head; e != NULL; e = e->next)
323 pthread_mutex_unlock (&md->lock);
327 *toc = calloc(count, sizeof(**toc));
328 for (e = md->head; e != NULL; e = e->next)
329 (*toc)[i++] = strdup(e->key);
331 pthread_mutex_unlock (&md->lock);
333 } /* }}} int meta_data_toc */
335 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
340 if ((md == NULL) || (key == NULL))
343 pthread_mutex_lock (&md->lock);
349 if (strcasecmp (key, this->key) == 0)
358 pthread_mutex_unlock (&md->lock);
363 md->head = this->next;
365 prev->next = this->next;
367 pthread_mutex_unlock (&md->lock);
370 md_entry_free (this);
373 } /* }}} int meta_data_delete */
378 int meta_data_add_string (meta_data_t *md, /* {{{ */
379 const char *key, const char *value)
383 if ((md == NULL) || (key == NULL) || (value == NULL))
386 e = md_entry_alloc (key);
390 e->value.mv_string = md_strdup (value);
391 if (e->value.mv_string == NULL)
393 ERROR ("meta_data_add_string: md_strdup failed.");
397 e->type = MD_TYPE_STRING;
399 return (md_entry_insert (md, e));
400 } /* }}} int meta_data_add_string */
402 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
403 const char *key, int64_t value)
407 if ((md == NULL) || (key == NULL))
410 e = md_entry_alloc (key);
414 e->value.mv_signed_int = value;
415 e->type = MD_TYPE_SIGNED_INT;
417 return (md_entry_insert (md, e));
418 } /* }}} int meta_data_add_signed_int */
420 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
421 const char *key, uint64_t value)
425 if ((md == NULL) || (key == NULL))
428 e = md_entry_alloc (key);
432 e->value.mv_unsigned_int = value;
433 e->type = MD_TYPE_UNSIGNED_INT;
435 return (md_entry_insert (md, e));
436 } /* }}} int meta_data_add_unsigned_int */
438 int meta_data_add_double (meta_data_t *md, /* {{{ */
439 const char *key, double value)
443 if ((md == NULL) || (key == NULL))
446 e = md_entry_alloc (key);
450 e->value.mv_double = value;
451 e->type = MD_TYPE_DOUBLE;
453 return (md_entry_insert (md, e));
454 } /* }}} int meta_data_add_double */
456 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
457 const char *key, _Bool value)
461 if ((md == NULL) || (key == NULL))
464 e = md_entry_alloc (key);
468 e->value.mv_boolean = value;
469 e->type = MD_TYPE_BOOLEAN;
471 return (md_entry_insert (md, e));
472 } /* }}} int meta_data_add_boolean */
477 int meta_data_get_string (meta_data_t *md, /* {{{ */
478 const char *key, char **value)
483 if ((md == NULL) || (key == NULL) || (value == NULL))
486 pthread_mutex_lock (&md->lock);
488 e = md_entry_lookup (md, key);
491 pthread_mutex_unlock (&md->lock);
495 if (e->type != MD_TYPE_STRING)
497 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
498 pthread_mutex_unlock (&md->lock);
502 temp = md_strdup (e->value.mv_string);
505 pthread_mutex_unlock (&md->lock);
506 ERROR ("meta_data_get_string: md_strdup failed.");
510 pthread_mutex_unlock (&md->lock);
515 } /* }}} int meta_data_get_string */
517 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
518 const char *key, int64_t *value)
522 if ((md == NULL) || (key == NULL) || (value == NULL))
525 pthread_mutex_lock (&md->lock);
527 e = md_entry_lookup (md, key);
530 pthread_mutex_unlock (&md->lock);
534 if (e->type != MD_TYPE_SIGNED_INT)
536 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
537 pthread_mutex_unlock (&md->lock);
541 *value = e->value.mv_signed_int;
543 pthread_mutex_unlock (&md->lock);
545 } /* }}} int meta_data_get_signed_int */
547 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
548 const char *key, uint64_t *value)
552 if ((md == NULL) || (key == NULL) || (value == NULL))
555 pthread_mutex_lock (&md->lock);
557 e = md_entry_lookup (md, key);
560 pthread_mutex_unlock (&md->lock);
564 if (e->type != MD_TYPE_UNSIGNED_INT)
566 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
567 pthread_mutex_unlock (&md->lock);
571 *value = e->value.mv_unsigned_int;
573 pthread_mutex_unlock (&md->lock);
575 } /* }}} int meta_data_get_unsigned_int */
577 int meta_data_get_double (meta_data_t *md, /* {{{ */
578 const char *key, double *value)
582 if ((md == NULL) || (key == NULL) || (value == NULL))
585 pthread_mutex_lock (&md->lock);
587 e = md_entry_lookup (md, key);
590 pthread_mutex_unlock (&md->lock);
594 if (e->type != MD_TYPE_DOUBLE)
596 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
597 pthread_mutex_unlock (&md->lock);
601 *value = e->value.mv_double;
603 pthread_mutex_unlock (&md->lock);
605 } /* }}} int meta_data_get_double */
607 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
608 const char *key, _Bool *value)
612 if ((md == NULL) || (key == NULL) || (value == NULL))
615 pthread_mutex_lock (&md->lock);
617 e = md_entry_lookup (md, key);
620 pthread_mutex_unlock (&md->lock);
624 if (e->type != MD_TYPE_BOOLEAN)
626 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
627 pthread_mutex_unlock (&md->lock);
631 *value = e->value.mv_boolean;
633 pthread_mutex_unlock (&md->lock);
635 } /* }}} int meta_data_get_boolean */
637 /* vim: set sw=2 sts=2 et fdm=marker : */