make config parser delimiters configurable

Sat, 12 Nov 2022 12:09:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 12 Nov 2022 12:09:37 +0100
changeset 425
6440171c257f
parent 424
3df9258cd3cc
child 426
6a2e7a464991

make config parser delimiters configurable

src/server/config/objconf.c file | annotate | diff | comparison | revisions
src/server/config/serverconfig.c file | annotate | diff | comparison | revisions
src/server/config/serverconfig.h file | annotate | diff | comparison | revisions
--- a/src/server/config/objconf.c	Sat Nov 12 11:52:47 2022 +0100
+++ b/src/server/config/objconf.c	Sat Nov 12 12:09:37 2022 +0100
@@ -75,6 +75,7 @@
     ConfigParser2 parser;
     memset(&parser, 0, sizeof(ConfigParser2));
     parser.mp = mp;
+    parser.delim = "()/*%";
     parser.validateDirective = objectconf_validate_directive;
     parser.validateObjBegin = objectconf_validate_objbegin;
     parser.validateObjEnd = objectconf_validate_objend;
--- a/src/server/config/serverconfig.c	Sat Nov 12 11:52:47 2022 +0100
+++ b/src/server/config/serverconfig.c	Sat Nov 12 12:09:37 2022 +0100
@@ -49,6 +49,7 @@
     memset(&parser, 0, sizeof(ConfigParser2));
     parser.mp = mp;
     parser.allow_hierarchy = true;
+    parser.delim = "";
     ConfigNode *root = serverconfig_load_file(&parser, file);
     if(!root) {
         cxMempoolDestroy(mp);
@@ -96,17 +97,17 @@
     return root;
 }
 
-static int scfg_char_is_delim(char c) {
-    static const char *scfg_tokenizer_delim = "()/*%";
-    for(int i=0;i<sizeof(scfg_tokenizer_delim)-1;i++) {
-        if(c == scfg_tokenizer_delim[i]) {
+static int scfg_char_is_delim(ConfigParser2 *parser, char c) {
+    size_t len = strlen(parser->delim);
+    for(int i=0;i<len;i++) {
+        if(c == parser->delim[i]) {
             return 1;
         }
     }
     return 0;
 }
 
-static CFGToken get_next_token(cxstring content, int *pos) {
+static CFGToken get_next_token(ConfigParser2 *parser, cxstring content, int *pos) {
     CFGToken token = { {NULL, 0}, CFG_NO_TOKEN };
     CFGTokenType type = CFG_TOKEN;
     
@@ -164,7 +165,7 @@
             if(token_begin < 0) {
                 token_begin = i;
             }
-        } else if(scfg_char_is_delim(c)) {
+        } else if(scfg_char_is_delim(parser, c)) {
             if(token_begin >= 0) {
                 token_end = i;
                 i--;
@@ -248,6 +249,10 @@
     CxMempool *mp = parser->mp;
     CxAllocator *a = (CxAllocator*)mp->allocator;
     
+    if(!parser->delim) {
+        parser->delim = "";
+    }
+    
     // PARSE:
     // first non space/comment token is directive/object name
     // following tokens are arguments
@@ -271,7 +276,7 @@
     
     int text_start = 0;
     int err = 0;
-    while((token = get_next_token(content, &pos)).type != CFG_NO_TOKEN) {
+    while((token = get_next_token(parser, content, &pos)).type != CFG_NO_TOKEN) {
         //printf("%s [%.*s]\n", token_type_str(token.type), (int)token.content.length, token.content.ptr);
 
         switch(token.type) {
--- a/src/server/config/serverconfig.h	Sat Nov 12 11:52:47 2022 +0100
+++ b/src/server/config/serverconfig.h	Sat Nov 12 12:09:37 2022 +0100
@@ -121,6 +121,8 @@
     
     enum ConfigParserError error;
     
+    const char *delim;
+    
     /*
      * errno in case of IO errors (parser.error == CONFIG_PARSER_IO_ERROR)
      */

mercurial