]> git.deb.at Git - pkg/abook.git/blob - conff.c
Imported Upstream version 0.4.16
[pkg/abook.git] / conff.c
1
2 /*
3  *
4  * $Id: conff.c,v 1.5 2001/12/19 20:20:55 jheinonen Exp $
5  *
6  * Copyright (C) Jaakko Heinonen
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include "misc.h"
28 #ifdef HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
31 #include "conff.h"
32
33 #ifndef DEBUG
34 #       define NDEBUG   1
35 #else
36 #       undef NDEBUG
37 #endif
38
39 #include <assert.h>
40
41 #ifdef _AIX
42 int strcasecmp (const char *, const char *);
43 int strncasecmp (const char *, const char *, size_t);
44 #endif
45
46 #define COMMENT_CHAR    '#'
47
48 static void
49 conff_free_node(struct conff_node *node)
50 {
51         free(node -> value);
52         free(node -> key);
53         free(node);
54 }
55
56 /*
57  * conff_add_key
58  *
59  * returns 0 if the key was successfully added
60  */
61
62 int
63 conff_add_key(struct conff_node **ptr, char *key, char *value, int flags)
64 {
65         struct conff_node *new_item, *next = NULL;
66         int replace = 0;
67
68         assert(key != NULL && value != NULL);
69
70         for(; *ptr; ptr = &( (*ptr) -> next) ) 
71                 if(!strcasecmp(key, (*ptr) -> key ) ) {
72                         if (flags & REPLACE_KEY) {
73                                 replace = 1;
74                                 break;
75                         } else
76                                 return 1;
77                 }
78         
79         if( (new_item = malloc(sizeof(struct conff_node))) == NULL )
80                 return 5;
81
82         if(replace) { 
83                 next = (*ptr) -> next;
84                 conff_free_node(*ptr);
85         }
86
87         new_item -> key = strdup(key);
88         new_item -> value = strdup(value);
89         new_item -> next = next;
90
91         *ptr = new_item;
92
93         return 0;
94 }
95
96 char *
97 conff_get_value(struct conff_node *node, char *key)
98 {
99
100         assert(key != NULL);
101         
102         for(; node ; node = node -> next) {
103                 if(!strcasecmp(node -> key, key))
104                         return node -> value;
105         }
106
107         return NULL; /* not found */
108 }
109
110 void
111 conff_free_nodes(struct conff_node *node)
112 {
113         if(node != NULL) {
114                 conff_free_nodes( node -> next );
115                 conff_free_node( node );
116         }
117 }
118
119 #ifdef DEBUG
120 void
121 print_values(struct conff_node *node)
122 {
123         for(;node; node = node -> next)
124                 fprintf(stderr, "%s - %s\n", node -> key, node -> value);
125 }
126 #endif
127
128 void
129 conff_remove_key(struct conff_node **node, char *key)
130 {
131         assert(key != NULL);
132         
133         for(; *node; node = &((*node) -> next) ) {
134                 if(!strcasecmp(key, (*node) -> key ) ) {
135                         struct conff_node *tmp = *node;
136                         *node = (*node) -> next;
137                         conff_free_node(tmp);
138                         return;
139                 }
140         }
141 }
142
143 int
144 conff_save_file(struct conff_node *node, char *filename)
145 {
146         FILE *out;
147
148         assert(filename != NULL);
149
150         if (!(out = fopen(filename, "w")))
151                 return -1;
152
153         for(; node; node = node -> next)
154                 fprintf(out, "%s=%s\n", node -> key, node -> value);
155
156         fputc('\n', out);
157         fclose(out);
158
159         return 0;
160 }
161
162 int
163 conff_load_file(struct conff_node **node, char *filename, int flags)
164 {
165         FILE *in;
166         char *line = NULL, *tmp;
167         int i = 0;
168
169         assert(filename != NULL);
170
171         if (!(in = fopen(filename, "r")))
172                 return -1;
173
174         for(;;) {
175                 i++;
176
177                 line = getaline(in);
178                 if( feof(in) )
179                         break;
180                 if(!line)
181                         continue;
182
183                 strtrim(line);
184
185                 if(*line == '\n' || *line == '\0' || *line == COMMENT_CHAR) {
186                         free(line);
187                         continue;
188                 }
189
190                 if ( (tmp = strchr(line, '=') )) {
191                         *tmp++ = 0;
192                         conff_add_key(node, strtrim(line), strtrim(tmp), flags);
193                 } else {
194 /*                      fprintf(stderr, "parse error2,line #%d\n",i);*/
195                         fclose(in);
196                         free(line);
197                         return i;
198                 }
199                 free(line);
200         }
201
202         free(line);
203         fclose(in);
204
205         return 0;
206 }
207