-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings_parser.c
249 lines (226 loc) · 8.03 KB
/
settings_parser.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "include/settings_parser.h"
/*definitions for the lexycal analyser*/
#define LEXEME_LEN 20
#define LEXEME_LEN_INC 10
/*token names*/
#define WORD 0
#define SEPARATOR 2
#define ENDLINE 3
/*definitions used to manage the dynamic creation of the settings list*/
#define NUM_SETTINGS 10
#define NUM_SETTINGS_INC 10
/*struct that represents a token returned by the lexycal analyser*/
typedef struct _token{
int name;
char *value;
} token;
/*global variables*/
char __current = ' '; //the current character considered by the lexer
int __streamEnded = 0; //indicates the end of the token stream from the lexer
token __curr_token; //contains the current token returned by the lexycal analyser
FILE *__pFileSettings; //FILE structure for the settings file
int __num_lines = 0; //used to save the number of lines parsed
/*function prototypes*/
void __remove_whitespaces(FILE *__pFileSettings);
int __check_space(int charsRead, int *currentMaxLen, char **pointer);
int __get_next_token();
int __line(setting*);
// ===========================================================================
// free_settings_structure
// ===========================================================================
void free_settings_structure(settingsList *str){
int i;
for(i = 0; i < str->count; i++){
free(str->list[i].name);
}
free(str->list);
}
// ===========================================================================
// get_setting_by_name
// ===========================================================================
char *get_setting_by_name(char *name, settingsList *list){
int i;
for(i = 0; i < list->count; i++){
if(strcmp(list->list[i].name, name) == 0){
int len = strlen(list->list[i].value) + 1;
char *tmp = malloc(sizeof(char) * len);
if(!tmp){
fprintf(stderr, "get_setting_by_name: error while allocating memory.\n");
return NULL;
}
strcpy(tmp,list->list[i].value);
return tmp;
}
}
return NULL;
}
// ****************************************************************************
// PARSING [PARSER]
// ****************************************************************************
// ===========================================================================
// parse_settings
// ===========================================================================
int parse_settings(char *filename, settingsList *settings){
__pFileSettings = fopen(filename, "r");
if(!__pFileSettings){
fprintf(stderr, "Error while opening the settings file.\n");
return -1;
}
/*now we allocate space for the settings list*/
settings->list = (setting*) malloc(NUM_SETTINGS * sizeof(setting));
if(!settings->list){
fprintf(stderr, "Error while allocating memory\n");
return -1;
}
int currentCapacity = NUM_SETTINGS;
settings->count = 0;
__streamEnded = __get_next_token();
if( __streamEnded == -1) return -1; //an error occurred.
while(!__streamEnded){ //while there are tokens available..
__num_lines++;
if(settings->count >= currentCapacity){
currentCapacity+= NUM_SETTINGS_INC;
settings->list = realloc(settings->list, currentCapacity * sizeof(setting));
if(!settings->list){
fprintf(stderr, "Error while reallocating memory\n");
return -1;
}
}
setting set;
int rval = __line(&set);
if(rval == -1){
return -1;
}else if(rval == 1){ //empty line, skip
continue;
}else{
settings->list[settings->count++] = set;
}
}
fclose(__pFileSettings);
return 0;
}
// ===========================================================================
// line
// ===========================================================================
int __line(setting *set){
if(__curr_token.name == ENDLINE){
__streamEnded = __get_next_token();
return 1;
}else{
if(__curr_token.name != WORD){
fprintf(stderr, "Syntax error while parsing the settings file (line %d): a setting " \
"name is missing.\n", __num_lines);
return -1;
}
set->name = __curr_token.value;
__streamEnded = __get_next_token();
if(__streamEnded == 1){
fprintf(stderr, "Syntax error while parsing the settings file (line %d): line not " \
"ended.\n", __num_lines);
return -1;
}
if(__curr_token.name != SEPARATOR){
fprintf(stderr, "Syntax error while parsing the settings file (line %d): a separator " \
"\">\" is missing.\n", __num_lines);
return -1;
}
__streamEnded = __get_next_token();
if(__streamEnded == 1){
fprintf(stderr, "Syntax error while parsing the settings file (line %d): line not "\
"finished.\n", __num_lines);
return -1;
}
if(__curr_token.name != WORD){
fprintf(stderr, "Syntax error while parsing the setting file (line %d): value is " \
"missing.\n", __num_lines);
return -1;
}
set->value = __curr_token.value;
__streamEnded = __get_next_token();
if(__streamEnded == 1){
fprintf(stderr, "Syntax error while parsing the setting (line %d): line not "\
"finished.\n", __num_lines);
return -1;
}
if(__curr_token.name != ENDLINE){
fprintf(stderr, "Syntax error while parsing the setting file (line %d): a newline \"\\n\" "\
"is missing.\n", __num_lines);
return -1;
}
__streamEnded = __get_next_token();
return 0;
}
}
// *****************************************************************************
// SCANNING [LEXER]
// *****************************************************************************
// ===========================================================================
// __remove_whitespaces
// ===========================================================================
void __remove_whitespaces(FILE *__pFileSettings){
for( ; ; __current = fgetc(__pFileSettings)){
if(__current != ' ' && __current != '\t' && __current != '\r') break;
}
}
// ===========================================================================
// __check_space
// Description: A function that reallocates space. Pattern used frequently inside this
// source code, so it's better to have it in a function.
// ===========================================================================
int __check_space(int charsRead, int *currentMaxLen, char **pointer){
if(charsRead + 2 >= *currentMaxLen){ //if there is not sufficient space
*currentMaxLen = *currentMaxLen + LEXEME_LEN_INC;
*pointer = (char *) realloc(*pointer, *currentMaxLen * sizeof(char));
if(!(*pointer)){
fprintf(stderr, "Error while reallocating memory.\n");
return -1;
}
}
return 0;
}
// ===========================================================================
// __get_next_token
// ===========================================================================
int __get_next_token(){
__remove_whitespaces(__pFileSettings);
int charsRead = 0;
int currentMaxLen = LEXEME_LEN;
char *temp = (char *) malloc(currentMaxLen * sizeof(char));
if(!temp){
fprintf(stderr, "Error while allocating memory.\n");
return -1;
}
/*read and analyze the input*/
while(__current != EOF){
if(__check_space(charsRead + 2, ¤tMaxLen, &temp) == -1) return -1;
if(__current == '>'){ //SEPARATOR
__curr_token.name = SEPARATOR;
__curr_token.value = ">";
__current = fgetc(__pFileSettings);
return 0;
}else if(__current == '\n'){ //ENDLINE
__curr_token.name = ENDLINE;
__curr_token.value = "\n";
__current = fgetc(__pFileSettings);
return 0;
}else if(isalnum(__current) || __current == ':'){ //WORD
do{
temp[charsRead++] = __current;
if(__check_space(charsRead + 2, ¤tMaxLen, &temp) == -1) return -1;
__current = fgetc(__pFileSettings);
}while(isalnum(__current) || __current == ':' || __current == '.');
temp[charsRead] = '\0';
__curr_token.name = WORD;
__curr_token.value = temp;
return 0;
}else if(__current == '#'){ //Ignore comments
do{
__current = fgetc(__pFileSettings);
}while(__current != '\n' && __current != EOF);
}else{
fprintf(stderr, "Lexical error while parsing the settings.\n");
return -1;
}
}
return 1;
}