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)
59 * returns 0 if the key was successfully added
63 conff_add_key(struct conff_node **ptr, char *key, char *value, int flags)
65 struct conff_node *new_item, *next = NULL;
68 assert(key != NULL && value != NULL);
70 for(; *ptr; ptr = &( (*ptr) -> next) )
71 if(!strcasecmp(key, (*ptr) -> key ) ) {
72 if (flags & REPLACE_KEY) {
79 if( (new_item = (struct conff_node *)malloc(sizeof(struct conff_node)))
84 next = (*ptr) -> next;
85 conff_free_node(*ptr);
88 new_item -> key = strdup(key);
89 new_item -> value = strdup(value);
90 new_item -> next = next;
98 conff_get_value(struct conff_node *node, char *key)
103 for(; node ; node = node -> next) {
104 if(!strcasecmp(node -> key, key))
105 return node -> value;
108 return NULL; /* not found */
112 conff_free_nodes(struct conff_node *node)
115 conff_free_nodes( node -> next );
116 conff_free_node( node );
122 print_values(struct conff_node *node)
124 for(;node; node = node -> next)
125 fprintf(stderr, "%s - %s\n", node -> key, node -> value);
130 conff_remove_key(struct conff_node **node, char *key)
134 for(; *node; node = &((*node) -> next) ) {
135 if(!strcasecmp(key, (*node) -> key ) ) {
136 struct conff_node *tmp = *node;
137 *node = (*node) -> next;
138 conff_free_node(tmp);
145 conff_save_file(struct conff_node *node, char *filename)
149 assert(filename != NULL);
151 if (!(out = fopen(filename, "w")))
154 for(; node; node = node -> next)
155 fprintf(out, "%s=%s\n", node -> key, node -> value);
164 conff_load_file(struct conff_node **node, char *filename, int flags)
167 char *line = NULL, *tmp;
170 assert(filename != NULL);
172 if (!(in = fopen(filename, "r")))
186 if(*line == '\n' || *line == '\0' || *line == COMMENT_CHAR) {
191 if ( (tmp = strchr(line, '=') )) {
193 conff_add_key(node, strtrim(line), strtrim(tmp), flags);
195 /* fprintf(stderr, "parse error2,line #%d\n",i);*/