2 * collectd - src/utils_fbhash.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
27 #include "utils_fbhash.h"
28 #include "utils_avltree.h"
42 static void fbh_free_tree (c_avl_tree_t *tree) /* {{{ */
54 status = c_avl_pick (tree, (void *) &key, (void *) &value);
63 } /* }}} void fbh_free_tree */
65 static int fbh_read_file (fbhash_t *h) /* {{{ */
73 fh = fopen (h->filename, "r");
77 memset (&fl, 0, sizeof (fl));
79 fl.l_whence = SEEK_SET;
81 fl.l_len = 0; /* == entire file */
82 /* TODO: Lock file? -> fcntl */
84 status = fcntl (fileno (fh), F_SETLK, &fl);
91 tree = c_avl_create ((void *) strcmp);
98 /* Read `fh' into `tree' */
99 while (fgets (buffer, sizeof (buffer), fh) != NULL) /* {{{ */
108 buffer[sizeof (buffer) - 1] = 0;
109 len = strlen (buffer);
111 /* Remove trailing newline characters. */
113 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
119 /* Seek first non-space character */
121 while ((*key != 0) && isspace ((int) *key))
124 /* Skip empty lines and comments */
125 if ((key[0] == 0) || (key[0] == '#'))
128 /* Seek first colon */
129 value = strchr (key, ':');
133 /* Null-terminate `key'. */
137 /* Skip leading whitespace */
138 while ((*value != 0) && isspace ((int) *value))
141 /* Skip lines without value */
145 key_copy = strdup (key);
146 value_copy = strdup (value);
148 if ((key_copy == NULL) || (value_copy == NULL))
155 status = c_avl_insert (tree, key_copy, value_copy);
163 DEBUG ("utils_fbhash: fbh_read_file: key = %s; value = %s;",
165 } /* }}} while (fgets) */
169 fbh_free_tree (h->tree);
173 } /* }}} int fbh_read_file */
175 static int fbh_check_file (fbhash_t *h) /* {{{ */
180 memset (&statbuf, 0, sizeof (statbuf));
182 status = stat (h->filename, &statbuf);
186 if (h->mtime >= statbuf.st_mtime)
189 status = fbh_read_file (h);
191 h->mtime = statbuf.st_mtime;
194 } /* }}} int fbh_check_file */
199 fbhash_t *fbh_create (const char *file) /* {{{ */
207 h = malloc (sizeof (*h));
210 memset (h, 0, sizeof (*h));
212 h->filename = strdup (file);
213 if (h->filename == NULL)
220 pthread_mutex_init (&h->lock, /* attr = */ NULL);
222 status = fbh_check_file (h);
230 } /* }}} fbhash_t *fbh_create */
232 void fbh_destroy (fbhash_t *h) /* {{{ */
237 pthread_mutex_destroy (&h->lock);
239 fbh_free_tree (h->tree);
240 } /* }}} void fbh_destroy */
242 char *fbh_get (fbhash_t *h, const char *key) /* {{{ */
248 if ((h == NULL) || (key == NULL))
254 pthread_mutex_lock (&h->lock);
256 /* TODO: Checking this everytime may be a bit much..? */
259 status = c_avl_get (h->tree, key, (void *) &value);
262 assert (value != NULL);
263 value_copy = strdup (value);
266 pthread_mutex_unlock (&h->lock);
269 } /* }}} char *fbh_get */
271 /* vim: set sw=2 sts=2 et fdm=marker : */