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)
321 *toc = malloc(count * sizeof(**toc));
322 for (e = md->head; e != NULL; e = e->next)
323 (*toc)[i++] = strdup(e->key);
325 pthread_mutex_unlock (&md->lock);
327 } /* }}} int meta_data_toc */
329 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
334 if ((md == NULL) || (key == NULL))
337 pthread_mutex_lock (&md->lock);
343 if (strcasecmp (key, this->key) == 0)
352 pthread_mutex_unlock (&md->lock);
357 md->head = this->next;
359 prev->next = this->next;
361 pthread_mutex_unlock (&md->lock);
364 md_entry_free (this);
367 } /* }}} int meta_data_delete */
372 int meta_data_add_string (meta_data_t *md, /* {{{ */
373 const char *key, const char *value)
377 if ((md == NULL) || (key == NULL) || (value == NULL))
380 e = md_entry_alloc (key);
384 e->value.mv_string = md_strdup (value);
385 if (e->value.mv_string == NULL)
387 ERROR ("meta_data_add_string: md_strdup failed.");
391 e->type = MD_TYPE_STRING;
393 return (md_entry_insert (md, e));
394 } /* }}} int meta_data_add_string */
396 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
397 const char *key, int64_t value)
401 if ((md == NULL) || (key == NULL))
404 e = md_entry_alloc (key);
408 e->value.mv_signed_int = value;
409 e->type = MD_TYPE_SIGNED_INT;
411 return (md_entry_insert (md, e));
412 } /* }}} int meta_data_add_signed_int */
414 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
415 const char *key, uint64_t value)
419 if ((md == NULL) || (key == NULL))
422 e = md_entry_alloc (key);
426 e->value.mv_unsigned_int = value;
427 e->type = MD_TYPE_UNSIGNED_INT;
429 return (md_entry_insert (md, e));
430 } /* }}} int meta_data_add_unsigned_int */
432 int meta_data_add_double (meta_data_t *md, /* {{{ */
433 const char *key, double value)
437 if ((md == NULL) || (key == NULL))
440 e = md_entry_alloc (key);
444 e->value.mv_double = value;
445 e->type = MD_TYPE_DOUBLE;
447 return (md_entry_insert (md, e));
448 } /* }}} int meta_data_add_double */
450 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
451 const char *key, _Bool value)
455 if ((md == NULL) || (key == NULL))
458 e = md_entry_alloc (key);
462 e->value.mv_boolean = value;
463 e->type = MD_TYPE_BOOLEAN;
465 return (md_entry_insert (md, e));
466 } /* }}} int meta_data_add_boolean */
471 int meta_data_get_string (meta_data_t *md, /* {{{ */
472 const char *key, char **value)
477 if ((md == NULL) || (key == NULL) || (value == NULL))
480 pthread_mutex_lock (&md->lock);
482 e = md_entry_lookup (md, key);
485 pthread_mutex_unlock (&md->lock);
489 if (e->type != MD_TYPE_STRING)
491 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
492 pthread_mutex_unlock (&md->lock);
496 temp = md_strdup (e->value.mv_string);
499 pthread_mutex_unlock (&md->lock);
500 ERROR ("meta_data_get_string: md_strdup failed.");
504 pthread_mutex_unlock (&md->lock);
509 } /* }}} int meta_data_get_string */
511 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
512 const char *key, int64_t *value)
516 if ((md == NULL) || (key == NULL) || (value == NULL))
519 pthread_mutex_lock (&md->lock);
521 e = md_entry_lookup (md, key);
524 pthread_mutex_unlock (&md->lock);
528 if (e->type != MD_TYPE_SIGNED_INT)
530 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
531 pthread_mutex_unlock (&md->lock);
535 *value = e->value.mv_signed_int;
537 pthread_mutex_unlock (&md->lock);
539 } /* }}} int meta_data_get_signed_int */
541 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
542 const char *key, uint64_t *value)
546 if ((md == NULL) || (key == NULL) || (value == NULL))
549 pthread_mutex_lock (&md->lock);
551 e = md_entry_lookup (md, key);
554 pthread_mutex_unlock (&md->lock);
558 if (e->type != MD_TYPE_UNSIGNED_INT)
560 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
561 pthread_mutex_unlock (&md->lock);
565 *value = e->value.mv_unsigned_int;
567 pthread_mutex_unlock (&md->lock);
569 } /* }}} int meta_data_get_unsigned_int */
571 int meta_data_get_double (meta_data_t *md, /* {{{ */
572 const char *key, double *value)
576 if ((md == NULL) || (key == NULL) || (value == NULL))
579 pthread_mutex_lock (&md->lock);
581 e = md_entry_lookup (md, key);
584 pthread_mutex_unlock (&md->lock);
588 if (e->type != MD_TYPE_DOUBLE)
590 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
591 pthread_mutex_unlock (&md->lock);
595 *value = e->value.mv_double;
597 pthread_mutex_unlock (&md->lock);
599 } /* }}} int meta_data_get_double */
601 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
602 const char *key, _Bool *value)
606 if ((md == NULL) || (key == NULL) || (value == NULL))
609 pthread_mutex_lock (&md->lock);
611 e = md_entry_lookup (md, key);
614 pthread_mutex_unlock (&md->lock);
618 if (e->type != MD_TYPE_BOOLEAN)
620 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
621 pthread_mutex_unlock (&md->lock);
625 *value = e->value.mv_boolean;
627 pthread_mutex_unlock (&md->lock);
629 } /* }}} int meta_data_get_boolean */
631 /* vim: set sw=2 sts=2 et fdm=marker : */