src/server/config/conf.c

changeset 415
d938228c382e
parent 115
51d9a15eac98
child 453
4586d534f9b5
--- a/src/server/config/conf.c	Wed Nov 02 19:19:01 2022 +0100
+++ b/src/server/config/conf.c	Sun Nov 06 15:53:32 2022 +0100
@@ -31,32 +31,30 @@
 #include <string.h>
 
 int cfg_parse_basic_file(ConfigParser *parser, FILE *in) {
-    parser->lines = NULL;
-    UcxMempool *mp = ucx_mempool_new(512);
-    parser->mp = mp->allocator;
+    parser->lines_begin = NULL;
+    parser->lines_end = NULL;
+    CxMempool *mp = cxBasicMempoolCreate(512);
+    CxAllocator *a = (CxAllocator*)mp->allocator;
+    parser->mp = a;
 
     // one logical line over many lines
-    sstr_t mline;
+    cxmutstr mline;
     mline.ptr = NULL;
     mline.length = 0;
-    ConfigLine *start_line;
-    ConfigLine *end_line;
+    ConfigLine *start_line = NULL;
+    ConfigLine *end_line = NULL;
     
     // read file
-    sstr_t l;
+    cxmutstr l;
     while((l = cfg_readln(in)).ptr != NULL) {
         void *org_ptr = l.ptr;
         
         // put the line to the list
         ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine);
-        line->line = sstrdup_a(parser->mp, l); // TODO: check for 0-len str
+        line->line = cx_strdup_a(parser->mp, cx_strcast(l)); // TODO: check for 0-len str
         line->object = NULL;
         line->type = LINE_OTHER;
-        if(parser->lines) {
-            parser->lines = ucx_list_append_a(parser->mp, parser->lines, line);
-        } else {
-            parser->lines = ucx_list_append_a(parser->mp, parser->lines, line);
-        }
+        CFG_LINE_ADD(&parser->lines_begin, &parser->lines_end, line);
 
         // check if the line contains something
         l = cfg_trim_comment(l);
@@ -66,10 +64,8 @@
             // check for multi line
             if(mline.ptr != NULL) {
                 // concate lines
-                char *ptr = ucx_mempool_malloc(
-                        mp,
-                        mline.length + l.length + 1);
-                
+                char *ptr = cxMalloc(a, mline.length + l.length + 1);
+                // TODO: maybe we can use cx_strcat
                 memcpy(ptr, mline.ptr, mline.length);
                 memcpy(ptr + mline.length - 1, l.ptr, l.length);
                 mline.length += l.length;
@@ -83,12 +79,12 @@
             }
             if(l.ptr[l.length - 1] == '\\') {
                 if(mline.ptr == NULL) {
-                    mline = sstrdup_a(parser->mp, l);
+                    mline = cx_strdup_a(parser->mp, cx_strcast(l));
                     start_line = line;
                 }
             } else {
                 // this line is complete so we can parse it
-                sstr_t ll; // we parse this line
+                cxmutstr ll; // we parse this line
 
                 if(mline.ptr == NULL) {
                     // single line
@@ -124,8 +120,8 @@
     return 0;
 }
 
-sstr_t cfg_readln(FILE *file) {
-    sstr_t ns;
+cxmutstr cfg_readln(FILE *file) {
+    cxmutstr ns;
     ns.ptr = NULL;
     ns.length = 0;
 
@@ -148,11 +144,11 @@
             ptr[0] = 0;
         }
 
-        sstr_t line = sstr(buf);
-        return sstrdup(line);
+        cxmutstr line = cx_mutstr(buf);
+        return cx_strdup(cx_strcast(line));
     }
 
-    sstr_t s;
+    cxmutstr s;
     s.ptr = NULL;
     s.length = 0;
     return s;
@@ -162,8 +158,8 @@
 /*
  * removes a comment from the line
  */
-sstr_t cfg_trim_comment(sstr_t line) {
-    sstr_t nl = line;
+cxmutstr cfg_trim_comment(cxmutstr line) {
+    cxmutstr nl = line;
     for(int i=0;i<line.length;i++) {
         if(line.ptr[i] == '#') {
             if(i > 0) {
@@ -177,7 +173,7 @@
             }
         }
     }
-    return sstrtrim(nl);
+    return cx_strtrim_m(nl);
 }
 
 /*
@@ -185,7 +181,7 @@
  * containing the other parameters or an empty string, if there are no more
  * parameters
  */
-sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value) {
+cxmutstr cfg_param(cxmutstr params, cxmutstr *name, cxmutstr *value) {
     name->ptr = NULL;
     name->length = 0;
     value->ptr = NULL;
@@ -204,7 +200,7 @@
 
             params.ptr = params.ptr + i;
             params.length -= i;
-            return sstrtrim(params);
+            return cx_strtrim_m(params);
         }
     }
 
@@ -214,7 +210,7 @@
     
     // get value
     if(i>=params.length) {
-        sstr_t ns;
+        cxmutstr ns;
         ns.ptr = NULL;
         ns.length = 0;
         return ns;
@@ -253,37 +249,33 @@
     // create new params string
     params.ptr += i;
     params.length -= i;
-    return sstrtrim(params);
+    return cx_strtrim_m(params);
 }
 
 /*
  * gets a value from a parameter
  */
-sstr_t cfg_param_get(UcxList *list, sstr_t name) {
-    while(list != NULL) {
-        ConfigParam *param = list->data;
-        if(!sstrcmp(param->name, name)) {
+cxmutstr cfg_param_get(ConfigParam *param, cxstring name) {
+    while(param != NULL) {
+        if(!cx_strcmp((cxstring){param->name.ptr, param->name.length}, name)) {
             return param->value;
         }
-        list = list->next;
+        param = param->next;
     }
-    sstr_t ns;
-    ns.ptr = NULL;
-    ns.length = 0;
-    return ns;
+    return (cxmutstr){ NULL, 0 };
 }
 
 /*
  * parses a line containing a directive and returns a ConfigDirective object
  * or NULL if an error occurs
  */
-ConfigDirective* cfg_parse_directive(sstr_t line, UcxAllocator *mp) {
+ConfigDirective* cfg_parse_directive(cxmutstr line, CxAllocator *mp) {
     if(line.length < 6) {
         log_ereport(LOG_FAILURE, "cfg_parse_directive: line too short");
         return NULL; // line too short
     }
 
-    sstr_t name;
+    cxstring name;
 
     int i;
     for(i=0;i<line.length;i++) {
@@ -296,20 +288,20 @@
 
     // create directive object
     ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective);
-    directive->directive_type = sstrdup_a(mp, name);
+    directive->directive_type = cx_strdup_a(mp, name);
     directive->type_num = cfg_get_directive_type_num(name);
     directive->condition = NULL; // set later by main parsing function
     //directive->param = NULL;
 
-    sstr_t param_str;
+    cxstring param_str;
     param_str.ptr = name.ptr + i;
     param_str.length = line.length - i;
-    param_str = sstrtrim(param_str);
-    directive->value = sstrdup_a(mp, param_str);
+    param_str = cx_strtrim(param_str);
+    directive->value = cx_strdup_a(mp, param_str);
     
     /*
-    sstr_t pname;
-    sstr_t pvalue;
+    cxmutstr pname;
+    cxmutstr pvalue;
     for(;;) {
         param_str = cfg_param(param_str, &pname, &pvalue);
         if(pname.length <= 0) {
@@ -319,10 +311,10 @@
 
         // create param object
         ConfigParam *param = OBJ_NEW(mp, ConfigParam);
-        param->name = sstrdup_mp(mp, pname);
+        param->name = cx_strdup_mp(mp, pname);
 
         if(pvalue.length > 0) {
-            param->value = sstrdup_mp(mp, pvalue);
+            param->value = cx_strdup_mp(mp, pvalue);
         } else {
             param->value.ptr = NULL;
             param->value.length = 0;
@@ -337,10 +329,11 @@
     return directive;
 }
 
-UcxList* cfg_param_list(sstr_t param_str, UcxAllocator *mp) {
-    sstr_t pname;
-    sstr_t pvalue;
-    UcxList *plist = NULL;
+ConfigParam* cfg_param_list(cxmutstr param_str, CxAllocator *mp) {
+    cxmutstr pname;
+    cxmutstr pvalue;
+    ConfigParam *plist_begin = NULL;
+    ConfigParam *plist_end = NULL;
     for(;;) {
         param_str = cfg_param(param_str, &pname, &pvalue);
         if(pname.length <= 0) {
@@ -349,19 +342,20 @@
 
         // create param object
         ConfigParam *param = OBJ_NEW(mp, ConfigParam);
-        param->name = sstrdup_a(mp, pname);
+        param->name = cx_strdup_a(mp, cx_strcast(pname));
+        param->next = NULL;
 
         if(pvalue.length > 0) {
-            param->value = sstrdup_a(mp, pvalue);
+            param->value = cx_strdup_a(mp, cx_strcast(pvalue));
         } else {
             param->value.ptr = NULL;
             param->value.length = 0;
         }
 
         // add param to list
-        plist = ucx_list_append_a(mp, plist, param);
+        CFG_PARAM_ADD(&plist_begin, &plist_end, param);
     }
-    return plist;
+    return plist_begin;
 }
 
 
@@ -377,26 +371,26 @@
  *   AddLog         5
  *   Init           6
  */
-int cfg_get_directive_type_num(sstr_t type) {
+int cfg_get_directive_type_num(cxstring type) {
     /* get nsapi function type */
     
     // TODO: replace hard coded numbers
     int dt = -1;
-    if(sstrcmp(type, sstr("AuthTrans")) == 0) {
+    if(cx_strcmp(type, cx_str("AuthTrans")) == 0) {
         dt = NSAPIAuthTrans;
-    } else if(sstrcmp(type, sstr("NameTrans")) == 0) {
+    } else if(cx_strcmp(type, cx_str("NameTrans")) == 0) {
         dt = NSAPINameTrans;
-    } else if(sstrcmp(type, sstr("PathCheck")) == 0) {
+    } else if(cx_strcmp(type, cx_str("PathCheck")) == 0) {
         dt = NSAPIPathCheck;
-    } else if(sstrcmp(type, sstr("ObjectType")) == 0) {
+    } else if(cx_strcmp(type, cx_str("ObjectType")) == 0) {
         dt = NSAPIObjectType;
-    } else if(sstrcmp(type, sstr("Service")) == 0) {
+    } else if(cx_strcmp(type, cx_str("Service")) == 0) {
         dt = NSAPIService;
-    } else if(sstrcmp(type, sstr("Error")) == 0) {
+    } else if(cx_strcmp(type, cx_str("Error")) == 0) {
         dt = NSAPIError;
-    } else if(sstrcmp(type, sstr("AddLog")) == 0) {
+    } else if(cx_strcmp(type, cx_str("AddLog")) == 0) {
         dt = NSAPIAddLog;
-    } else if(sstrcmp(type, sstr("Init")) == 0) {
+    } else if(cx_strcmp(type, cx_str("Init")) == 0) {
         dt = INIT_DIRECTIVE;
     }
     return dt;
@@ -405,7 +399,7 @@
 /*
  * checks if the line contains only a comment or space
  */
-int cfg_get_basic_type(sstr_t line) {
+int cfg_get_basic_type(cxmutstr line) {
     if(line.length == 0) {
         return LINE_NOCONTENT;
     } else if(line.ptr[0] == '#') {
@@ -417,7 +411,7 @@
 /*
  * checks if the line contains a begin/end tag or a directive
  */
-int cfg_get_line_type(sstr_t line) {
+int cfg_get_line_type(cxmutstr line) {
     if(line.length < 3) {
         // this line is to short to be correct
         return LINE_ERROR;
@@ -436,16 +430,16 @@
     }
 }
 
-int cfg_get_tag_type(sstr_t tag) {
-    if(!sstrcmp(tag, sstr("Object"))) {
+int cfg_get_tag_type(cxstring tag) {
+    if(!cx_strcmp(tag, cx_str("Object"))) {
         return TAG_OBJECT;
-    } else if(!sstrcmp(tag, sstr("If"))) {
+    } else if(!cx_strcmp(tag, cx_str("If"))) {
         return TAG_IF;
-    } else if(!sstrcmp(tag, sstr("ElseIf"))) {
+    } else if(!cx_strcmp(tag, cx_str("ElseIf"))) {
         return TAG_ELSEIF;
-    } else if(!sstrcmp(tag, sstr("Else"))) {
+    } else if(!cx_strcmp(tag, cx_str("Else"))) {
         return TAG_ELSE;
-    } else if(!sstrcmp(tag, sstr("Client"))) {
+    } else if(!cx_strcmp(tag, cx_str("Client"))) {
         return TAG_CLIENT;
     }
     return -1;
@@ -455,8 +449,8 @@
  * returns the name of the ending tag
  * on error, this functions returns a zero length string
  */
-sstr_t cfg_get_end_tag_name(sstr_t line) {
-    sstr_t ns;
+cxmutstr cfg_get_end_tag_name(cxmutstr line) {
+    cxmutstr ns;
     ns.ptr = NULL;
     ns.length = 0;
 
@@ -465,7 +459,7 @@
         return ns;
     }
 
-    sstr_t name;
+    cxmutstr name;
     name.ptr = line.ptr + 2;
     name.length = line.length - 3;
 
@@ -477,10 +471,10 @@
         return ns;
     }
 
-    return sstrtrim(name);
+    return cx_strtrim_m(name);
 }
 
-ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxAllocator *mp) {
+ConfigTag* cfg_parse_begin_tag(cxmutstr line, CxAllocator *mp) {
     if(line.length < 4) {
         return NULL; // this line can't contain a valid tag
     }
@@ -489,7 +483,7 @@
         return NULL; // syntax error
     }
 
-    sstr_t name;
+    cxmutstr name;
     name.ptr = line.ptr + 1;
     int i;
     for(i=1;i<line.length - 1;i++) {
@@ -504,21 +498,21 @@
 
     // create tag object
     ConfigTag *tag = OBJ_NEW(mp, ConfigTag);
-    tag->name = sstrdup_a(mp, name);
+    tag->name = cx_strdup_a(mp, cx_strcast(name));
     tag->param = NULL;
 
     // parse parameters
-    sstr_t param_str;
+    cxmutstr param_str;
     param_str.ptr = line.ptr + i;
     param_str.length = line.length - name.length - 2;
-    param_str = sstrtrim(param_str);
+    param_str = cx_strtrim_m(param_str);
     if(param_str.length == 0) {
         return tag; // no parameters
     }
-    tag->param_str = sstrdup_a(mp, param_str);
+    tag->param_str = cx_strdup_a(mp, cx_strcast(param_str));
 
-    sstr_t pname;
-    sstr_t pvalue;
+    cxmutstr pname;
+    cxmutstr pvalue;
     for(;;) {
         param_str = cfg_param(param_str, &pname, &pvalue);
         if(pname.length == 0) {
@@ -527,16 +521,17 @@
 
         // create param object
         ConfigParam *param = OBJ_NEW(mp, ConfigParam);
-        param->name = sstrdup_a(mp, pname);
+        param->next = NULL;
+        param->name = cx_strdup_a(mp, cx_strcast(pname));
         if(pvalue.length > 0) {
-            param->value = sstrdup_a(mp, pvalue);
+            param->value = cx_strdup_a(mp, cx_strcast(pvalue));
         } else {
             param->value.ptr = NULL;
             param->value.length = 0;
         }
 
         // add param to list
-        tag->param = ucx_list_append_a(mp, tag->param, param);
+        CFG_PARAM_ADD(&tag->param, NULL, param);
     }
 
     return tag;
@@ -549,11 +544,13 @@
  * gets a ConfigDirective with a specific name from a List of directives
  * returns a directive or NULL, if the directive cannot be found
  */
-ConfigDirective* cfg_directivelist_get(UcxList *dirs, sstr_t name) {
+// TODO: remove
+/*
+ConfigDirective* cfg_directivelist_get(UcxList *dirs, cxmutstr name) {
     while(dirs != NULL) {
         ConfigDirective *d = dirs->data;
         if(d != NULL) {
-            if(!sstrcmp(d->directive_type, name)) {
+            if(!cx_strcmp(d->directive_type, name)) {
                 return d;
             }
         }
@@ -562,10 +559,10 @@
     return NULL;
 }
 
-sstr_t cfg_directivelist_get_str(UcxList *dirs, sstr_t name) {
+cxmutstr cfg_directivelist_get_str(UcxList *dirs, cxmutstr name) {
     ConfigDirective *d = cfg_directivelist_get(dirs, name);
     if(d == NULL) {
-        sstr_t n;
+        cxmutstr n;
         n.ptr = NULL;
         n.length = 0;
         return n;
@@ -573,16 +570,17 @@
     //return cfg_directive_pstr1(d);
     return d->value;
 }
+*/
 
 /*
  * returns the name of the first parameter of the directive
  * useful for 'name value' directives
  */
 /*
-sstr_t cfg_directive_pstr1(ConfigDirective *dir) {
+cxmutstr cfg_directive_pstr1(ConfigDirective *dir) {
     if(dir->param == NULL) {
         fprintf(stderr, "%s", "Error: cfg_directive_pstr1: param is NULL\n");
-        sstr_t n;
+        cxmutstr n;
         n.ptr = NULL;
         n.length = 0;
         return n;
@@ -591,7 +589,7 @@
     ConfigParam *p = dir->param->data;
     return p->name;
 }
-*/
+
 
 static void cfg_list_free(void *list) {
     ucx_list_free(list);
@@ -600,14 +598,8 @@
 static void cfg_map_free(void *map) {
     ucx_map_free(map);
 }
+*/
 
 
 
-void cfg_map_destr(UcxMempool *mp, UcxMap *map) {
-    if(map) {
-        ucx_mempool_reg_destr(mp, map, cfg_map_free);
-    }
-}
 
-
-

mercurial