dav/scfg.c

changeset 46
0542668d0f26
child 47
fbbbeed4ba8f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dav/scfg.c	Fri Jun 13 13:52:59 2014 +0200
@@ -0,0 +1,151 @@
+/*
+ * 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 <libidav/utils.h>
+#include <ucx/map.h>
+
+#include "scfg.h"
+
+#define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
+
+#ifdef _WIN32
+#define ENV_HOME getenv("USERPROFILE")
+#else
+#define ENV_HOME getenv("HOME")
+#endif /* _WIN32 */
+
+static UcxMap *directories;
+
+int load_sync_config() {
+    char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
+    xmlDoc *doc = xmlReadFile(file, NULL, 0);
+    if(!doc) {
+        fprintf(stderr, "Missing configuration file\n");
+        scfg_print_example();
+        return -1;
+    }
+    
+    directories = ucx_map_new(8);
+    
+    int ret = 0;
+    xmlNode *node = xmlDocGetRootElement(doc)->children;
+    while(node) {
+        if(node->type == XML_ELEMENT_NODE) {
+            if(xstreq(node->name, "directory")) {
+                if(scfg_load_directory(node)) {
+                    ret = 1;
+                    ucx_map_free(directories);
+                    break;
+                }
+            }
+        }
+        node = node->next;
+    }
+    
+    xmlFreeDoc(doc);
+    
+    return ret;
+}
+
+int scfg_load_directory(xmlNode *node) {
+    char *name = NULL;
+    char *path = NULL;
+    char *repository = NULL;
+    char *database = NULL;
+    
+    node = node->children;
+    while(node) {
+        if(node->type == XML_ELEMENT_NODE) {
+            char *value = util_xml_get_text(node);
+            if(!value) {
+                // next
+            } else if(xstreq(node->name, "name")) {
+                name = value;
+            } else if(xstreq(node->name, "path")) {
+                path = value;
+            } else if(xstreq(node->name, "repository")) {
+                repository = value;
+            } else if(xstreq(node->name, "database")) {
+                database = value;
+            }
+        }
+        node = node->next;
+    }
+    
+    if(!name) {
+        fprintf(stderr, "Missing name element for directory\n");
+        return -1;
+    }
+    if(!path) {
+        fprintf(stderr, "Missing path element for directory\n");
+        return -1;
+    }
+    if(!repository) {
+        fprintf(stderr, "Missing repository element for directory\n");
+        return -1;
+    }
+    if(!database) {
+        fprintf(stderr, "Missing database element for directory\n");
+        return -1;
+    }
+    
+    SyncDirectory *dir = malloc(sizeof(SyncDirectory));
+    dir->name = strdup(name);
+    dir->path = strdup(path);
+    dir->repository = strdup(repository);
+    dir->database = strdup(database);
+    
+    ucx_map_cstr_put(directories, name, dir);
+    
+    return 0;
+}
+
+void scfg_print_example() {
+    fprintf(stderr, "example sync.xml\n\n");
+    fprintf(stderr, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
+    fprintf(stderr, "<configuration>\n");
+    fprintf(stderr, "  <directory>\n");
+    fprintf(stderr, "    <!-- identifier -->\n");
+    fprintf(stderr, "    <name>documents</name>\n\n");
+    fprintf(stderr, "    <!-- local path to the directory -->\n");
+    fprintf(stderr, "    <path>/home/user/Documents</path>\n\n");
+    fprintf(stderr, "    <!-- repository name specified in config.xml -->\n");
+    fprintf(stderr, "    <repository>server</repository>\n\n");
+    fprintf(stderr, "    <!-- database file name -->\n");
+    fprintf(stderr, "    <database>documents.db</database>\n");
+    fprintf(stderr, "  </directory>\n");
+    fprintf(stderr, "</configuration>\n");
+}
+
+SyncDirectory* scfg_get_dir(char *name) {
+    return ucx_map_cstr_get(directories, name);
+}

mercurial