2 * collectd - src/utils_match.c
3 * Copyright (C) 2008 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 #include "utils_match.h"
31 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
38 int (*callback) (const char *str, void *user_data);
45 static int default_callback (const char *str, void *user_data)
47 cu_match_value_t *data = (cu_match_value_t *) user_data;
49 if (data->ds_type == UTILS_MATCH_DS_TYPE_GAUGE)
54 value = strtod (str, &endptr);
58 data->value.gauge = value;
60 else if ((data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_SET)
61 || (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_ADD))
66 value = strtoll (str, &endptr, 0);
70 if (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_SET)
71 data->value.counter = value;
73 data->value.counter += value;
75 else if (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_INC)
77 data->value.counter++;
85 } /* int default_callback */
90 cu_match_t *match_create_callback (const char *regex,
91 int (*callback) (const char *str, void *user_data),
97 obj = (cu_match_t *) malloc (sizeof (cu_match_t));
100 memset (obj, '\0', sizeof (cu_match_t));
102 status = regcomp (&obj->regex, regex, REG_EXTENDED);
105 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
110 obj->callback = callback;
111 obj->user_data = user_data;
114 } /* cu_match_t *match_create_callback */
116 cu_match_t *match_create_simple (const char *regex, int match_ds_type)
118 cu_match_value_t *user_data;
121 user_data = (cu_match_value_t *) malloc (sizeof (cu_match_value_t));
122 if (user_data == NULL)
124 memset (user_data, '\0', sizeof (cu_match_value_t));
126 obj = match_create_callback (regex, default_callback, user_data);
133 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
136 } /* cu_match_t *match_create_simple */
138 void match_destroy (cu_match_t *obj)
143 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
145 sfree (obj->user_data);
149 } /* void match_destroy */
151 int match_apply (cu_match_t *obj, const char *str)
156 size_t sub_match_len;
158 if ((obj == NULL) || (str == NULL))
164 status = regexec (&obj->regex, str, /* nmatch = */ 1, &re_match,
167 /* Regex did not match */
171 if (re_match.rm_so < 0)
173 status = obj->callback (str, obj->user_data);
177 assert (re_match.rm_so < re_match.rm_eo);
178 sub_match_len = (size_t) (re_match.rm_eo - re_match.rm_so);
179 sub_match = (char *) malloc (sizeof (char) * (sub_match_len + 1));
180 if (sub_match == NULL)
182 ERROR ("malloc failed.");
185 sstrncpy (sub_match, str + re_match.rm_so, sub_match_len + 1);
187 status = obj->callback (sub_match, obj->user_data);
192 } /* int match_apply */
194 void *match_get_user_data (cu_match_t *obj)
198 return (obj->user_data);
199 } /* void *match_get_user_data */
201 /* vim: set sw=2 sts=2 ts=8 : */