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.
42 int strcasecmp (const char *, const char *);
43 int strncasecmp (const char *, const char *, size_t);
46 #define COMMENT_CHAR '#'
49 conff_free_node(struct conff_node *node)
57 conff_add_key(struct conff_node **ptr, char *key, char *value, int flags)
59 struct conff_node *new_item, *next = NULL;
61 assert(key != NULL && value != NULL);
63 for(; *ptr; ptr = &( (*ptr) -> next) )
64 if(!strcasecmp(key, (*ptr) -> key ) ) {
65 if (flags & REPLACE_KEY) {
66 next = (*ptr) -> next;
67 conff_free_node(*ptr);
74 * out of memory - error is ignored
75 * NOTE: with REPLACE_KEY flag the node will be deleted in OOM
78 if( (new_item = malloc(sizeof(struct conff_node))) == NULL )
81 new_item -> key = strdup(key);
82 new_item -> value = strdup(value);
83 new_item -> next = next;
89 conff_get_value(struct conff_node *node, char *key)
94 for(; node ; node = node -> next) {
95 if(!strcasecmp(node -> key, key))
99 return NULL; /* not found */
103 conff_free_nodes(struct conff_node *node)
106 conff_free_nodes( node -> next );
107 conff_free_node( node );
113 print_values(struct conff_node *node)
115 for(;node; node = node -> next)
116 fprintf(stderr, "%s - %s\n", node -> key, node -> value);
121 conff_remove_key(struct conff_node **node, char *key)
125 for(; *node; node = &((*node) -> next) ) {
126 if(!strcasecmp(key, (*node) -> key ) ) {
127 struct conff_node *tmp = *node;
128 *node = (*node) -> next;
129 conff_free_node(tmp);
136 conff_save_file(struct conff_node *node, char *filename)
140 assert(filename != NULL);
142 if (!(out = fopen(filename, "w")))
145 for(; node; node = node -> next)
146 fprintf(out, "%s=%s\n", node -> key, node -> value);
155 conff_load_file(struct conff_node **node, char *filename, int flags)
158 char *line = NULL, *tmp;
161 if (!(in = fopen(filename, "r")))
175 if(*line == '\n' || *line == '\0' || *line == COMMENT_CHAR) {
180 if ( (tmp = strchr(line, '=') )) {
182 conff_add_key(node, strtrim(line), strtrim(tmp), flags);
184 /* fprintf(stderr, "parse error2,line #%d\n",i);*/