annotate mcabber/parsecfg.c @ 0:b3b2332715fb

Tailorization of /trunk Import of the upstream sources from Repository: file:///tmp/svn-mcabber Module: /trunk Revision: 15
author tailor@frmp8452
date Thu, 30 Jun 2005 21:39:31 +0000
parents
children
rev   line source
0
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
1 #include <stdio.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
2 #include <stdlib.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
3 #include <unistd.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
4 #include <ctype.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
5 #include <string.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
6
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
7
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
8 #include "list.h"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
9
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
10 /* Definicion de tipos */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
11 #define MAX_LENGHT_INPUT 1024
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
12 #define cfg_entry(n) list_entry(n, cfg_entry_t, list)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
13
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
14 typedef struct _cfg_entry_t {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
15 char *key;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
16 char *value;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
17 struct list_head list;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
18 } cfg_entry_t;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
19
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
20 static LIST_HEAD(cfg_list);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
21
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
22
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
23 void push_in_list(char *key, char *value)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
24 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
25 cfg_entry_t *new_entry = calloc(1, sizeof(cfg_entry_t));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
26
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
27 new_entry->key = (char *) calloc(1, strlen(key) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
28 new_entry->value = (char *) calloc(1, strlen(value) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
29
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
30 strcpy(new_entry->key, key);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
31 strcpy(new_entry->value, value);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
32
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
33 list_add(&new_entry->list, &cfg_list);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
34 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
35
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
36 int cfg_file(char *filename)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
37 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
38 FILE *fp;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
39 char *buf;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
40 char *line;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
41 char *value;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
42
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
43 buf = malloc(255);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
44
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
45 if ((fp = fopen(filename, "r")) == NULL) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
46 perror("fopen (parsecfg.c:46)");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
47 exit(EXIT_FAILURE);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
48 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
49
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
50 while (fgets(buf, 255, fp) != NULL) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
51 line = buf;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
52
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
53 while (isspace((int) *line))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
54 line++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
55
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
56 while ((strlen(line) > 0)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
57 && isspace((int) line[strlen(line) - 1]))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
58 line[strlen(line) - 1] = '\0';
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
59
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
60 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
61 continue;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
62
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
63 if ((strchr(line, '=') != NULL)) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
64 value = strchr(line, '=');
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
65 *value = '\0';
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
66 value++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
67
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
68 while (isspace((int) *value))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
69 value++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
70
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
71 while ((strlen(line) > 0)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
72 && isspace((int) line[strlen(line) - 1]))
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
73 line[strlen(line) - 1] = '\0';
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
74
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
75 push_in_list(line, value);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
76 continue;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
77 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
78 fprintf(stderr, "CFG: orphaned line \"%s\"\n", line);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
79 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
80 return 1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
81 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
82
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
83 char *cfg_read(char *key)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
84 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
85 struct list_head *n, *pos;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
86 cfg_entry_t *search_entry = NULL;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
87
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
88 list_for_each_safe(pos, n, &cfg_list) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
89 search_entry = cfg_entry(pos);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
90 if (search_entry->key) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
91 if (!strcasecmp(search_entry->key, key)) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
92 return search_entry->value;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
93 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
94 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
95 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
96 return NULL;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
97 }