/[winpt]/trunk/Src/wptGPGParser.cpp
ViewVC logotype

Contents of /trunk/Src/wptGPGParser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (show annotations)
Sun Nov 5 08:57:45 2006 UTC (18 years, 3 months ago) by twoaday
File size: 10892 byte(s)


1 /* wptGPGParser.cpp - Config file parser
2 * Copyright (C) 2002, 2003, 2004, 2006 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <windows.h>
28
29 #include "wptTypes.h"
30 #include "wptW32API.h"
31 #include "wptGPG.h"
32 #include "wptErrors.h"
33
34
35 /* Add the option @n to the linked list @list. */
36 static void
37 add_option (conf_option_t *list, conf_option_t n)
38 {
39 conf_option_t t;
40
41 if (!*list)
42 *list = n;
43 else {
44 for (t = *list; t->next; t = t->next)
45 ;
46 t->next = n;
47 }
48 }
49
50 /* Allocate a new option and return it. */
51 static conf_option_t
52 new_option (void)
53 {
54 conf_option_t e;
55
56 e = new conf_option_s;
57 if (!e)
58 BUG (0);
59 memset (e, 0, sizeof *e);
60 return e;
61 }
62
63
64 static void
65 add_opaque_option (config_file_t opt, const char *line, int used)
66 {
67 conf_option_t e;
68
69 e = new_option ();
70 e->used = used > 0? 1 : 0;
71 e->name = m_strdup (line);
72 e->type = ENTRY_OPAQUE;
73 add_option (&opt->list, e);
74 }
75
76
77 static void
78 add_single_option (config_file_t opt, const char *name, int used)
79 {
80 conf_option_t e;
81
82 e = new_option ();
83 e->used = used > 0? 1 : 0;
84 e->name = m_strdup (name);
85 e->type = ENTRY_SINGLE;
86 add_option (&opt->list, e);
87 }
88
89
90 static int
91 add_group_option (config_file_t opt, const char *name, int used)
92 {
93 conf_option_t e;
94
95 e = new_option ();
96 e->used = used > 0? 1 : 0;
97 e->name = m_strdup (name);
98 e->type = ENTRY_GROUP;
99 add_option (&opt->list, e);
100 return 0;
101 }
102
103
104 static size_t
105 count_whitespaces (char *line)
106 {
107 size_t i;
108 size_t ncount = 0;
109
110 if (!strchr (line, ' '))
111 return 0;
112 for( i = 0; i < strlen (line); i++) {
113 if (line[i] == ' ')
114 ncount++;
115 }
116 return ncount;
117 }
118
119
120 static int
121 add_multi_option (config_file_t opt, char *line, int used)
122 {
123 conf_option_t e;
124 char *p;
125 int state = 0;
126
127 e = new_option ();
128 if (count_whitespaces (line) == 1) {
129 while ((p = strsep (&line, " ")) && state != 2) {
130 switch (state) {
131 case 0: e->name = m_strdup (p); break;
132 case 1: e->val = m_strdup (p); break;
133 }
134 state++;
135 }
136 }
137 else {
138 p = strchr (line, ' ');
139 if (!p)
140 return -1;
141 state = p - line;
142 e->name = new char[state + 1];
143 if (!e->name)
144 BUG (NULL);
145 memcpy (e->name, line, state);
146 e->name[state] = '\0';
147 strncpy (e->name, line, state);
148 e->val = m_strdup (line + state + 1);
149 }
150 e->used = used;
151 e->type = ENTRY_MULTI;
152 add_option (&opt->list, e);
153
154 return 0;
155 }
156
157
158 static conf_group_t
159 new_group (conf_group_t *grp, const char *name)
160 {
161 conf_group_t g, t;
162
163 g = new conf_group_s;
164 if (!g)
165 BUG (NULL);
166 memset (g, 0, sizeof *g);
167 g->used = 1;
168 g->name = m_strdup (name);
169 if (!*grp)
170 *grp = g;
171 else {
172 for (t = *grp; t->next; t = t->next)
173 ;
174 t->next = g;
175 }
176 return g;
177 }
178
179
180 static const char*
181 group_from_cfgfile (config_file_t opt, const char *line)
182 {
183 char *p, *buf, *buf2, *name = NULL;
184 conf_group_t g;
185 conf_member_t m = NULL, t;
186
187 p = strchr (line, '=');
188 if (!p || strncmp (line, "group ", 6))
189 return NULL;
190 buf = m_strdup (line + (p - line + 1));
191 line += 6;
192 p = strchr (line, '=');
193 if (p) {
194 size_t pos = p - line;
195 name = new char[pos + 1];
196 if (!name)
197 BUG( NULL );
198 memset (name, 0, pos+1);
199 strncpy (name, line, pos);
200 }
201 g = new_group (&opt->grp, name);
202 buf2 = buf;
203 while ((p = strsep (&buf2, " ")) && *p) {
204 m = new conf_member_s;
205 if (!m)
206 BUG (0);
207 memset (m, 0, sizeof *m);
208 m->used = 1;
209 m->name = m_strdup (p);
210 if (!g->list)
211 g->list = m;
212 else {
213 for (t=g->list; t->next; t = t->next)
214 ;
215 t->next = m;
216 }
217 }
218 free_if_alloc (buf);
219 if (!m)
220 return NULL;
221 return g->name;
222 }
223
224
225 void
226 conf_release_group (conf_group_t grp)
227 {
228 conf_member_t m, m2;
229
230 m = grp->list;
231 while (m) {
232 m2 = m->next;
233 free_if_alloc (m->name);
234 free_if_alloc (m);
235 m = m2;
236 }
237 }
238
239
240 static void
241 release_option (conf_option_t opt)
242 {
243 if (!opt)
244 return;
245 free_if_alloc (opt->name);
246 free_if_alloc (opt->val);
247 free_if_alloc (opt);
248 }
249
250
251 void
252 release_config (config_file_t opt)
253 {
254 conf_option_t e, e2;
255 conf_group_t g, g2;
256
257 if (!opt)
258 return;
259 e = opt->list;
260 while (e) {
261 e2 = e->next;
262 release_option (e);
263 e = e2;
264 }
265 g = opt->grp;
266 while (g) {
267 g2 = g->next;
268 conf_release_group (g);
269 g = g2;
270 }
271 free_if_alloc (opt);
272 }
273
274
275 int
276 commit_config (const char *file, config_file_t opt)
277 {
278 FILE *inp;
279 conf_option_t e;
280 conf_group_t g;
281 conf_member_t m;
282
283 inp = fopen (file, "w+b");
284 if (!inp)
285 return WPTERR_FILE_OPEN;
286 for (e = opt->list; e; e = e->next) {
287 if (!e->used)
288 continue;
289 switch (e->type) {
290 case ENTRY_OPAQUE:
291 fprintf (inp, "%s", e->name);
292 break;
293
294 case ENTRY_MULTI:
295 fprintf (inp, "%s %s\r\n", e->name, e->val);
296 break;
297
298 case ENTRY_SINGLE:
299 fprintf (inp, "%s\r\n", e->name);
300 break;
301
302 case ENTRY_GROUP:
303 g = conf_find_group (opt, e->name);
304 if (g && g->used) {
305 fprintf (inp, "group %s=", g->name);
306 for (m = g->list; m; m = m->next) {
307 if (m->used)
308 fprintf (inp, "%s ", m->name);
309 }
310 fprintf (inp, "\r\n");
311 }
312 break;
313 }
314 }
315 fclose (inp);
316 return 0;
317 }
318
319
320 void
321 new_config (config_file_t *r_opt)
322 {
323 config_file_t opt;
324
325 opt = new conf_file_s;
326 if (!opt)
327 BUG (NULL);
328 memset (opt, 0, sizeof *opt);
329 *r_opt = opt;
330 }
331
332
333 int
334 parse_config (const char *file, config_file_t *r_opt)
335 {
336 FILE *inp;
337 char buf[1024], *p;
338 config_file_t opt = NULL;
339
340 if (!r_opt)
341 return -1;
342
343 inp = fopen( file, "rb");
344 if (!inp) {
345 inp = fopen (file, "wb");
346 if (inp == NULL)
347 return -1;
348 }
349 opt = new conf_file_s;
350 if (!opt)
351 BUG (NULL);
352 memset (opt, 0, sizeof *opt);
353 while (!feof (inp)) {
354 p = fgets (buf, DIM (buf) -1, inp);
355 if (!p)
356 break;
357 if (*p == '#' || *p == '\r' || *p == '\n' || *p == '\t') {
358 add_opaque_option (opt, p, 1);
359 continue;
360 }
361 if( strstr (p, "\r\n" ))
362 p[strlen (p) - 2] = '\0';
363 else if (strstr (p, "\n"))
364 p[strlen (p) - 1] = '\0';
365 if (!strchr( p, ' ' ))
366 add_single_option (opt, p, 1);
367 else if (!strncmp (p, "group", 5)) {
368 const char *s = group_from_cfgfile (opt, p);
369 if (s)
370 add_group_option (opt, s, 1);
371 }
372 else
373 add_multi_option (opt, p, 1);
374 }
375 fclose (inp);
376 *r_opt = opt;
377 return 0;
378 }
379
380
381 /* Search for an option with the name @str and
382 return this option if it exists.
383 Return value: NULL if not found. */
384 conf_option_t
385 conf_find_option (config_file_t opt, const char *str)
386 {
387 conf_option_t e;
388
389 for (e = opt->list; e; e = e->next) {
390 if (e->type == ENTRY_OPAQUE)
391 continue;
392 if (!stricmp (e->name, str))
393 break;
394 }
395
396 return e;
397 }
398
399
400 conf_group_t
401 conf_find_group (config_file_t opt, const char *str)
402 {
403 conf_group_t g;
404
405 for (g = opt->grp; g; g = g->next) {
406 if (!stricmp( g->name, str))
407 break;
408 }
409
410 return g;
411 }
412
413
414 conf_member_t
415 conf_find_member (config_file_t opt, const char *grp, const char *str)
416 {
417 conf_member_t m;
418 conf_group_t g = conf_find_group (opt, grp);
419
420 if (!g)
421 return NULL;
422
423 for (m = g->list; m; m = m->next) {
424 if (!stricmp (m->name, str))
425 return m;
426 }
427 return NULL;
428 }
429
430
431 int
432 conf_delete_group (config_file_t opt, const char *str)
433 {
434 conf_group_t g = conf_find_group (opt, str);
435
436 if (g)
437 g->used = 0;
438 return g? 0 : -1;
439 }
440
441
442 int
443 conf_delete_member (config_file_t opt, const char *grp, const char *str)
444 {
445 conf_member_t m = conf_find_member (opt, grp, str);
446
447 if (m)
448 m->used = 0;
449 return m? 0 : -1;
450 }
451
452
453 int
454 conf_delete_option (config_file_t opt, const char * str)
455 {
456 conf_option_t e = conf_find_option (opt, str);
457
458 if (e)
459 e->used = 0;
460 return e? 0 : -1;
461 }
462
463
464 int
465 conf_add_entry_int (config_file_t opt, int type, const char *name, int val)
466 {
467 char buf[64];
468
469 _snprintf (buf, DIM (buf)-1, "%d", val);
470 conf_add_entry (opt, type, name, buf);
471 return 0;
472 }
473
474 int
475 conf_add_entry (config_file_t opt, int type, const char *name, const char *val)
476 {
477 conf_option_t e;
478
479 e = new_option ();
480 e->used = 1;
481 e->type = type;
482 e->name = m_strdup (name);
483 e->val = val? m_strdup (val) : NULL;
484 add_option (&opt->list, e);
485 return 0;
486 }
487
488
489 /* Modify the value of the entry with the name @name.
490 If the entry were not found, add it to the option list. */
491 int
492 conf_modify_entry (config_file_t opt, int type, const char *name, const char *val)
493 {
494 conf_option_t e;
495 int rc = 0;
496
497 e = conf_find_option (opt, name);
498 if (!e)
499 rc = conf_add_entry (opt, type, name, val);
500 else if (type != ENTRY_SINGLE && val != NULL) {
501 free_if_alloc (e->val);
502 e->val = m_strdup (val);
503 rc = 0;
504 }
505
506 return rc;
507 }
508
509
510 int
511 conf_add_member (config_file_t opt, const char *grp, const char *str)
512 {
513 conf_group_t g = conf_find_group (opt, grp);
514 conf_member_t m, t;
515
516 if (!g)
517 return -1;
518
519 m = new conf_member_s;
520 if (!m)
521 BUG (NULL);
522 memset (m, 0, sizeof *m);
523 m->used = 1;
524 m->name = m_strdup (str);
525 if( !g->list )
526 g->list = m;
527 else {
528 for( t = g->list; t->next; t = t->next )
529 ;
530 t->next = m;
531 }
532 return 0;
533 }
534
535
536 int
537 conf_add_group (config_file_t opt, const char *str)
538 {
539 conf_group_t g, t;
540
541 g = new conf_group_s;
542 if (!g)
543 BUG (NULL);
544 memset (g, 0, sizeof *g);
545 g->used = 1;
546 g->name = m_strdup (str);
547
548 if (!opt->grp)
549 opt->grp = g;
550 else {
551 for (t = opt->grp; t->next; t = t->next)
552 ;
553 t->next = g;
554 }
555 conf_add_entry (opt, ENTRY_GROUP, str, NULL);
556 return 0;
557 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26