path_concat

This commit is contained in:
timerix 2022-10-28 11:57:43 +06:00
parent e2ecfc3d27
commit 5e35cb6721
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#include "filesystem.h"
char* __path_concat(uint16 n, char* prev_part, ...){
char** parts=(char**)malloc(n*sizeof(char*));
uint32* lengths=malloc(n*sizeof(uint32));
uint32 totalLength=0;
// reading args from va_list
va_list vl;
va_start(vl, prev_part);
for(uint16 i=0; i<n; i++){
char* part=va_arg(vl,char*);
int16 length=cptr_length(part);
parts[i]=part;
lengths[i]=length;
totalLength+=length;
}
va_end(vl);
// allocating memory for output value
char* totality=malloc(totalLength+1);
const char* output=totality;
totality[totalLength]=0;
// copying content of all strings to rezult
uint16 k=0;
for(; k<n-1; k++){
memcopy(parts[k], totality, lengths[k]);
totality+=lengths[k];
*totality=path_sep;
totality++;
}
memcopy(parts[k], totality, lengths[k]);
free(parts);
free(lengths);
return output;
}

View File

@ -0,0 +1,22 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "std.h"
#if defined(_WIN32) || defined (_WIN64)
static const char path_sep='\\';
#else
static const char path_sep='/';
#endif
char* __path_concat(uint16 n, char* path_start, ...);
#define path_concat(PATH_PARTS) __path_concat(count_args(PATH_PARTS), PATH_PARTS)
#if __cplusplus
}
#endif