src/server/config/mimeconf.c

Tue, 09 Jul 2013 20:56:01 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 09 Jul 2013 20:56:01 +0200
changeset 86
49bb6c8ceb2b
parent 79
f48cea237ec3
child 88
73b3485e96f1
permissions
-rw-r--r--

replaced usage of deprecated openldap functions

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.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);
    cfg_list_destr(conf->parser.mp, conf->directives);
    if(r != 0) {
        // TODO: free
        return NULL;
    }
    
    fclose(in);

    return conf;
}

void free_mime_config(MimeConfig *conf) {
    ucx_mempool_free(conf->parser.mp);
    free(conf);
}

int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
    MimeConfig *conf = p;
    UcxMempool *mp = conf->parser.mp;
    
    // parse mime directive
    MimeDirective *dir = ucx_mempool_calloc(mp, 1, sizeof(MimeDirective));
    
    UcxList *params = cfg_param_list(line, mp);
    UCX_FOREACH(UcxList*, params, pl) {
        ConfigParam *param = pl->data;
        
        if(!sstrcmp(param->name, sstr("type"))) {
            dir->type = param->value;
        } else if(!sstrcmp(param->name, sstr("exts"))) {
            // comma-separated file extensions
            
            size_t nx = 0;
            sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx);
            for(int i=0;i<nx;i++) {
                sstr_t extstr = sstrdup_mp(mp, exts[i]);
                dir->exts = ucx_list_append(dir->exts, extstr.ptr);
                free(exts[i].ptr);
            }
            free(exts);
        }
    }
    
    cfg_list_destr(mp, dir->exts);
    conf->directives = ucx_list_append(conf->directives, dir);
    conf->ntypes++;

    return 0;
}

mercurial