src/server/config/mimeconf.c

changeset 79
f48cea237ec3
parent 62
c47e081b6c0f
child 88
73b3485e96f1
equal deleted inserted replaced
78:3578977d29a3 79:f48cea237ec3
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 32
33 #include "mimeconf.h" 33 #include "mimeconf.h"
34 34
35 typedef struct {
36 ucx_destructor destructor;
37 void *ptr;
38 } ucx_regdestr;
39
35 MimeConfig *load_mime_config(char *file) { 40 MimeConfig *load_mime_config(char *file) {
36 FILE *in = fopen(file, "r"); 41 FILE *in = fopen(file, "r");
37 if(in == NULL) { 42 if(in == NULL) {
38 return NULL; 43 return NULL;
39 } 44 }
41 MimeConfig *conf = malloc(sizeof(MimeConfig)); 46 MimeConfig *conf = malloc(sizeof(MimeConfig));
42 conf->parser.parse = mimeconf_parse; 47 conf->parser.parse = mimeconf_parse;
43 conf->file = file; 48 conf->file = file;
44 conf->directives = NULL; 49 conf->directives = NULL;
45 conf->ntypes = 0; 50 conf->ntypes = 0;
46
47 int r = cfg_parse_basic_file((ConfigParser*)conf, in); 51 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
52 cfg_list_destr(conf->parser.mp, conf->directives);
48 if(r != 0) { 53 if(r != 0) {
49 // TODO: free 54 // TODO: free
50 return NULL; 55 return NULL;
51 } 56 }
52 57
54 59
55 return conf; 60 return conf;
56 } 61 }
57 62
58 void free_mime_config(MimeConfig *conf) { 63 void free_mime_config(MimeConfig *conf) {
59 if(conf->directives != NULL) {
60 ucx_list_free(conf->directives);
61 }
62 if(conf->parser.lines != NULL) {
63 ucx_dlist_free(conf->parser.lines);
64 }
65 ucx_mempool_free(conf->parser.mp); 64 ucx_mempool_free(conf->parser.mp);
66 free(conf); 65 free(conf);
67 } 66 }
68 67
69 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) {
83 // comma-separated file extensions 82 // comma-separated file extensions
84 83
85 size_t nx = 0; 84 size_t nx = 0;
86 sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx); 85 sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx);
87 for(int i=0;i<nx;i++) { 86 for(int i=0;i<nx;i++) {
88 dir->exts = ucx_list_append(dir->exts, exts[i].ptr); 87 sstr_t extstr = sstrdup_mp(mp, exts[i]);
88 dir->exts = ucx_list_append(dir->exts, extstr.ptr);
89 free(exts[i].ptr);
89 } 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(conf->directives, dir); 96 conf->directives = ucx_list_append(conf->directives, dir);
96 conf->ntypes++; 97 conf->ntypes++;
97 98
98 return 0; 99 return 0;
99 } 100 }

mercurial