libidav/utils.c

changeset 2
fbdfaacc4182
parent 1
b5bb7b3cd597
child 18
af411868ab9b
equal deleted inserted replaced
1:b5bb7b3cd597 2:fbdfaacc4182
211 } 211 }
212 return 0; 212 return 0;
213 } 213 }
214 214
215 int util_strtouint(const char *str, uint64_t *value) { 215 int util_strtouint(const char *str, uint64_t *value) {
216 if (str == NULL || *str == '\0') return 0;
216 char *end; 217 char *end;
217 errno = 0; 218 errno = 0;
218 uint64_t val = strtoull(str, &end, 0); 219 uint64_t val = strtoull(str, &end, 0);
219 if(errno == 0) { 220 if(errno == 0 && *end == '\0') {
220 *value = val; 221 *value = val;
221 return 1; 222 return 1;
222 } else { 223 } else {
223 return 0; 224 return 0;
224 } 225 }
225 } 226 }
226 227
227 int util_strtoint(const char *str, int64_t *value) { 228 int util_strtoint(const char *str, int64_t *value) {
229 if (str == NULL || *str == '\0') return 0;
228 char *end; 230 char *end;
229 errno = 0; 231 errno = 0;
230 int64_t val = strtoll(str, &end, 0); 232 int64_t val = strtoll(str, &end, 0);
231 if(errno == 0) { 233 if(errno == 0 && *end == '\0') {
232 *value = val; 234 *value = val;
233 return 1; 235 return 1;
234 } else { 236 } else {
235 return 0; 237 return 0;
236 } 238 }
237 } 239 }
238 240
239 int util_szstrtouint(const char *str, uint64_t *value) { 241 int util_szstrtouint(const char *str, uint64_t *value) {
242 if (str == NULL || *str == '\0') return 0;
240 char *end; 243 char *end;
241 errno = 0; 244 errno = 0;
242 size_t len = strlen(str); 245 size_t len = strlen(str);
243 uint64_t val = strtoull(str, &end, 0); 246 uint64_t val = strtoull(str, &end, 0);
244 if(end == str+len) { 247 if(errno != 0) {
248 return 0;
249 } if(end == str+len) {
245 *value = val; 250 *value = val;
246 return 1; 251 return 1;
247 } else if(end == str+len-1) { 252 } else if(end == str+len-1) {
248 uint64_t mul = 1; 253 uint64_t mul = 1;
249 switch(end[0]) { 254 switch(end[0]) {
279 *result = 0; 284 *result = 0;
280 return 1; 285 return 1;
281 } 286 }
282 } 287 }
283 288
284 char* util_url_base_s(cxstring url) { 289 cxstring util_url_base_s(cxstring url) {
285 size_t i = 0; 290 size_t i = 0;
286 if(url.length > 0) { 291 if(url.length > 0) {
287 int slmax; 292 int slmax;
288 if(cx_strprefix(url, cx_str("http://"))) { 293 if(cx_strprefix(url, cx_str("http://"))) {
289 slmax = 3; 294 slmax = 3;
301 break; 306 break;
302 } 307 }
303 } 308 }
304 } 309 }
305 } 310 }
306 cxstring server = cx_strsubsl(url, 0, i); 311 return cx_strsubsl(url, 0, i);
307 return cx_strdup(server).ptr; 312 }
308 } 313
309 314 char* util_url_base(const char *url) {
310 char* util_url_base(char *url) { 315 return cx_strdup(util_url_base_s(cx_str(url))).ptr;
311 return util_url_base_s(cx_str(url));
312 } 316 }
313 317
314 #ifdef _WIN32 318 #ifdef _WIN32
315 #define strncasecmp _strnicmp 319 #define strncasecmp _strnicmp
316 #endif 320 #endif
317 321
318 const char* util_url_path(const char *url) { 322 const char* util_url_path(const char *url) {
319 const char *path = NULL; 323 return util_url_path_s(cx_str(url)).ptr;
320 size_t len = strlen(url); 324 }
325
326 cxstring util_url_path_s(cxstring url) {
327 cxstring path = { "", 0 };
321 int slashcount = 0; 328 int slashcount = 0;
322 int slmax; 329 int slmax;
323 if(len > 7 && !strncasecmp(url, "http://", 7)) { 330 if(url.length > 7 && !strncasecmp(url.ptr, "http://", 7)) {
324 slmax = 3; 331 slmax = 3;
325 } else if(len > 8 && !strncasecmp(url, "https://", 8)) { 332 } else if(url.length > 8 && !strncasecmp(url.ptr, "https://", 8)) {
326 slmax = 3; 333 slmax = 3;
327 } else { 334 } else {
328 slmax = 1; 335 slmax = 1;
329 } 336 }
330 char c; 337 char c;
331 for(int i=0;i<len;i++) { 338 for(int i=0;i<url.length;i++) {
332 c = url[i]; 339 c = url.ptr[i];
333 if(c == '/') { 340 if(c == '/') {
334 slashcount++; 341 slashcount++;
335 if(slashcount == slmax) { 342 if(slashcount == slmax) {
336 path = url + i; 343 path = cx_strsubs(url, i);
337 break; 344 break;
338 } 345 }
339 } 346 }
340 }
341 if(!path) {
342 path = url + len; // empty string
343 } 347 }
344 return path; 348 return path;
345 } 349 }
346 350
347 char* util_url_decode(DavSession *sn, const char *url) { 351 char* util_url_decode(DavSession *sn, const char *url) {
615 return mkdir(path, mode); 619 return mkdir(path, mode);
616 #endif 620 #endif
617 } 621 }
618 622
619 char* util_concat_path(const char *url_base, const char *p) { 623 char* util_concat_path(const char *url_base, const char *p) {
620 cxstring base = cx_str((char*)url_base); 624 cxstring base = cx_str(url_base);
621 cxstring path; 625 cxstring path;
622 if(p) { 626 if(p) {
623 path = cx_str((char*)p); 627 path = cx_str((char*)p);
624 } else { 628 } else {
629 path = CX_STR("");
630 }
631
632 return util_concat_path_s(base, path).ptr;
633 }
634
635 cxmutstr util_concat_path_s(cxstring base, cxstring path) {
636 if(!path.ptr) {
625 path = CX_STR(""); 637 path = CX_STR("");
626 } 638 }
627 639
628 int add_separator = 0; 640 int add_separator = 0;
629 if(base.length != 0 && base.ptr[base.length-1] == '/') { 641 if(base.length != 0 && base.ptr[base.length-1] == '/') {
641 url = cx_strcat(3, base, CX_STR("/"), path); 653 url = cx_strcat(3, base, CX_STR("/"), path);
642 } else { 654 } else {
643 url = cx_strcat(2, base, path); 655 url = cx_strcat(2, base, path);
644 } 656 }
645 657
646 return url.ptr; 658 return url;
647 } 659 }
648 660
649 char* util_get_url(DavSession *sn, const char *href) { 661 char* util_get_url(DavSession *sn, const char *href) {
650 cxstring base = cx_str(sn->base_url); 662 cxstring base = cx_str(sn->base_url);
651 cxstring href_str = cx_str(href); 663 cxstring href_str = cx_str(href);

mercurial