dav/scfg.c

changeset 747
efbd59642577
parent 700
165811ea12ab
child 778
bf53db813cc9
--- a/dav/scfg.c	Sun Apr 16 14:12:24 2023 +0200
+++ b/dav/scfg.c	Fri Apr 21 21:25:32 2023 +0200
@@ -31,8 +31,10 @@
 #include <string.h>
 #include <errno.h>
 #include <libidav/utils.h>
-#include <ucx/map.h>
-#include <ucx/utils.h>
+#include <cx/hash_map.h>
+#include <cx/utils.h>
+#include <cx/linked_list.h>
+#include <cx/printf.h>
 
 #include "scfg.h"
 #include "config.h"
@@ -58,10 +60,10 @@
 #define ENV_HOME getenv("HOME")
 #endif /* _WIN32 */
 
-static UcxMap *directories;
+static CxMap *directories;
 
-UcxMapIterator scfg_directory_iterator() {
-    return ucx_map_iterator(directories);
+CxIterator scfg_directory_iterator() {
+    return cxMapIteratorValues(directories);
 }
 
 static int create_default_sync_config(char *file) {
@@ -79,24 +81,22 @@
     return 0;
 }
 
-static UcxList* add_regex_pattern(UcxList *list, char *value,
+static void add_regex_pattern(CxList *list, char *value,
         unsigned short xmlline) {
-    regex_t *regex = malloc(sizeof(regex_t));
-    if (regcomp(regex, value, REG_EXTENDED|REG_NOSUB)) {
+    regex_t regex;
+    if (regcomp(&regex, value, REG_EXTENDED|REG_NOSUB)) {
         print_warning(xmlline,
                 "Invalid regular expression (%s) ... skipped\n", value);
-        free(regex);
-        return list;
     } else {
-        return ucx_list_append(list, regex);
+        cxListAdd(list, &regex);
     }
 }
 
 static int scfg_load_filter(
         xmlNode *node,
-        UcxList **include,
-        UcxList **exclude,
-        UcxList **tags)
+        CxList *include,
+        CxList *exclude,
+        CxList *tags)
 {
     node = node->children;
     
@@ -105,11 +105,11 @@
             char *value = util_xml_get_text(node);
             if(xstreq(node->name, "include")) {
                 if(value) {
-                    *include = add_regex_pattern(*include, value, node->line);
+                    add_regex_pattern(include, value, node->line);
                 }
             } else if(xstreq(node->name, "exclude")) {
                 if(value) {
-                    *exclude = add_regex_pattern(*exclude, value, node->line);
+                    add_regex_pattern(exclude, value, node->line);
                 }
             } else if(xstreq(node->name, "tags")) {
                 if(value) {
@@ -143,7 +143,7 @@
                         }
                         xmlFree(scope);
                         
-                        *tags = ucx_list_append(*tags, tagfilter);
+                        cxListAdd(tags, tagfilter);
                     }
                 }
             } else {
@@ -165,11 +165,15 @@
 }
 
 Filter* parse_filter(xmlNode *node) {
-    UcxList *include = NULL;
-    UcxList *exclude = NULL;
-    UcxList *tags = NULL;
+    CxList *include = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(regex_t));
+    CxList *exclude = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(regex_t));
+    CxList *tags = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
     
