#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucx/mempool.h>
#include "mimeconf.h"
typedef struct {
ucx_destructor destructor;
void *ptr;
} ucx_regdestr;
MimeConfig *load_mime_config(
char *file) {
FILE *in = fopen(file,
"r");
if(in ==
NULL) {
return NULL;
}
MimeConfig *conf = malloc(
sizeof(MimeConfig));
conf->parser.parse = mimeconf_parse;
conf->file = file;
conf->directives =
NULL;
conf->ntypes =
0;
int r = cfg_parse_basic_file((ConfigParser*)conf, in);
if(r !=
0) {
fclose(in);
free(conf);
return NULL;
}
fclose(in);
return conf;
}
void free_mime_config(MimeConfig *conf) {
ucx_mempool_destroy(conf->parser.mp->pool);
free(conf);
}
int mimeconf_parse(
void *p, ConfigLine *begin, ConfigLine *end,
sstr_t line) {
MimeConfig *conf = p;
UcxAllocator *mp = conf->parser.mp;
MimeDirective *dir =
OBJ_NEW_N(mp, MimeDirective);
UcxList *params = cfg_param_list(line, mp);
UCX_FOREACH(pl, params) {
ConfigParam *param = pl->data;
if(!sstrcmp(param->name, sstr(
"type"))) {
dir->type = param->value;
}
else if(!sstrcmp(param->name, sstr(
"exts"))) {
ssize_t nx =
0;
sstr_t *exts = sstrsplit(param->value, sstrn(
",",
1), &nx);
for(
int i=
0;i<nx;i++) {
sstr_t extstr = sstrdup_a(mp, exts[i]);
dir->exts = ucx_list_append_a(mp, dir->exts, extstr.ptr);
free(exts[i].ptr);
}
free(exts);
}
}
conf->directives = ucx_list_append_a(mp, conf->directives, dir);
conf->ntypes++;
return 0;
}