config-table.c

Go to the documentation of this file.
00001 /*
00002  ** Copyright(C) 2008 INL
00003  ** Written by Sebastien Tricaud <s.tricaud@inl.fr>
00004  **
00005  ** $Id: config-table.c 4937 2008-09-23 15:47:05Z toady $
00006  **
00007  ** This program is free software; you can redistribute it and/or modify
00008  ** it under the terms of the GNU General Public License as published by
00009  ** the Free Software Foundation, version 3 of the License.
00010  **
00011  ** This program is distributed in the hope that it will be useful,
00012  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  ** GNU General Public License for more details.
00015  **
00016  ** You should have received a copy of the GNU General Public License
00017  ** along with this program; if not, write to the Free Software
00018  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <errno.h>
00025 
00026 #include <nubase.h>
00027 
00028 #include "linuxlist.h"
00029 #include "config-table.h"
00030 
00042 LLIST_HEAD(config_table_list);
00043 
00044 char *nubase_config_table_get(char *key)
00045 {
00046         struct config_table_t *config_table;
00047 
00048         llist_for_each_entry(config_table, &config_table_list, list) {
00049                 if (!strcmp(config_table->key, key)) {
00050                         return config_table->value;
00051                 }
00052         }
00053 
00054         return NULL;
00055 }
00056 
00057 char *nubase_config_table_get_alwaysstring(char *key)
00058 {
00059         char *str;
00060 
00061         str = nubase_config_table_get(key);
00062         if ( ! str ) return "";
00063 
00064         return str;
00065 }
00066 
00067 char *nubase_config_table_get_or_default(char *key, char *replace)
00068 {
00069         char *str;
00070 
00071         str = nubase_config_table_get(key);
00072 
00073         if (str) {
00074                 return strdup(str);
00075         } else if (replace) {
00076                 return strdup(replace);
00077         } else {
00078                 return strdup("");
00079         }
00080 
00081 }
00082 
00083 struct config_table_t *nubase_config_table_append(char *key, char *value)
00084 {
00085         struct config_table_t *config_table;
00086 
00087         if (nubase_config_table_get(key)) return NULL;
00088 
00089         config_table = malloc(sizeof(*config_table));
00090         if ( ! config_table ) {
00091                 errno = ENOMEM;
00092                 return NULL;
00093         }
00094 
00095         config_table->key = strdup(key);
00096         config_table->value = strdup(value);
00097 
00098         llist_add_tail(&config_table->list, &config_table_list);
00099 
00100 
00101         return config_table;
00102 }
00103 
00104 void nubase_config_table_destroy(void)
00105 {
00106         struct config_table_t *config_table;
00107 
00108         while(!llist_empty(&config_table_list)) {
00109                 config_table = llist_entry(config_table_list.next, struct config_table_t, list);
00110                 llist_del(&config_table->list);
00111                 free(config_table->key);
00112                 free(config_table->value);
00113                 free(config_table);
00114         }
00115 
00116         // Reinitialize the list for reuse
00117         INIT_LLIST_HEAD(&config_table_list);
00118 }
00119 
00120 /* Similar to nubase_config_table_append,
00121  * but does not check for existing value
00122  * and if it exists, free() it */
00123 struct config_table_t *nubase_config_table_set(char *key, char *value)
00124 {
00125         struct config_table_t *config_table;
00126 
00127         /* It does not exists so we use _append*/
00128         if ( ! nubase_config_table_get(key) ) {
00129                 return nubase_config_table_append(key, value);
00130         }
00131 
00132         llist_for_each_entry(config_table, &config_table_list, list) {
00133                 if (!strncmp(key, config_table->key, strlen(config_table->key))) {
00134                         llist_del(&config_table->list);
00135                         return nubase_config_table_append(key, value);
00136                 }
00137         }
00138 
00139         return NULL;
00140 }
00141 
00142 int nubase_config_table_get_or_default_int(char *key, int defint)
00143 {
00144         char *str;
00145         int i;
00146 
00147         str = nubase_config_table_get_or_default(key, str_itoa(defint));
00148 
00149         i = atoi(str);
00150 
00151         return i;
00152 }
00153 
00154 void nubase_config_table_print(void *userdata, void (*func)(void *data, char *keyeqval))
00155 {
00156         struct config_table_t *config_table;
00157         char *buffer;
00158         size_t buffer_len;
00159 
00160         llist_for_each_entry(config_table, &config_table_list, list) {
00161                 buffer_len = strlen((const char *)config_table->key) + 1 + strlen((const char *)config_table->value) + 1;
00162                 buffer = malloc(buffer_len);
00163                 secure_snprintf(buffer,  buffer_len,
00164                                 "%s=%s",(char *)config_table->key, (char *)config_table->value); 
00165                 
00166                 func(userdata, buffer);
00167 
00168                 free(buffer);
00169         }
00170 }
00171 
00172 #ifdef _UNIT_TEST_
00173 #include <stdio.h>
00174 int main(void)
00175 {
00176         struct config_table_t *config_table;
00177         int i = 0;
00178 
00179         nubase_config_table_append("foo", "bar");
00180         nubase_config_table_append("foo", "bar");
00181         nubase_config_table_append("nu", "pik");
00182         nubase_config_table_append("tout", "foulcan");
00183         nubase_config_table_append("jean", "nemard");
00184 
00185         printf("\n........................\nllist_for_each_entry\n........................\n");
00186 
00187         llist_for_each_entry(config_table, &config_table_list, list) {
00188                 printf("key=%s, value=%s\n", config_table->key, config_table->value);
00189         }
00190 
00191         printf("\n........................\nnubase_config_table_get\n........................\n");
00192         printf("The value for 'nu' is '%s'\n", nubase_config_table_get("nu"));
00193 
00194 }
00195 #endif
00196 

Generated on Sat Nov 22 04:00:37 2008 for NuFW by  doxygen 1.4.7