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