-    if(scfg_load_filter(node, &include, &exclude, &tags)) {
+    include->simple_destructor = (cx_destructor_func)regfree;
+    exclude->simple_destructor = (cx_destructor_func)regfree;
+    // TODO: set tags destructor
+    
+    if(scfg_load_filter(node, include, exclude, tags)) {
         return NULL;
     }
     
@@ -181,10 +185,10 @@
 }
 
 void init_default_filter(Filter *filter) {
-    if(!filter->include) {
-        regex_t *matchall = malloc(sizeof(regex_t));
-        regcomp(matchall, ".*", REG_NOSUB);
-        filter->include = ucx_list_append(NULL, matchall);
+    if(filter->include->size == 0) {
+        regex_t matchall;
+        regcomp(&matchall, ".*", REG_NOSUB);
+        cxListAdd(filter->include, &matchall);
     }
     /*
     if(!filter->exclude) {
@@ -343,15 +347,15 @@
     return sc;    
 }
 
-static UcxList* parse_splitconfig(xmlNode *node, int *error) {
-    UcxList *splitconfig = NULL;
+static CxList* parse_splitconfig(xmlNode *node, int *error) {
+    CxList *splitconfig = cxLinkedListCreateSimple(CX_STORE_POINTERS);
     int err = 0;
     xmlNode *c = node->children;
     while(c) {
         if(c->type == XML_ELEMENT_NODE && xstreq(c->name, "split")) {
             SplitConfig *sc = parse_split(c);
             if(sc) {
-                splitconfig = ucx_list_append(splitconfig, sc);
+                cxListAdd(splitconfig, sc);
             } else {
                 err = 1;
                 break;
@@ -427,10 +431,10 @@
     char *database = NULL;
     TagConfig *tagconfig = NULL;
     Versioning *versioning = NULL;
-    UcxList *include = NULL;
-    UcxList *exclude = NULL;
-    UcxList *tagfilter = NULL;
-    UcxList *splitconfig = NULL;
+    CxList *include = cxLinkedListCreateSimple(sizeof(regex_t));
+    CxList *exclude = cxLinkedListCreateSimple(sizeof(regex_t));
+    CxList *tagfilter = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    CxList *splitconfig = NULL;
     int max_retry = 0;
     int allow_cmd = SYNC_CMD_PULL | SYNC_CMD_PUSH
                   | SYNC_CMD_ARCHIVE | SYNC_CMD_RESTORE;
@@ -470,7 +474,7 @@
             } else if(xstreq(node->name, "repository")) {
                 repository = value;
             } else if(xstreq(node->name, "filter")) {
-                if(scfg_load_filter(node, &include, &exclude, &tagfilter)) {
+                if(scfg_load_filter(node, include, exclude, tagfilter)) {
                     return 1;
                 }
             } else if(xstreq(node->name, "database")) {
@@ -647,7 +651,7 @@
     dir->filter.tags = tagfilter;
     init_default_filter(&dir->filter);
     
-    if (trash && sstrtrim(sstr(trash)).length > 0) {
+    if (trash && cx_strtrim(cx_str(trash)).length > 0) {
         if (trash[0] == '/' || trash[0] == '$') {
             dir->trash = scfg_create_path(trash);
         } else {
@@ -665,13 +669,13 @@
         dir->trash = NULL;
     }
     
-    ucx_map_cstr_put(directories, name, dir);
+    cxMapPut(directories, cx_hash_key_str(name), dir);
     
     return 0;
 }
 
 int load_sync_config() {
-    directories = ucx_map_new(8);
+    directories = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8);
     
     if(check_config_dir()) {
         fprintf(stderr, "Cannot create .dav directory\n");
@@ -724,8 +728,8 @@
     return ret;
 }
 
-SyncDirectory* scfg_get_dir(char *name) {
-    return ucx_map_cstr_get(directories, name);
+SyncDirectory* scfg_get_dir(const char *name) {
+    return cxMapGet(directories, cx_hash_key_str(name));
 }
 
 int scfg_check_dir(SyncDirectory *dir) {
@@ -756,7 +760,7 @@
     return 0;
 }
 
-char* scfg_create_path(char *cfg) {
+char* scfg_create_path(const char *cfg) {
     if(!cfg) {
         return NULL;
     }
@@ -764,15 +768,15 @@
         return strdup(cfg);
     }
     
-    sstr_t s = sstr(cfg);
-    sstr_t path = sstrchr(sstr(cfg), '/');
+    cxstring s = cx_str(cfg);
+    cxstring path = cx_strchr(cx_str(cfg), '/');
     char *localpath = NULL;
     if(path.length > 0) {
         // path = $var/path/
         
-        sstr_t var = sstrsubsl(s, 1, path.ptr - s.ptr - 1);
+        cxstring var = cx_strsubsl(s, 1, path.ptr - s.ptr - 1);
         if(var.length > 0) {
-            char *env = sstrdup(var).ptr;
+            char *env = cx_strdup(var).ptr;
             char *envval = getenv(env);
             free(env);
             if(envval) {
@@ -786,7 +790,7 @@
                 exit(-1);
             }
         } else {
-            localpath = sstrdup(path).ptr;
+            localpath = cx_strdup(path).ptr;
         }
     } else {
         // path = $var
@@ -852,20 +856,19 @@
     return ret;
 }
 
-char* generate_db_name(char *basename) {
+char* generate_db_name(const char *basename) {
     char *dbname = NULL;
     int count = -1;
     while(!dbname) {
-        sstr_t name = count < 0 ?
-                ucx_sprintf("%s-db.xml", basename) : 
-                ucx_sprintf("%s%d-db.xml", basename, count);
+        cxmutstr name = count < 0 ?
+                cx_asprintf("%s-db.xml", basename) : 
+                cx_asprintf("%s%d-db.xml", basename, count);
         count++;
         
-        UcxMapIterator i = ucx_map_iterator(directories);
-        SyncDirectory *dir;
+        CxIterator i = cxMapIteratorValues(directories);
         bool unique = true;
-        UCX_MAP_FOREACH(key, dir, i) {
-            if(!sstrcmp(name, sstr(dir->database))) {
+        cx_foreach(SyncDirectory *, dir, i) {
+            if(!cx_strcmp(cx_strcast(name), cx_str(dir->database))) {
                 unique = false;
                 break;
             }
@@ -879,23 +882,15 @@
 }
 
 void free_filter(Filter filter) {
-    UCX_FOREACH(elm, filter.include) {
-        regfree(elm->data);
-        free(elm->data);
-    }
-    ucx_list_free(filter.include);
-    UCX_FOREACH(elm, filter.exclude) {
-        regfree(elm->data);
-        free(elm->data);
-    }
-    ucx_list_free(filter.exclude);
+    cxListDestroy(filter.include);
+    cxListDestroy(filter.exclude);
+    cxListDestroy(filter.tags);
 }
 
 void free_sync_config() {
     if(directories) {
-        UcxMapIterator i = ucx_map_iterator(directories);
-        SyncDirectory *dir;
-        UCX_MAP_FOREACH(elm, dir, i) {
+        CxIterator i = cxMapIteratorValues(directories);
+        cx_foreach(SyncDirectory *, dir, i) {
             free(dir->name);
             free(dir->path);
             free(dir->repository);
@@ -913,6 +908,6 @@
             free(dir);
         }
 
-        ucx_map_free(directories);
+        cxMapDestroy(directories);
     }
 }

mercurial