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);
119 copy->type = orig->type;
120 if (copy->type == MD_TYPE_STRING)
121 copy->value.mv_string = strdup (orig->value.mv_string);
123 copy->value = orig->value;
125 copy->next = md_entry_clone (orig->next);
127 } /* }}} meta_entry_t *md_entry_clone */
129 static void md_entry_free (meta_entry_t *e) /* {{{ */
136 if (e->type == MD_TYPE_STRING)
137 free (e->value.mv_string);
140 md_entry_free (e->next);
143 } /* }}} void md_entry_free */
145 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
150 if ((md == NULL) || (e == NULL))
153 pthread_mutex_lock (&md->lock);
159 if (strcasecmp (e->key, this->key) == 0)
168 /* This key does not exist yet. */
169 if (md->head == NULL)
173 assert (prev != NULL);
179 else /* (this != NULL) */
186 e->next = this->next;
189 pthread_mutex_unlock (&md->lock);
194 md_entry_free (this);
198 } /* }}} int md_entry_insert */
200 /* XXX: The lock on md must be held while calling this function! */
201 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
206 if ((md == NULL) || (key == NULL))
209 for (e = md->head; e != NULL; e = e->next)
210 if (strcasecmp (key, e->key) == 0)
214 } /* }}} meta_entry_t *md_entry_lookup */
219 meta_data_t *meta_data_create (void) /* {{{ */
223 md = (meta_data_t *) malloc (sizeof (*md));
226 ERROR ("meta_data_create: malloc failed.");
229 memset (md, 0, sizeof (*md));
232 pthread_mutex_init (&md->lock, /* attr = */ NULL);
235 } /* }}} meta_data_t *meta_data_create */
237 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
244 copy = meta_data_create ();
248 pthread_mutex_lock (&orig->lock);
249 copy->head = md_entry_clone (orig->head);
250 pthread_mutex_unlock (&orig->lock);
253 } /* }}} meta_data_t *meta_data_clone */
255 void meta_data_destroy (meta_data_t *md) /* {{{ */
260 md_entry_free (md->head);
261 pthread_mutex_destroy (&md->lock);
263 } /* }}} void meta_data_destroy */
265 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
269 if ((md == NULL) || (key == NULL))
272 pthread_mutex_lock (&md->lock);
274 for (e = md->head; e != NULL; e = e->next)
276 if (strcasecmp (key, e->key) == 0)
278 pthread_mutex_unlock (&md->lock);
283 pthread_mutex_unlock (&md->lock);
285 } /* }}} int meta_data_exists */
287 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
291 if ((md == NULL) || (key == NULL))
294 pthread_mutex_lock (&md->lock);
296 for (e = md->head; e != NULL; e = e->next)
298 if (strcasecmp (key, e->key) == 0)
300 pthread_mutex_unlock (&md->lock);
305 pthread_mutex_unlock (&md->lock);
307 } /* }}} int meta_data_type */
309 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
311 int i = 0, count = 0;
314 if ((md == NULL) || (toc == NULL))
317 pthread_mutex_lock (&md->lock);
319 for (e = md->head; e != NULL; e = e->next)
324 pthread_mutex_unlock (&md->lock);
328 *toc = calloc(count, sizeof(**toc));
329 for (e = md->head; e != NULL; e = e->next)
330 (*toc)[i++] = strdup(e->key);
332 pthread_mutex_unlock (&md->lock);
334 } /* }}} int meta_data_toc */
336 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
341 if ((md == NULL) || (key == NULL))
344 pthread_mutex_lock (&md->lock);
350 if (strcasecmp (key, this->key) == 0)
359 pthread_mutex_unlock (&md->lock);
364 md->head = this->next;
366 prev->next = this->next;
368 pthread_mutex_unlock (&md->lock);
371 md_entry_free (this);
374 } /* }}} int meta_data_delete */
379 int meta_data_add_string (meta_data_t *md, /* {{{ */
380 const char *key, const char *value)
384 if ((md == NULL) || (key == NULL) || (value == NULL))
387 e = md_entry_alloc (key);
391 e->value.mv_string = md_strdup (value);
392 if (e->value.mv_string == NULL)
394 ERROR ("meta_data_add_string: md_strdup failed.");
398 e->type = MD_TYPE_STRING;
400 return (md_entry_insert (md, e));
401 } /* }}} int meta_data_add_string */
403 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
404 const char *key, int64_t value)
408 if ((md == NULL) || (key == NULL))
411 e = md_entry_alloc (key);
415 e->value.mv_signed_int = value;
416 e->type = MD_TYPE_SIGNED_INT;
418 return (md_entry_insert (md, e));
419 } /* }}} int meta_data_add_signed_int */
421 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
422 const char *key, uint64_t value)
426 if ((md == NULL) || (key == NULL))
429 e = md_entry_alloc (key);
433 e->value.mv_unsigned_int = value;
434 e->type = MD_TYPE_UNSIGNED_INT;
436 return (md_entry_insert (md, e));
437 } /* }}} int meta_data_add_unsigned_int */
439 int meta_data_add_double (meta_data_t *md, /* {{{ */
440 const char *key, double value)
444 if ((md == NULL) || (key == NULL))
447 e = md_entry_alloc (key);
451 e->value.mv_double = value;
452 e->type = MD_TYPE_DOUBLE;
454 return (md_entry_insert (md, e));
455 } /* }}} int meta_data_add_double */
457 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
458 const char *key, _Bool value)
462 if ((md == NULL) || (key == NULL))
465 e = md_entry_alloc (key);
469 e->value.mv_boolean = value;
470 e->type = MD_TYPE_BOOLEAN;
472 return (md_entry_insert (md, e));
473 } /* }}} int meta_data_add_boolean */
478 int meta_data_get_string (meta_data_t *md, /* {{{ */
479 const char *key, char **value)
484 if ((md == NULL) || (key == NULL) || (value == NULL))
487 pthread_mutex_lock (&md->lock);
489 e = md_entry_lookup (md, key);
492 pthread_mutex_unlock (&md->lock);
496 if (e->type != MD_TYPE_STRING)
498 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
499 pthread_mutex_unlock (&md->lock);
503 temp = md_strdup (e->value.mv_string);
506 pthread_mutex_unlock (&md->lock);
507 ERROR ("meta_data_get_string: md_strdup failed.");
511 pthread_mutex_unlock (&md->lock);
516 } /* }}} int meta_data_get_string */
518 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
519 const char *key, int64_t *value)
523 if ((md == NULL) || (key == NULL) || (value == NULL))
526 pthread_mutex_lock (&md->lock);
528 e = md_entry_lookup (md, key);
531 pthread_mutex_unlock (&md->lock);
535 if (e->type != MD_TYPE_SIGNED_INT)
537 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
538 pthread_mutex_unlock (&md->lock);
542 *value = e->value.mv_signed_int;
544 pthread_mutex_unlock (&md->lock);
546 } /* }}} int meta_data_get_signed_int */
548 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
549 const char *key, uint64_t *value)
553 if ((md == NULL) || (key == NULL) || (value == NULL))
556 pthread_mutex_lock (&md->lock);
558 e = md_entry_lookup (md, key);
561 pthread_mutex_unlock (&md->lock);
565 if (e->type != MD_TYPE_UNSIGNED_INT)
567 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
568 pthread_mutex_unlock (&md->lock);
572 *value = e->value.mv_unsigned_int;
574 pthread_mutex_unlock (&md->lock);
576 } /* }}} int meta_data_get_unsigned_int */
578 int meta_data_get_double (meta_data_t *md, /* {{{ */
579 const char *key, double *value)
583 if ((md == NULL) || (key == NULL) || (value == NULL))
586 pthread_mutex_lock (&md->lock);
588 e = md_entry_lookup (md, key);
591 pthread_mutex_unlock (&md->lock);
595 if (e->type != MD_TYPE_DOUBLE)
597 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
598 pthread_mutex_unlock (&md->lock);
602 *value = e->value.mv_double;
604 pthread_mutex_unlock (&md->lock);
606 } /* }}} int meta_data_get_double */
608 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
609 const char *key, _Bool *value)
613 if ((md == NULL) || (key == NULL) || (value == NULL))
616 pthread_mutex_lock (&md->lock);
618 e = md_entry_lookup (md, key);
621 pthread_mutex_unlock (&md->lock);
625 if (e->type != MD_TYPE_BOOLEAN)
627 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
628 pthread_mutex_unlock (&md->lock);
632 *value = e->value.mv_boolean;
634 pthread_mutex_unlock (&md->lock);
636 } /* }}} int meta_data_get_boolean */
638 /* vim: set sw=2 sts=2 et fdm=marker : */