comparison mcabber/src/parsecfg.c @ 279:f5dd437c057b

Rewrite the settings system Settings now use glibc library, and can be used for aliases and bindings.
author Mikael Berthe <mikael@lilotux.net>
date Tue, 05 Jul 2005 23:50:50 +0100
parents 7604e3cdbb86
children 9bdfef4f4735 12f919be3da5
comparison
equal deleted inserted replaced
278:a7af5517b453 279:f5dd437c057b
3 #include <unistd.h> 3 #include <unistd.h>
4 #include <ctype.h> 4 #include <ctype.h>
5 #include <string.h> 5 #include <string.h>
6 #include <glib.h> 6 #include <glib.h>
7 7
8 #include "list.h" 8 #include "settings.h"
9 #include "utils.h" 9 #include "utils.h"
10
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 = (cfg_entry_t*)g_new0(char, sizeof(cfg_entry_t));
26
27 new_entry->key = g_new0(char, strlen(key) + 1);
28 new_entry->value = g_new0(char, 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 10
36 int cfg_file(char *filename) 11 int cfg_file(char *filename)
37 { 12 {
38 FILE *fp; 13 FILE *fp;
39 char *buf; 14 char *buf;
89 64
90 while ((strlen(line) > 0) 65 while ((strlen(line) > 0)
91 && isspace((int) line[strlen(line) - 1])) 66 && isspace((int) line[strlen(line) - 1]))
92 line[strlen(line) - 1] = '\0'; 67 line[strlen(line) - 1] = '\0';
93 68
94 push_in_list(line, value); 69 settings_set(SETTINGS_TYPE_OPTION, line, value);
95 continue; 70 continue;
96 } 71 }
97 fprintf(stderr, "CFG: orphaned line \"%s\"\n", line); 72 fprintf(stderr, "CFG: orphaned line \"%s\"\n", line);
98 } 73 }
99 g_free(buf); 74 g_free(buf);
100 return 1; 75 return 1;
101 } 76 }
102
103 char *cfg_read(char *key)
104 {
105 struct list_head *n, *pos;
106 cfg_entry_t *search_entry = NULL;
107
108 list_for_each_safe(pos, n, &cfg_list) {
109 search_entry = cfg_entry(pos);
110 if (search_entry->key) {
111 if (!strcasecmp(search_entry->key, key)) {
112 return search_entry->value;
113 }
114 }
115 }
116 return NULL;
117 }
118
119 int cfg_read_int(char *key)
120 {
121 char *optval;
122
123 optval = cfg_read(key);
124
125 if (optval)
126 return atoi(optval);
127
128 return 0;
129 }