refactoring
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#include "filesystem.h"
|
||||
|
||||
char* __path_concat(u32 n, ...){
|
||||
char** parts=(char**)malloc(n*sizeof(char*));
|
||||
u32* lengths=malloc(n*sizeof(u32));
|
||||
char* __path_concat(allocator_ptr al, u32 n, ...){
|
||||
char** parts=(char**)allocator_alloc(al, n*sizeof(char*));
|
||||
u32* lengths=allocator_alloc(al, n*sizeof(u32));
|
||||
u32 totalLength=0;
|
||||
|
||||
// reading args from va_list
|
||||
@@ -18,7 +18,7 @@ char* __path_concat(u32 n, ...){
|
||||
va_end(vl);
|
||||
|
||||
// allocating memory for output value
|
||||
char* totality=malloc(totalLength+1);
|
||||
char* totality=allocator_alloc(al, totalLength+1);
|
||||
const char* output=totality;
|
||||
totality[totalLength]=0;
|
||||
|
||||
@@ -32,13 +32,13 @@ char* __path_concat(u32 n, ...){
|
||||
}
|
||||
memcpy(totality, parts[k], lengths[k]);
|
||||
|
||||
free(parts);
|
||||
free(lengths);
|
||||
allocator_free(al, lengths);
|
||||
allocator_free(al, parts);
|
||||
return output;
|
||||
}
|
||||
|
||||
char* path_fixSeparators(const char* path){
|
||||
char* pathCopy=cptr_copy(path);
|
||||
char* path_fixSeparators(allocator_ptr al, const char* path){
|
||||
char* pathCopy=cptr_copy(al, path);
|
||||
char c;
|
||||
while((c=*pathCopy)){
|
||||
if(c==path_notSep)
|
||||
@@ -49,13 +49,16 @@ char* path_fixSeparators(const char* path){
|
||||
}
|
||||
|
||||
Maybe path_throwIfEscapes(const char* path){
|
||||
LinearAllocator _al; LinearAllocator_construct(&_al, 128);
|
||||
allocator_ptr al=&_al.base;
|
||||
if(cptr_contains(path,".."))
|
||||
safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),);
|
||||
safethrow(cptr_concat(al, "path <",path,"> uses <..>, that's not allowed"),);
|
||||
LinearAllocator_destruct(&_al);
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
char* path_parentDir(char* dir){
|
||||
char* copy=cptr_copy(dir);
|
||||
char* path_parentDir(allocator_ptr al, char* dir){
|
||||
char* copy=cptr_copy(al, dir);
|
||||
i32 length=cptr_length(copy);
|
||||
i32 i=cptr_lastIndexOfChar(copy,path_sep);
|
||||
if(i!=-1 && i==length-1){
|
||||
@@ -63,8 +66,8 @@ char* path_parentDir(char* dir){
|
||||
i=cptr_lastIndexOfChar(copy,path_sep);
|
||||
}
|
||||
if(i==-1){
|
||||
free(copy);
|
||||
copy=malloc(2);
|
||||
allocator_free(al, copy);
|
||||
copy=allocator_alloc(al, 2);
|
||||
copy[0]='.';
|
||||
copy[1]=0;
|
||||
}
|
||||
@@ -72,7 +75,7 @@ char* path_parentDir(char* dir){
|
||||
}
|
||||
|
||||
|
||||
char* path_basename(char* path, bool with_extension){
|
||||
char* path_basename(allocator_ptr al, char* path, bool with_extension){
|
||||
i32 nameIndex=cptr_lastIndexOfChar(path, path_sep)+1;
|
||||
string rezult=string_fromCptr(path+nameIndex);
|
||||
if(!with_extension){
|
||||
|
||||
Reference in New Issue
Block a user