dav/main.c

changeset 723
5ca174b3247a
parent 720
2b8a65ed6f4c
child 726
8f6ad495f538
--- a/dav/main.c	Sun Jul 05 11:55:54 2020 +0200
+++ b/dav/main.c	Sat Jul 25 11:16:31 2020 +0200
@@ -715,94 +715,6 @@
     return ret;
 }
 
-static char* ls_date_str(time_t tm) {
-    struct tm t;
-    struct tm n;
-    time_t now = time(NULL);
-#ifdef _WIN32
-    memcpy(&t, localtime(&tm), sizeof(struct tm));
-    memcpy(&n, localtime(&now), sizeof(struct tm));
-#else
-    localtime_r(&tm, &t);
-    localtime_r(&now, &n);
-#endif /* _WIN32 */
-    char *str = malloc(16);
-    if(t.tm_year == n.tm_year) {
-        strftime(str, 16, "%b %d %H:%M", &t);
-    } else {
-        strftime(str, 16, "%b %d  %Y", &t);
-    }
-    return str;
-}
-
-static char* ls_size_str(DavResource *res) {
-    char *str = malloc(16);
-    uint64_t size = res->contentlength;
-    
-    if(res->iscollection) {
-        str[0] = '\0'; // currently no information for collections
-    } else if(size < 0x400) {
-        snprintf(str, 16, "%" PRIu64 " bytes", size);
-    } else if(size < 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) {
-            // size < 10 KiB
-            snprintf(str, 16, "%.1f KiB", s);
-        } else {
-            snprintf(str, 16, "%.0f KiB", s);
-        }
-    } else if(size < 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) {
-            // size < 10 MiB
-            snprintf(str, 16, "%.1f MiB", s);
-        } else {
-            size /= 0x100000;
-            snprintf(str, 16, "%.0f MiB", s);
-        }
-    } else if(size < 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) {
-            // size < 10 GiB
-            snprintf(str, 16, "%.1f GiB", s);
-        } else {
-            size /= 0x40000000;
-            snprintf(str, 16, "%.0f GiB", s);
-        }
-    } else {
-        size /= 1024;
-        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) {
-            // size < 10 TiB
-            snprintf(str, 16, "%.1f TiB", s);
-        } else {
-            size /= 0x40000000;
-            snprintf(str, 16, "%.0f TiB", s);
-        }
-    }
-    return str;
-}
-
 static char* ls_name(char *parent, char *path, int *len) {
     if(parent) {
         path += strlen(parent);
@@ -866,8 +778,8 @@
         type = "";
     }
     
-    char *date = ls_date_str(res->lastmodified);
-    char *size = ls_size_str(res);
+    char *date = util_date_str(res->lastmodified);
+    char *size = util_size_str(res->iscollection, res->contentlength);
     int namelen = strlen(res->name);
     char *name = recursive ? ls_name(parent, res->path, &namelen) : res->name;
     
@@ -2431,7 +2343,7 @@
             printf("size: %d\n", count_children(res));
         } else {
             printf("type: resource\n");
-            char *len = ls_size_str(res);
+            char *len = util_size_str(res->iscollection, res->contentlength);
             printf("size: %s\n", len);
             free(len);
         }

mercurial