53 |
53 |
54 #include "../daemon/netsite.h" |
54 #include "../daemon/netsite.h" |
55 #include "../public/nsapi.h" |
55 #include "../public/nsapi.h" |
56 #include <ucx/string.h> |
56 #include <ucx/string.h> |
57 #include <ucx/mempool.h> |
57 #include <ucx/mempool.h> |
|
58 #include <ucx/utils.h> |
58 |
59 |
59 #include "pblock.h" |
60 #include "pblock.h" |
60 #include "util.h" |
61 #include "util.h" |
61 |
62 |
62 #include <openssl/bio.h> |
63 #include <openssl/bio.h> |
286 va_list args; |
287 va_list args; |
287 va_start(args, fmt); |
288 va_start(args, fmt); |
288 return vsnprintf(s, n, fmt, args); |
289 return vsnprintf(s, n, fmt, args); |
289 } |
290 } |
290 |
291 |
|
292 NSAPI_PUBLIC int util_vasprintf(pool_handle_t *pool, char **s, const char *fmt, |
|
293 va_list args) |
|
294 { |
|
295 UcxAllocator a = util_pool_allocator(pool); |
|
296 va_list ap; |
|
297 va_copy(ap, args); |
|
298 sstr_t str = ucx_vasprintf(&a, fmt, ap); |
|
299 *s = str.ptr; |
|
300 return str.length; |
|
301 } |
|
302 |
|
303 NSAPI_PUBLIC int util_asprintf(pool_handle_t *pool, char **s, const char *fmt, ...) |
|
304 { |
|
305 va_list args; |
|
306 va_start(args, fmt); |
|
307 return util_vasprintf(pool, s, fmt, args); |
|
308 } |
|
309 |
291 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args) |
310 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args) |
292 { |
311 { |
293 return vsprintf(s, fmt, args); |
312 return vsprintf(s, fmt, args); |
294 } |
313 } |
295 |
314 |
417 } else { |
436 } else { |
418 return 0; |
437 return 0; |
419 } |
438 } |
420 } |
439 } |
421 |
440 |
|
441 NSAPI_PUBLIC const char* util_resource_name(const char *url) { |
|
442 scstr_t urlstr = scstr(url); |
|
443 if(urlstr.ptr[urlstr.length-1] == '/') { |
|
444 urlstr.length--; |
|
445 } |
|
446 scstr_t resname = scstrrchr(urlstr, '/'); |
|
447 if(resname.length > 1) { |
|
448 return resname.ptr+1; |
|
449 } else { |
|
450 return url; |
|
451 } |
|
452 } |
|
453 |
|
454 NSAPI_PUBLIC char* util_parent_path(const char *path) { |
|
455 char *name = (char*)util_resource_name((char*)path); |
|
456 size_t namelen = strlen(name); |
|
457 size_t pathlen = strlen(path); |
|
458 size_t parentlen = pathlen - namelen; |
|
459 char *parent = MALLOC(parentlen + 1); |
|
460 memcpy(parent, path, parentlen); |
|
461 parent[parentlen] = '\0'; |
|
462 return parent; |
|
463 } |
|
464 |
|
465 NSAPI_PUBLIC char* util_parent_path_pool(pool_handle_t *pool, const char *path) { |
|
466 // maybe we can unify this function with util_parent_path |
|
467 char *name = (char*)util_resource_name((char*)path); |
|
468 size_t namelen = strlen(name); |
|
469 size_t pathlen = strlen(path); |
|
470 size_t parentlen = pathlen - namelen; |
|
471 char *parent = pool_malloc(pool, parentlen + 1); |
|
472 memcpy(parent, path, parentlen); |
|
473 parent[parentlen] = '\0'; |
|
474 return parent; |
|
475 } |
422 |
476 |
423 /* ------------------------------ util_itoa ------------------------------- */ |
477 /* ------------------------------ util_itoa ------------------------------- */ |
424 /* |
478 /* |
425 NSAPI_PUBLIC int util_itoa(int i, char *a) |
479 NSAPI_PUBLIC int util_itoa(int i, char *a) |
426 { |
480 { |