2 * collection4 - utils_search.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
30 #include "utils_search.h"
31 #include "utils_array.h"
34 #include <fcgi_stdio.h>
40 char *plugin_instance;
50 static char *read_quoted_string (const char **buffer) /* {{{ */
52 const char *ptr = *buffer;
61 while ((*ptr != '"') && (*ptr != 0))
71 if ((ret_len < 1) || (*ptr != '"'))
74 ret = malloc (ret_len + 1);
80 while ((*ptr != '"') && (*ptr != 0))
94 /* terminate string */
97 /* "ptr" points to the '"' sign, so advance one more */
102 } /* }}} char *read_quoted_string */
104 static char *read_unquoted_word (const char **buffer) /* {{{ */
106 const char *ptr = *buffer;
111 while (!isspace ((int) ptr[ret_len]) && (ptr[ret_len] != 0))
117 ret = malloc (ret_len + 1);
121 memcpy (ret, ptr, ret_len);
128 } /* }}} char *read_unquoted_word */
130 static char *next_token (const char **buffer) /* {{{ */
132 const char *ptr = *buffer;
135 while (isspace ((int) (*ptr)))
140 else if (ptr[0] == '"')
142 ret = read_quoted_string (&ptr);
150 ret = read_unquoted_word (&ptr);
155 } /* }}} char *next_token */
157 static int store_token_field (char **field, const char *token) /* {{{ */
161 if ((field == NULL) || (token == NULL))
164 copy = strdup (token);
172 } /* }}} int store_token_field */
174 static int store_token (search_info_t *si, const char *token) /* {{{ */
176 if (strncmp ("host:", token, strlen ("host:")) == 0)
177 return (store_token_field (&si->host, token + strlen ("host:")));
178 else if (strncmp ("plugin:", token, strlen ("plugin:")) == 0)
179 return (store_token_field (&si->plugin, token + strlen ("plugin:")));
180 else if (strncmp ("plugin_instance:", token, strlen ("plugin_instance:")) == 0)
181 return (store_token_field (&si->plugin_instance, token + strlen ("plugin_instance:")));
182 else if (strncmp ("type:", token, strlen ("type:")) == 0)
183 return (store_token_field (&si->type, token + strlen ("type:")));
184 else if (strncmp ("type_instance:", token, strlen ("type_instance:")) == 0)
185 return (store_token_field (&si->type_instance, token + strlen ("type_instance:")));
187 return (array_append (si->terms, token));
188 } /* }}} int store_token */
193 search_info_t *search_parse (const char *search) /* {{{ */
199 si = malloc (sizeof (*si));
202 memset (si, 0, sizeof (*si));
204 si->terms = array_create ();
205 if (si->terms == NULL)
213 while ((token = next_token (&ptr)) != NULL)
215 store_token (si, token);
220 } /* }}} search_info_t *search_parse */
222 void search_destroy (search_info_t *si) /* {{{ */
229 free (si->plugin_instance);
231 free (si->type_instance);
233 array_destroy (si->terms);
234 } /* }}} void search_destroy */
236 /* vim: set sw=2 sts=2 et fdm=marker : */