470 sstr_t parent = sstr(path); |
470 sstr_t parent = sstr(path); |
471 sstr_t child = sstr(ch); |
471 sstr_t child = sstr(ch); |
472 sstr_t newstr; |
472 sstr_t newstr; |
473 sstr_t s; |
473 sstr_t s; |
474 |
474 |
475 s.length = 0; |
475 |
476 s.ptr = NULL; |
476 UcxAllocator a = util_pool_allocator(pool); |
477 newstr.length = parent.length + child.length; |
|
478 if(parent.ptr[parent.length - 1] != '/') { |
|
479 s = sstrn("/", 1); |
|
480 newstr.length++; |
|
481 } |
|
482 |
|
483 newstr.ptr = pool_malloc(pool, newstr.length + 1); |
|
484 if(!newstr.ptr) { |
|
485 // TODO: error |
|
486 newstr.length = 0; |
|
487 return newstr; |
|
488 } |
|
489 if(s.length == 1) { |
477 if(s.length == 1) { |
490 newstr = sstrncat(newstr, 3, parent, s, child); |
478 newstr = sstrcat_a(&a, 3, parent, s, child); |
491 } else { |
479 } else { |
492 newstr = sstrncat(newstr, 2, parent, child); |
480 newstr = sstrcat_a(&a, 2, parent, child); |
493 } |
481 } |
494 newstr.ptr[newstr.length] = '\0'; |
|
495 |
482 |
496 return newstr; |
483 return newstr; |
497 } |
484 } |
498 |
485 |
499 sstr_t util_path_remove_last(sstr_t path) { |
486 sstr_t util_path_remove_last(sstr_t path) { |
513 return path; |
500 return path; |
514 } |
501 } |
515 |
502 |
516 void util_add_ppath(sstr_t root, sstr_t path, pblock *vars) { |
503 void util_add_ppath(sstr_t root, sstr_t path, pblock *vars) { |
517 // concat path |
504 // concat path |
518 sstr_t translated_path; |
505 size_t length = root.length + path.length; |
519 translated_path.length = root.length + path.length; |
506 char *translated_path = alloca(length); |
520 translated_path.ptr = alloca(translated_path.length); |
507 |
521 |
508 memcpy(translated_path, root.ptr, root.length); |
522 translated_path = sstrncat(translated_path, 2, root, path); |
509 memcpy(translated_path + root.length, path.ptr, path.length); |
|
510 |
523 |
511 |
524 // add path to specified pblock |
512 // add path to specified pblock |
525 pblock_kvinsert( |
513 pblock_kvinsert( |
526 pb_key_ppath, |
514 pb_key_ppath, |
527 translated_path.ptr, |
515 translated_path, |
528 translated_path.length, |
516 length, |
529 vars); |
517 vars); |
|
518 } |
|
519 |
|
520 |
|
521 UcxAllocator util_pool_allocator(pool_handle_t *pool) { |
|
522 UcxAllocator a; |
|
523 a.malloc = (ucx_allocator_malloc)pool_malloc; |
|
524 a.calloc = (ucx_allocator_calloc)pool_calloc; |
|
525 a.realloc = (ucx_allocator_realloc)pool_realloc; |
|
526 a.free = (ucx_allocator_free)pool_free; |
|
527 a.pool = pool; |
|
528 return a; |
530 } |
529 } |
531 |
530 |
532 |
531 |
533 // new - code in parts from params.cpp |
532 // new - code in parts from params.cpp |
534 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) { |
533 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) { |