6 * Copyright (C) Jaakko Heinonen
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * conff.c version 0.3.0
46 int strcasecmp (const char *, const char *);
47 int strncasecmp (const char *, const char *, size_t);
50 #define COMMENT_CHAR '#'
53 conff_free_node(struct conff_node *node)
61 conff_add_key(struct conff_node **ptr, char *key, char *value, int flags)
63 struct conff_node *new_item, *next = NULL;
65 assert(key != NULL && value != NULL);
67 for(; *ptr; ptr = &( (*ptr) -> next) )
68 if(!strcasecmp(key, (*ptr) -> key ) ) {
69 if (flags & REPLACE_KEY) {
70 next = (*ptr) -> next;
71 conff_free_node(*ptr);
78 * out of memory - error is ignored
79 * NOTE: with REPLACE_KEY flag the node will be deleted in OOM
82 if( (new_item = malloc(sizeof(struct conff_node))) == NULL )
85 new_item -> key = strdup(key);
86 new_item -> value = strdup(value);
87 new_item -> next = next;
93 conff_get_value(struct conff_node *node, char *key)
98 for(; node ; node = node -> next) {
99 if(!strcasecmp(node -> key, key))
100 return node -> value;
103 return NULL; /* not found */
107 conff_free_nodes(struct conff_node *node)
110 conff_free_nodes( node -> next );
111 conff_free_node( node );
117 print_values(struct conff_node *node)
119 for(;node; node = node -> next)
120 fprintf(stderr, "%s - %s\n", node -> key, node -> value);
125 conff_remove_key(struct conff_node **node, char *key)
129 for(; *node; node = &((*node) -> next) ) {
130 if(!strcasecmp(key, (*node) -> key ) ) {
131 struct conff_node *tmp = *node;
132 *node = (*node) -> next;
133 conff_free_node(tmp);
140 conff_save_file(struct conff_node *node, char *filename)
144 assert(filename != NULL);
146 if (!(out = fopen(filename, "w")))
149 for(; node; node = node -> next)
150 fprintf(out, "%s=%s\n", node -> key, node -> value);
159 conff_load_file(struct conff_node **node, char *filename, int flags)
162 char *line = NULL, *tmp;
165 if (!(in = fopen(filename, "r")))
179 if(*line == '\n' || *line == '\0' || *line == COMMENT_CHAR) {
184 if ( (tmp = strchr(line, '=') )) {
186 conff_add_key(node, strtrim(line), strtrim(tmp), flags);
188 /* fprintf(stderr, "parse error2,line #%d\n",i);*/