1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
*
* File : config_parser.c
*
* Author : Zurcher Jeremy
*
* Date :
*
* Purpose :
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "config_parser.h"
#define COMMENT '#'
#define STREND '\0'
#define NEWLINE '\n'
#define TABULATION '\t'
#define SPACE ' '
enum { NOPE, UNFINISHED_LINE, UNFINISHED_COMMENT };
struct config_reader {
int fd;
int last;
char *pos;
char *end;
int state;
int current_line;
char buffer[BUFFLEN];
};
static struct config_reader reader = { 0 };
#ifdef _DEBUG_
static void debug_buffer() {
printf("\tBUFFER : ");
int i;
for (i=0; i<BUFFLEN; i++) {
char ch = reader.buffer[i];
if(ch==NEWLINE)printf("*");
else if(ch==STREND)printf("@");
else printf("%c",ch);
}
printf("\n\tpos : %c[%d]\n",*reader.pos,(reader.pos-reader.buffer));
printf("\tend : %c[%d]\n",*reader.end,(reader.end-reader.buffer));
}
#endif /* _DEBUG_ */
static int feed_buffer(int fd, char *buffer, int len)
{
int ret;
if(reader.last) return 0;
ret = read(fd,buffer,len);
if(ret==-1) return -1;
if(ret<len) reader.last = 1;
buffer[ret] = STREND;
reader.pos = buffer;
reader.end = &buffer[ret];
return ret;
}
static char* eat_comment(char *from)
{
int ret;
char *tmp;
for(;;) {
tmp = strchr(from, NEWLINE);
if(tmp!=NULL) {
reader.current_line++;
return tmp;
}
ret = feed_buffer(reader.fd, reader.buffer, BUFFLEN-1);
if(ret==-1) return (char*)-1;
if(ret==0) return NULL;
from = reader.pos;
}
}
static char* clean_tail(char *str)
{
char *tmp = strchr(str, COMMENT);
if(tmp!=NULL) {
*tmp=STREND;
reader.state = UNFINISHED_COMMENT;
} else {
reader.state = UNFINISHED_LINE;
}
tmp = strchr(str, STREND);
for(;;) {
tmp--;
if(*tmp!=SPACE && *tmp!=TABULATION) break;
}
tmp++;
*tmp=STREND;
return tmp;
}
static char* prepare_line()
{
int len;
int ret;
char *cret;
char *tmp;
tmp = strchr(reader.pos, NEWLINE);
if(tmp!=NULL) {
*tmp=STREND;
cret = reader.pos;
reader.pos = tmp+1;
clean_tail(cret);
reader.state = NOPE;
return cret;
}
len = (reader.end-reader.pos);
memmove(reader.buffer,reader.pos,len);
reader.pos = reader.buffer+len;
ret = feed_buffer(reader.fd, reader.pos, reader.end-reader.pos);
if(ret==-1) return (char*)-1;
tmp = strchr(reader.pos, NEWLINE);
if(tmp!=NULL) {
*tmp=STREND;
reader.pos = tmp+1;
clean_tail(reader.buffer);
reader.state = NOPE;
}
else {
clean_tail(reader.pos);
}
return reader.buffer;
}
static char* find_next_token()
{
int ret;
char *end;
char *tmp = reader.pos;
if(reader.state==UNFINISHED_COMMENT){
tmp=eat_comment(reader.pos);
if(tmp==(char*)-1) return (char*)-1;
if(tmp==NULL) return NULL;
reader.state=NOPE;
}
else if (reader.state==UNFINISHED_LINE) {
ret = feed_buffer(reader.fd, reader.buffer, BUFFLEN-1);
if(ret==-1) return (char*)-1;
if(ret==0) return NULL;
reader.state=NOPE;
return prepare_line();
}
end = reader.end;
reader.current_line++;
for(;;) {
if(tmp==end) {
int ret = feed_buffer(reader.fd, reader.buffer, BUFFLEN-1);
if(ret==-1) return (char*)-1;
if(ret==0) return NULL;
tmp = reader.pos;
end = reader.end;
}
if(*tmp==NEWLINE) {
reader.current_line++;
}
else if(*tmp==COMMENT) {
tmp = eat_comment(tmp);
if(tmp==(char*)-1) return (char*)-1;
if(tmp==NULL) return NULL;
end = reader.end;
}
else if(*tmp!=SPACE || *tmp!=TABULATION) {
reader.pos = tmp;
return prepare_line();
}
tmp++;
}
}
static int init(char *filename)
{
if(reader.fd) close(reader.fd);
reader.fd = open(filename,O_RDONLY);
if(reader.fd==-1) return 1;
reader.state = NOPE;
reader.last = 0;
reader.current_line = 0;
reader.pos = reader.end = reader.buffer;
return 0;
}
int read_config(char *filename, config_callback callback)
{
int ret = EXIT_SUCCESS;
char *token;
if(init(filename)) return EXIT_FAILURE;
for(;;) {
token = find_next_token();
if(token==NULL) break;
if(token==(char*)-1) { ret=EXIT_FAILURE; break; }
ret = callback(token,reader.state!=NOPE);
if(ret) break;
}
close(reader.fd);
reader.fd = -1;
return ret;
}
|