libidav/utils.c

changeset 2
fbdfaacc4182
parent 1
b5bb7b3cd597
child 18
af411868ab9b
--- a/libidav/utils.c	Mon Jan 22 17:27:47 2024 +0100
+++ b/libidav/utils.c	Sat Jan 27 17:50:19 2024 +0100
@@ -213,10 +213,11 @@
 }
 
 int util_strtouint(const char *str, uint64_t *value) {
+    if (str == NULL || *str == '\0') return 0;
     char *end;
     errno = 0;
     uint64_t val = strtoull(str, &end, 0);
-    if(errno == 0) {
+    if(errno == 0 && *end == '\0') {
         *value = val;
         return 1;
     } else {
@@ -225,10 +226,11 @@
 }
 
 int util_strtoint(const char *str, int64_t *value) {
+    if (str == NULL || *str == '\0') return 0;
     char *end;
     errno = 0;
     int64_t val = strtoll(str, &end, 0);
-    if(errno == 0) {
+    if(errno == 0 && *end == '\0') {
         *value = val;
         return 1;
     } else {
@@ -237,11 +239,14 @@
 }
 
 int util_szstrtouint(const char *str, uint64_t *value) {
+    if (str == NULL || *str == '\0') return 0;
     char *end;
     errno = 0;
     size_t len = strlen(str);
     uint64_t val = strtoull(str, &end, 0);
-    if(end == str+len) {
+    if(errno != 0) {
+        return 0;
+    } if(end == str+len) {
         *value = val;
         return 1;
     } else if(end == str+len-1) {
@@ -281,7 +286,7 @@
     }
 }
 
-char* util_url_base_s(cxstring url) {
+cxstring util_url_base_s(cxstring url) {
     size_t i = 0;
     if(url.length > 0) {
         int slmax;
@@ -303,12 +308,11 @@
             }
         }
     }
-    cxstring server = cx_strsubsl(url, 0, i);
-    return cx_strdup(server).ptr;
+    return cx_strsubsl(url, 0, i);
 }
 
-char* util_url_base(char *url) {
-    return util_url_base_s(cx_str(url));
+char* util_url_base(const char *url) {
+    return cx_strdup(util_url_base_s(cx_str(url))).ptr;
 }
 
 #ifdef _WIN32
@@ -316,30 +320,30 @@
 #endif
 
 const char* util_url_path(const char *url) {
-    const char *path = NULL;
-    size_t len = strlen(url);
+    return util_url_path_s(cx_str(url)).ptr;
+}
+
+cxstring util_url_path_s(cxstring url) {
+    cxstring path = { "", 0 };
     int slashcount = 0;
     int slmax;
-    if(len > 7 && !strncasecmp(url, "http://", 7)) {
+    if(url.length > 7 && !strncasecmp(url.ptr, "http://", 7)) {
         slmax = 3;
-    } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
+    } else if(url.length > 8 && !strncasecmp(url.ptr, "https://", 8)) {
         slmax = 3;
     } else {
         slmax = 1;
     }
     char c;
-    for(int i=0;i<len;i++) {
-        c = url[i];
+    for(int i=0;i<url.length;i++) {
+        c = url.ptr[i];
         if(c == '/') {
             slashcount++;
             if(slashcount == slmax) {
-                path = url + i;
+                path = cx_strsubs(url, i);
                 break;
             }
         }
-    } 
-    if(!path) {
-        path = url + len; // empty string
     }
     return path;
 }
@@ -617,7 +621,7 @@
 }
 
 char* util_concat_path(const char *url_base, const char *p) {
-    cxstring base = cx_str((char*)url_base);
+    cxstring base = cx_str(url_base);
     cxstring path;
     if(p) {
         path = cx_str((char*)p);
@@ -625,6 +629,14 @@
         path = CX_STR("");
     }
     
+    return util_concat_path_s(base, path).ptr;
+}
+
+cxmutstr util_concat_path_s(cxstring base, cxstring path) {
+    if(!path.ptr) {
+        path = CX_STR("");
+    }
+    
     int add_separator = 0;
     if(base.length != 0 && base.ptr[base.length-1] == '/') {
         if(path.ptr[0] == '/') {
@@ -643,7 +655,7 @@
         url = cx_strcat(2, base, path);
     }
     
-    return url.ptr;
+    return url;
 }
 
 char* util_get_url(DavSession *sn, const char *href) {

mercurial