add util_size_str2() func, a variation of util_size_str with more parameters

Wed, 07 Feb 2024 17:12:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 07 Feb 2024 17:12:59 +0100
changeset 808
f21be698def9
parent 807
b41630ecc481
child 809
8e6d8f0327cf

add util_size_str2() func, a variation of util_size_str with more parameters

libidav/utils.c file | annotate | diff | comparison | revisions
libidav/utils.h file | annotate | diff | comparison | revisions
--- a/libidav/utils.c	Wed Feb 07 17:11:55 2024 +0100
+++ b/libidav/utils.c	Wed Feb 07 17:12:59 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,51 +772,66 @@
     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) {
+    return util_size_str2(iscollection, contentlength, contentlength, 1);
+}
+
+char* util_size_str2(DavBool iscollection, uint64_t contentlength, uint64_t dimension, int precision) {
     char *str = malloc(16);
     uint64_t size = contentlength;
-    
+
     if(iscollection) {
         str[0] = '\0'; // currently no information for collections
-    } else if(size < 0x400) {
+    } else if(dimension < 0x400) {
         snprintf(str, 16, "%" PRIu64 " bytes", size);
-    } else if(size < 0x100000) {
+    } else if(dimension < 0x100000) {
         float s = (float)size/0x400;
         int diff = (s*100 - (int)s*100);
         if(diff > 90) {
             diff = 0;
             s += 0.10f;
         }
-        if(size < 0x2800 && diff != 0) {
+        if(dimension < 0x2800 && diff != 0) {
             // size < 10 KiB
-            snprintf(str, 16, "%.1f KiB", s);
+            snprintf(str, 16, "%.*f KiB", precision, s);
         } else {
             snprintf(str, 16, "%.0f KiB", s);
         }
-    } else if(size < 0x40000000) {
+    } else if(dimension < 0x40000000) {
         float s = (float)size/0x100000;
         int diff = (s*100 - (int)s*100);
         if(diff > 90) {
             diff = 0;
             s += 0.10f;
         }
-        if(size < 0xa00000 && diff != 0) {
+        if(dimension < 0xa00000 && diff != 0) {
             // size < 10 MiB
-            snprintf(str, 16, "%.1f MiB", s);
+            snprintf(str, 16, "%.*f MiB", precision, s);
         } else {
             size /= 0x100000;
             snprintf(str, 16, "%.0f MiB", s);
         }
-    } else if(size < 0x1000000000ULL) {
+    } else if(dimension < 0x1000000000ULL) {
         float s = (float)size/0x40000000;
         int diff = (s*100 - (int)s*100);
         if(diff > 90) {
             diff = 0;
             s += 0.10f;
         }
-        if(size < 0x280000000 && diff != 0) {
+        if(dimension < 0x280000000 && diff != 0) {
             // size < 10 GiB
-            snprintf(str, 16, "%.1f GiB", s);
+            snprintf(str, 16, "%.*f GiB", precision, s);
         } else {
             size /= 0x40000000;
             snprintf(str, 16, "%.0f GiB", s);
@@ -773,9 +844,9 @@
             diff = 0;
             s += 0.10f;
         }
-        if(size < 0x280000000 && diff != 0) {
+        if(dimension < 0x280000000 && diff != 0) {
             // size < 10 TiB
-            snprintf(str, 16, "%.1f TiB", s);
+            snprintf(str, 16, "%.*f TiB", precision, s);
         } else {
             size /= 0x40000000;
             snprintf(str, 16, "%.0f TiB", s);
--- a/libidav/utils.h	Wed Feb 07 17:11:55 2024 +0100
+++ b/libidav/utils.h	Wed Feb 07 17:12:59 2024 +0100
@@ -78,8 +78,13 @@
 cxstring util_url_path_s(cxstring url);
 char* util_url_decode(DavSession *sn, const char *url);
 const char* util_resource_name(const char *url);
+const char* util_resource_name_c(const char *url, char pathseparator);
+const char* util_path_file_name(const char *url);
+
 char* util_concat_path(const char *url_base, const char *path);
 cxmutstr util_concat_path_s(cxstring url_base, cxstring path);
+cxmutstr util_concat_path_ext(cxstring url_base, cxstring path, char separator);
+cxmutstr util_concat_sys_path(cxstring base, cxstring path);
 char* util_get_url(DavSession *sn, const char *href);
 void util_set_url(DavSession *sn, const char *href);
 
@@ -97,8 +102,10 @@
 
 char* util_path_to_url(DavSession *sn, const char *path);
 char* util_parent_path(const char *path);
+char* util_sys_parent_path(const char *path);
 
 char* util_size_str(DavBool iscollection, uint64_t contentlength);
+char* util_size_str2(DavBool iscollection, uint64_t contentlength, uint64_t dimension, int precision);
 char* util_date_str(time_t tm);
 
 int util_getboolean(const char *v);

mercurial