libidav/utils.c

changeset 18
af411868ab9b
parent 2
fbdfaacc4182
child 20
db263186edf3
--- a/libidav/utils.c	Wed Jan 31 12:55:11 2024 +0100
+++ b/libidav/utils.c	Tue Feb 06 14:17:22 2024 +0100
@@ -612,6 +612,28 @@
     }
 }
 
+const char* util_resource_name_c(const char *url, char pathseparator) {
+    cxstring urlstr = cx_str(url);
+    if(urlstr.ptr[urlstr.length-1] == pathseparator) {
+        urlstr.length--;
+    }
+    cxstring resname = cx_strrchr(urlstr, pathseparator);
+    if(resname.length > 1) {
+        return resname.ptr+1;
+    } else {
+        return url;
+    }
+}
+
+const char* util_path_file_name(const char *url) {
+#ifdef _WIN32
+    return util_resource_name_c(url, '\\');
+#else
+    return util_resource_name_c(url, '/');
+#endif
+}
+
+
 int util_mkdir(char *path, mode_t mode) {
 #ifdef _WIN32
     return mkdir(path);
@@ -658,6 +680,40 @@
     return url;
 }
 
+cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) {
+    if(!path.ptr) {
+        path = CX_STR("");
+    }
+
+    int add_separator = 0;
+    if(base.length != 0 && base.ptr[base.length-1] == separator) {
+        if(path.ptr[0] == separator) {
+            base.length--;
+        }
+    } else {
+        if(path.length == 0 || path.ptr[0] != separator) {
+            add_separator = 1;
+        }
+    }
+
+    cxmutstr url;
+    if(add_separator) {
+        url = cx_strcat(3, base, cx_strn(&separator, 1), path);
+    } else {
+        url = cx_strcat(2, base, path);
+    }
+
+    return url;
+}
+
+cxmutstr util_concat_sys_path(cxstring base, cxstring path) {
+#ifdef _WIN32
+    return util_concat_path_ext(base, path, '\\');
+#else
+    return util_concat_path_ext(base, path, '/');
+#endif
+}
+
 char* util_get_url(DavSession *sn, const char *href) {
     cxstring base = cx_str(sn->base_url);
     cxstring href_str = cx_str(href);
@@ -716,6 +772,17 @@
     return parent;
 }
 
+char* util_sys_parent_path(const char *path) {
+    const char *name = util_path_file_name(path);
+    size_t namelen = strlen(name);
+    size_t pathlen = strlen(path);
+    size_t parentlen = pathlen - namelen;
+    char *parent = malloc(parentlen + 1);
+    memcpy(parent, path, parentlen);
+    parent[parentlen] = '\0';
+    return parent;
+}
+
 char* util_size_str(DavBool iscollection, uint64_t contentlength) {
     char *str = malloc(16);
     uint64_t size = contentlength;

mercurial