src/server/config/mimeconf.c

changeset 91
fac51f87def0
parent 88
73b3485e96f1
child 97
09fbefc0e6a9
equal deleted inserted replaced
90:279f343bbf6c 91:fac51f87def0
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 32
33 #include <ucx/mempool.h>
33 #include "mimeconf.h" 34 #include "mimeconf.h"
34 35
35 typedef struct { 36 typedef struct {
36 ucx_destructor destructor; 37 ucx_destructor destructor;
37 void *ptr; 38 void *ptr;
47 conf->parser.parse = mimeconf_parse; 48 conf->parser.parse = mimeconf_parse;
48 conf->file = file; 49 conf->file = file;
49 conf->directives = NULL; 50 conf->directives = NULL;
50 conf->ntypes = 0; 51 conf->ntypes = 0;
51 int r = cfg_parse_basic_file((ConfigParser*)conf, in); 52 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
52 cfg_list_destr(conf->parser.mp, conf->directives);
53 if(r != 0) { 53 if(r != 0) {
54 // TODO: free 54 // TODO: free
55 return NULL; 55 return NULL;
56 } 56 }
57 57
59 59
60 return conf; 60 return conf;
61 } 61 }
62 62
63 void free_mime_config(MimeConfig *conf) { 63 void free_mime_config(MimeConfig *conf) {
64 ucx_mempool_destroy(conf->parser.mp); 64 ucx_mempool_destroy(conf->parser.mp->pool);
65 free(conf); 65 free(conf);
66 } 66 }
67 67
68 int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { 68 int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
69 MimeConfig *conf = p; 69 MimeConfig *conf = p;
70 UcxMempool *mp = conf->parser.mp; 70 UcxAllocator *mp = conf->parser.mp;
71 71
72 // parse mime directive 72 // parse mime directive
73 MimeDirective *dir = ucx_mempool_calloc(mp, 1, sizeof(MimeDirective)); 73 MimeDirective *dir = OBJ_NEW_N(mp, MimeDirective);
74 74
75 UcxList *params = cfg_param_list(line, mp); 75 UcxList *params = cfg_param_list(line, mp);
76 UCX_FOREACH(UcxList*, params, pl) { 76 UCX_FOREACH(pl, params) {
77 ConfigParam *param = pl->data; 77 ConfigParam *param = pl->data;
78 78
79 if(!sstrcmp(param->name, sstr("type"))) { 79 if(!sstrcmp(param->name, sstr("type"))) {
80 dir->type = param->value; 80 dir->type = param->value;
81 } else if(!sstrcmp(param->name, sstr("exts"))) { 81 } else if(!sstrcmp(param->name, sstr("exts"))) {
82 // comma-separated file extensions 82 // comma-separated file extensions
83 83
84 size_t nx = 0; 84 size_t nx = 0;
85 sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx); 85 sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx);
86 for(int i=0;i<nx;i++) { 86 for(int i=0;i<nx;i++) {
87 sstr_t extstr = sstrdup_mp(mp, exts[i]); 87 sstr_t extstr = sstrdup_a(mp, exts[i]);
88 dir->exts = ucx_list_append(dir->exts, extstr.ptr); 88 dir->exts = ucx_list_append_a(mp, dir->exts, extstr.ptr);
89 free(exts[i].ptr); 89 free(exts[i].ptr);
90 } 90 }
91 free(exts); 91 free(exts);
92 } 92 }
93 } 93 }
94 94
95 cfg_list_destr(mp, dir->exts); 95 conf->directives = ucx_list_append_a(mp, conf->directives, dir);
96 conf->directives = ucx_list_append(conf->directives, dir);
97 conf->ntypes++; 96 conf->ntypes++;
98 97
99 return 0; 98 return 0;
100 } 99 }

mercurial