src/server/config/conf.c

changeset 25
5dee29c7c530
parent 23
a2c8fc23c90e
child 36
450d2d5f4735
equal deleted inserted replaced
24:1a7853a4257e 25:5dee29c7c530
38 sstr_t mline; 38 sstr_t mline;
39 mline.ptr = NULL; 39 mline.ptr = NULL;
40 mline.length = 0; 40 mline.length = 0;
41 ConfigLine *start_line; 41 ConfigLine *start_line;
42 ConfigLine *end_line; 42 ConfigLine *end_line;
43 43
44 // read file 44 // read file
45 sstr_t l; 45 sstr_t l;
46 while((l = cfg_readln(in)).ptr != NULL) { 46 while((l = cfg_readln(in)).ptr != NULL) {
47 void *org_ptr = l.ptr;
48
47 // put the line to the list 49 // put the line to the list
48 ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine); 50 ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine);
49 line->line = sstrdub(l); 51 line->line = sstrdup_mp(parser->mp, l); // TODO: check for 0-len str
50 line->object = NULL; 52 line->object = NULL;
51 line->type = LINE_OTHER; 53 line->type = LINE_OTHER;
52 parser->lines = ucx_dlist_append(parser->lines, line); 54 parser->lines = ucx_dlist_append(parser->lines, line);
53 55
54 // check if the line contains something 56 // check if the line contains something
57 59
58 if(line->type == LINE_OTHER) { 60 if(line->type == LINE_OTHER) {
59 // check for multi line 61 // check for multi line
60 if(mline.ptr != NULL) { 62 if(mline.ptr != NULL) {
61 // concate lines 63 // concate lines
62 char *ptr = malloc(mline.length + l.length + 1); 64 char *ptr = ucx_mempool_malloc(
65 parser->mp,
66 mline.length + l.length + 1);
67
63 memcpy(ptr, mline.ptr, mline.length); 68 memcpy(ptr, mline.ptr, mline.length);
64 memcpy(ptr + mline.length - 1, l.ptr, l.length); 69 memcpy(ptr + mline.length - 1, l.ptr, l.length);
65 mline.length += l.length; 70 mline.length += l.length;
66 free(mline.ptr); 71 free(mline.ptr);
67 mline.ptr = ptr; 72 mline.ptr = ptr;
71 76
72 line->type = LINE_MULTI; 77 line->type = LINE_MULTI;
73 } 78 }
74 if(l.ptr[l.length - 1] == '\\') { 79 if(l.ptr[l.length - 1] == '\\') {
75 if(mline.ptr == NULL) { 80 if(mline.ptr == NULL) {
76 mline = sstrdub(l); 81 mline = sstrdup_mp(parser->mp, l);
77 start_line = line; 82 start_line = line;
78 } 83 }
79 } else { 84 } else {
80 // this line is complete so we can parse it 85 // this line is complete so we can parse it
81 sstr_t ll; // we parse this line 86 sstr_t ll; // we parse this line
86 start_line = line; 91 start_line = line;
87 end_line = line; 92 end_line = line;
88 } else { 93 } else {
89 ll = mline; 94 ll = mline;
90 } 95 }
91 96
92 // parse 97 // parse
93 int r = parser->parse(parser, start_line, end_line, ll); 98 int r = parser->parse(parser, start_line, end_line, ll);
94 99
95 // clean up 100 // clean up
96 if(mline.ptr != NULL) { 101 if(mline.ptr != NULL) {
104 if(r != 0) { 109 if(r != 0) {
105 return -1; 110 return -1;
106 } 111 }
107 } 112 }
108 } 113 }
114
115 free(org_ptr);
109 } 116 }
110 117
111 return 0; 118 return 0;
112 } 119 }
113 120
134 if((ptr = strrchr(buf, '\n'))) { 141 if((ptr = strrchr(buf, '\n'))) {
135 ptr[0] = 0; 142 ptr[0] = 0;
136 } 143 }
137 144
138 sstr_t line = sstr(buf); 145 sstr_t line = sstr(buf);
139 return line; 146 return sstrdub(line);
140 } 147 }
141 148
142 sstr_t s; 149 sstr_t s;
143 s.ptr = NULL; 150 s.ptr = NULL;
144 s.length = 0; 151 s.length = 0;
274 name.ptr = line.ptr; 281 name.ptr = line.ptr;
275 name.length = i; 282 name.length = i;
276 283
277 // create directive object 284 // create directive object
278 ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective); 285 ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective);
279 directive->directive_type = sstrdub(name); 286 directive->directive_type = sstrdup_mp(mp, name);
280 directive->type_num = cfg_get_directive_type_num(name); 287 directive->type_num = cfg_get_directive_type_num(name);
281 directive->condition = NULL; // set later by main parsing function 288 directive->condition = NULL; // set later by main parsing function
282 directive->param = NULL; 289 directive->param = NULL;
283 290
284 sstr_t param_str; 291 sstr_t param_str;
294 } 301 }
295 302
296 303
297 // create param object 304 // create param object
298 ConfigParam *param = OBJ_NEW(mp, ConfigParam); 305 ConfigParam *param = OBJ_NEW(mp, ConfigParam);
299 /* 306 param->name = sstrdup_mp(mp, pname);
300 * TODO:
301 * Wenn man sstrdub_mp statt sstrdub nimmt, wird der Inhalt von pname
302 * verunstaltet. Warum?
303 */
304 param->name = sstrdub(pname);
305 307
306 if(pvalue.length > 0) { 308 if(pvalue.length > 0) {
307 param->value = sstrdub(pvalue); 309 param->value = sstrdup_mp(mp, pvalue);
308 } else { 310 } else {
309 param->value.ptr = NULL; 311 param->value.ptr = NULL;
310 param->value.length = 0; 312 param->value.length = 0;
311 } 313 }
312 314
368 int cfg_get_line_type(sstr_t line) { 370 int cfg_get_line_type(sstr_t line) {
369 if(line.length < 3) { 371 if(line.length < 3) {
370 // this line is to short to be correct 372 // this line is to short to be correct
371 return LINE_ERROR; 373 return LINE_ERROR;
372 } 374 }
373 375
374 if(line.ptr[0] == '<') { 376 if(line.ptr[0] == '<') {
375 // start or end tag 377 // start or end tag
376 // TODO: check for space between '<' and '/' 378 // TODO: check for space between '<' and '/'
377 if(line.ptr[1] == '/') { 379 if(line.ptr[1] == '/') {
378 return LINE_END_TAG; 380 return LINE_END_TAG;
450 return NULL; // syntax error 452 return NULL; // syntax error
451 } 453 }
452 454
453 // create tag object 455 // create tag object
454 ConfigTag *tag = OBJ_NEW(mp, ConfigTag); 456 ConfigTag *tag = OBJ_NEW(mp, ConfigTag);
455 tag->name = sstrdub_mp(mp, name); 457 tag->name = sstrdup_mp(mp, name);
456 tag->param = NULL; 458 tag->param = NULL;
457 459
458 // parse parameters 460 // parse parameters
459 sstr_t param_str; 461 sstr_t param_str;
460 param_str.ptr = line.ptr + i; 462 param_str.ptr = line.ptr + i;
472 break; 474 break;
473 } 475 }
474 476
475 // create param object 477 // create param object
476 ConfigParam *param = OBJ_NEW(mp, ConfigParam); 478 ConfigParam *param = OBJ_NEW(mp, ConfigParam);
477 param->name = sstrdub_mp(mp, pname); 479 param->name = sstrdup_mp(mp, pname);
478 if(pvalue.length > 0) { 480 if(pvalue.length > 0) {
479 param->value = sstrdub_mp(mp, pvalue); 481 param->value = sstrdup_mp(mp, pvalue);
480 } else { 482 } else {
481 param->value.ptr = NULL; 483 param->value.ptr = NULL;
482 param->value.length = 0; 484 param->value.length = 0;
483 } 485 }
484 486

mercurial