2 * Copyright (C) 2008 Diego Hernan Borghetti.
15 E_Section
*sections
= NULL
;
17 int e_config_read_line(FILE *fp
, char *buf
, int len
)
21 for (i
= 0; i
< len
; i
++) {
38 /* the line is too big, so we skip this. */
42 int __e_config_is_section(char *line
)
47 while (p
&& (*p
== ' ' || *p
== '\t'))
55 int __e_config_is_option(char *line
)
65 E_Section
*__e_config_section(char *line
)
71 sc
= (E_Section
*)malloc(sizeof(E_Section
));
76 /* is valid if the line have space or tab befor the '[' */
78 while (*p
== ' ' || *p
== '\t')
81 /* ok, this is the first character != of ' ' or '\t'
82 * and is the start of the section, '[' that is why
83 * we add a + two character.
87 /* at this point we are in the start of the section name,
88 * but need take care that the section name maybe have space,
89 * for eg. [ Keys ] and not [Keys].. so we support both model.
91 while (p
&& (*p
== ' ' || *p
== '\t'))
99 /* count the number of character. */
100 for (i
= 0, found
= 0; i
< strlen(p
); i
++) {
101 if (p
[i
] == ' ' || p
[i
] == ']') {
112 sc
->name
= (char *)malloc(i
+1);
115 if (*p
== ' ' || *p
== ']') {
127 E_Config
*__e_config_option(char *line
)
133 cf
= (E_Config
*)malloc(sizeof(E_Config
));
138 /* the options always can have tab or ' ' */
140 while (*p
== ' ' || *p
== '\t')
143 /* count the number of characters. */
144 for (i
= 0; i
< strlen(p
); i
++) {
145 if (p
[i
] == ' ' || p
[i
] == '=')
149 cf
->key
= (char *)malloc(i
+1);
152 if (*p
== ' ' || *p
== '=') {
162 p
= strchr(line
, '=');
165 /* the value also have space or tab, so skip it. */
166 while (p
&& (*p
== ' ' || *p
== '\t'))
169 cf
->value
= (char *)malloc(strlen(p
)+1);
170 strcpy(cf
->value
, p
);
174 void e_config_parse(char *file
)
182 fp
= fopen(file
, "r");
186 line
= (char *)malloc(1024);
188 i
= e_config_read_line(fp
, line
, 1023);
193 /* skip comments and blank lines */
194 if (line
[0] == '#' || line
[0] == '\n')
197 if (__e_config_is_section(line
) == 0) {
198 sc
= __e_config_section(line
);
204 /* if we don't have a section, skip this line. */
208 if (__e_config_is_option(line
) == 0) {
209 cf
= __e_config_option(line
);
218 i
= e_config_read_line(fp
, line
, 1023);
222 e_debug_printf("==> Config File <==\n");
225 e_debug_printf("Section: (%s)\n", sc
->name
);
228 e_debug_printf("\tKey: (%s)\n", cf
->key
);
229 e_debug_printf("\tValue: (%s)\n", cf
->value
);
234 e_debug_printf("==> End Config File <==\n");
241 void e_config_init(void)
246 home
= getenv("HOME");
250 file
= (char *)malloc(strlen(home
) + 6);
251 sprintf(file
, "%s/.eco", home
);
252 e_config_parse(file
);
256 E_Section
*e_config_find_section(char *name
)
262 if (!strcmp(p
->name
, name
))
269 E_Config
*e_config_find_config(E_Section
*sc
, char *key
)
275 if (!strcmp(p
->key
, key
))
282 char *e_config_get(char *name
, char *key
)
287 sc
= e_config_find_section(name
);
289 cf
= e_config_find_config(sc
, key
);
296 int e_config_get_int(char *name
, char *key
)
301 sc
= e_config_find_section(name
);
303 cf
= e_config_find_config(sc
, key
);
305 return(atoi(cf
->value
));
310 char e_config_get_char(char *name
, char *key
)
315 sc
= e_config_find_section(name
);
317 cf
= e_config_find_config(sc
, key
);
319 return((char)atoi(cf
->value
));