src/server/config/conf.h

Sat, 12 Nov 2022 11:01:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 12 Nov 2022 11:01:11 +0100
changeset 423
bb7cff720dd0
parent 415
d938228c382e
child 505
d41fc7f37aed
permissions
-rw-r--r--

add obj.conf expression parser

/*
 * 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.
 */

#ifndef CFG_CONF_H
#define	CFG_CONF_H

#include <stdio.h>
#include <stdlib.h>

#include <cx/linked_list.h>
#include <cx/hash_map.h>
#include <cx/mempool.h>
#include <cx/basic_mempool.h>
#include <cx/string.h>
#include <cx/utils.h>
#include <cx/compare.h>

#include "../util/object.h"

#ifdef	__cplusplus
extern "C" {
#endif
   
// mempool malloc macro
#define OBJ_NEW(p, type) (type*)cxMalloc(p, sizeof(type))
#define OBJ_NEW_N(p, type) (type*)cxCalloc(p, 1, sizeof(type))

// line types
#define LINE_OTHER      0
#define LINE_DIRECTIVE  1
#define LINE_BEGIN_TAG  2
#define LINE_END_TAG    3
#define LINE_MULTI      4
#define LINE_NOCONTENT  5 // only comment or space
#define LINE_ERROR      6 // parse error on this line

// tag types
#define TAG_OBJECT      0
#define TAG_IF          1
#define TAG_ELSEIF      2
#define TAG_ELSE        3
#define TAG_CLIENT      4

    
#define INIT_DIRECTIVE  16    
    
#define CFG_LINE_ADD(list_begin, list_end, elm) \
    cx_linked_list_add((void**)list_begin, (void**)list_end, offsetof(ConfigLineList, prev), offsetof(ConfigLineList, next), elm)
    
#define CFG_PARAM_ADD(list_begin, list_end, elm) \
    cx_linked_list_add((void**)list_begin, (void**)list_end, -1, offsetof(ConfigParam, next), elm)

#define CFG_DIRECTIVES_ADD(list, dir) \
    cx_linked_list_add((void**)list, NULL, -1, offsetof(ConfigDirectiveList, next), dir)
    
#define CFG_NUM_PARAMS(param) cx_linked_list_size(param, offsetof(ConfigParam, next))
    
typedef struct _cfg_line {
    cxmutstr line;    // raw line string
    void   *object; // pointer to data struct
    int    type;    // type, see line types
} ConfigLine;

typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, cxmutstr);

typedef struct _cfg_param ConfigParam;
struct _cfg_param {
    cxmutstr     name;
    cxmutstr     value;
    ConfigParam  *next;
};

typedef struct ConfigLineList ConfigLineList;
struct ConfigLineList {
    ConfigLine *line;
    ConfigLineList *prev;
    ConfigLineList *next;
};

typedef struct _cfg_parser {
    CxAllocator    *mp;
    ConfigLineList *lines_begin;
    ConfigLineList *lines_end;
    cfg_parse_f    parse;
} ConfigParser;


typedef struct _conf_tag ConfigTag;
struct _conf_tag {
    ConfigLine      *begin;
    ConfigLine      *end;

    cxmutstr        name;
    ConfigParam     *param;
    cxmutstr        param_str;
    ConfigTag       *parent;
    ConfigTag       *iftag; // only used by <ElseIf> and <Else>
    int             type_num;
};

typedef struct _conf_directive {
    ConfigLine *begin;
    ConfigLine *end;

    cxmutstr     directive_type;
    cxmutstr     value;
    //UcxList    *param;
    ConfigTag  *condition;
    int        type_num;
} ConfigDirective;

typedef struct ConfigDirectiveList ConfigDirectiveList;
struct ConfigDirectiveList {
    ConfigDirective *directive;
    ConfigDirectiveList *next;
};

int cfg_parse_basic_file(ConfigParser *parser, FILE *in);

cxmutstr cfg_readln(FILE *file);

cxmutstr cfg_trim_comment(cxmutstr line);

cxmutstr cfg_param(cxmutstr params, cxmutstr *name, cxmutstr *value);

cxmutstr cfg_param_get(ConfigParam *list, cxstring name);

ConfigDirective* cfg_parse_directive(cxmutstr line, CxAllocator *mp);

ConfigParam* cfg_param_list(cxmutstr param_str, CxAllocator *mp);

int cfg_get_directive_type_num(cxstring type);

int cfg_get_basic_type(cxmutstr line);

int cfg_get_line_type(cxmutstr line);

int cfg_get_tag_type(cxstring tag);

cxmutstr cfg_get_end_tag_name(cxmutstr line);

ConfigTag* cfg_parse_begin_tag(cxmutstr line, CxAllocator *mp);

//ConfigDirective* cfg_directivelist_get(UcxList *dirs, cxmutstr name);

//cxmutstr cfg_directivelist_get_str(UcxList *dirs, cxmutstr name);

cxmutstr cfg_directive_pstr1(ConfigDirective *dir);



#ifdef	__cplusplus
}
#endif

#endif	/* CFG_CONF_H */

mercurial