[path] use new mem_scratch_begin_next() API in path functions, to handle the case where passed-in result arena is itself a scratch arena

This commit is contained in:
Martin Fouilleul 2023-05-24 20:36:05 +02:00
parent ba125ae860
commit debffac5c0
2 changed files with 27 additions and 18 deletions

View File

@ -26,32 +26,38 @@ str8 path_find_executable(mem_arena* arena)
str8 path_find_resource(mem_arena* arena, str8 relPath) str8 path_find_resource(mem_arena* arena, str8 relPath)
{ {
str8_list list = {}; str8_list list = {};
mem_arena* scratch = mem_scratch(); mem_arena_scope scratch = mem_scratch_begin_next(arena);
str8 executablePath = path_find_executable(scratch); str8 executablePath = path_find_executable(scratch.arena);
str8 dirPath = path_slice_directory(executablePath); str8 dirPath = path_slice_directory(executablePath);
str8_list_push(scratch, &list, dirPath); str8_list_push(scratch.arena, &list, dirPath);
str8_list_push(scratch, &list, STR8("/")); str8_list_push(scratch.arena, &list, STR8("/"));
str8_list_push(scratch, &list, relPath); str8_list_push(scratch.arena, &list, relPath);
str8 path = str8_list_join(scratch, list); str8 path = str8_list_join(scratch.arena, list);
char* pathCString = str8_to_cstring(scratch, path); char* pathCString = str8_to_cstring(scratch.arena, path);
char* buffer = mem_arena_alloc_array(scratch, char, path.len+1);
char* real = realpath(pathCString, buffer); char* real = realpath(pathCString, 0);
str8 result = str8_push_cstring(arena, real); str8 result = str8_push_cstring(arena, real);
free(real);
mem_scratch_end(scratch);
return(result); return(result);
} }
str8 path_find_canonical(mem_arena* arena, str8 path) str8 path_find_canonical(mem_arena* arena, str8 path)
{ {
mem_arena* scratch = mem_scratch(); mem_arena_scope scratch = mem_scratch_begin_next(arena);
char* pathCString = str8_to_cstring(scratch, path); char* pathCString = str8_to_cstring(scratch.arena, path);
char* real = realpath(pathCString, 0); char* real = realpath(pathCString, 0);
str8 result = str8_push_cstring(arena, real); str8 result = str8_push_cstring(arena, real);
free(real); free(real);
mem_scratch_end(scratch);
return(result); return(result);
} }

View File

@ -43,10 +43,11 @@ str8 path_slice_filename(str8 fullPath)
str8_list path_split(mem_arena* arena, str8 path) str8_list path_split(mem_arena* arena, str8 path)
{ {
//TODO: use secondary scratch arena mem_arena_scope tmp = mem_scratch_begin_next(arena);
str8_list split = {0}; str8_list split = {0};
str8_list_push(arena, &split, STR8("/")); str8_list_push(tmp.arena, &split, STR8("/"));
str8_list res = str8_split(arena, path, split); str8_list res = str8_split(arena, path, split);
mem_scratch_end(tmp);
return(res); return(res);
} }
@ -58,8 +59,6 @@ str8 path_join(mem_arena* arena, str8_list elements)
str8 path_append(mem_arena* arena, str8 parent, str8 relPath) str8 path_append(mem_arena* arena, str8 parent, str8 relPath)
{ {
//TODO: use secondary scratch arena
str8 result = {0}; str8 result = {0};
if(parent.len == 0) if(parent.len == 0)
@ -72,16 +71,20 @@ str8 path_append(mem_arena* arena, str8 parent, str8 relPath)
} }
else else
{ {
mem_arena_scope tmp = mem_scratch_begin_next(arena);
str8_list list = {0}; str8_list list = {0};
str8_list_push(arena, &list, parent); str8_list_push(tmp.arena, &list, parent);
if( (parent.ptr[parent.len-1] != '/') if( (parent.ptr[parent.len-1] != '/')
&&(relPath.ptr[relPath.len-1] != '/')) &&(relPath.ptr[relPath.len-1] != '/'))
{ {
str8_list_push(arena, &list, STR8("/")); str8_list_push(tmp.arena, &list, STR8("/"));
} }
str8_list_push(arena, &list, relPath); str8_list_push(tmp.arena, &list, relPath);
result = str8_list_join(arena, list); result = str8_list_join(arena, list);
mem_scratch_end(tmp);
} }
return(result); return(result);
} }