Subversion Repositories Tewi

Rev

Rev 4 | Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: config.c 5 2024-09-13 10:08:00Z nishi $ */
2
 
3
#include "tw_config.h"
4
 
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
 
9
#include <cm_string.h>
10
#include <cm_log.h>
11
 
12
int tw_config_read(const char* path){
13
	cm_log("Config", "Reading %s", path);
14
	char cbuf[2];
15
	cbuf[1] = 0;
16
	FILE* f = fopen(path, "r");
17
	if(f != NULL){
18
		char* line = malloc(1);
19
		line[0] = 0;
20
		while(1){
21
			int c = fread(cbuf, 1, 1, f);
22
			if(cbuf[0] == '\n' || c <= 0){
23
				char* l = cm_trim(line);
24
				if(strlen(l) > 0 && l[0] != '#'){
5 nishi 25
					char** r = cm_split(l, " \t");
26
					int i;
27
					if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")){
28
						for(i = 1; r[i] != NULL; i++){
29
							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")){
30
								for(i = 0; r[i] != NULL; i++) free(r[i]);
31
								free(r);
32
								free(line);
33
								free(l);
34
								fclose(f);
35
								return 1;
36
							}
37
						}
38
					}
39
					for(i = 0; r[i] != NULL; i++) free(r[i]);
40
					free(r);
4 nishi 41
				}
42
				free(l);
43
				free(line);
44
				line = malloc(1);
45
				line[0] = 0;
46
				if(c <= 0) break;
47
			}else if(cbuf[0] != '\r'){
48
				char* tmp = line;
49
				line = cm_strcat(tmp, cbuf);
50
				free(tmp);
51
			}
52
		}
53
		free(line);
54
		fclose(f);
55
		return 0;
56
	}else{
5 nishi 57
		cm_log("Config", "Could not open the file");
4 nishi 58
		return 1;
59
	}
60
}