Add path normalization function.

This commit is contained in:
Bartosz Taudul 2022-09-02 01:23:14 +02:00
parent 165cc22115
commit 8cc43284bd
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -699,6 +699,62 @@ static void InitKernelSymbols()
}
#endif
char* NormalizePath( const char* path )
{
if( path[0] != '/' ) return nullptr;
const char* ptr = path;
const char* end = path;
while( *end ) end++;
char* res = (char*)tracy_malloc( end - ptr + 1 );
size_t rsz = 0;
while( ptr < end )
{
const char* next = ptr;
while( next < end && *next != '/' ) next++;
size_t lsz = next - ptr;
switch( lsz )
{
case 2:
if( memcmp( ptr, "..", 2 ) == 0 )
{
const char* back = res + rsz - 1;
while( back > res && *back != '/' ) back--;
rsz = back - res;
ptr = next + 1;
continue;
}
break;
case 1:
if( *ptr == '.' )
{
ptr = next + 1;
continue;
}
break;
case 0:
ptr = next + 1;
continue;
}
if( rsz != 1 ) res[rsz++] = '/';
memcpy( res+rsz, ptr, lsz );
rsz += lsz;
ptr = next + 1;
}
if( rsz == 0 )
{
memcpy( res, "/", 2 );
}
else
{
res[rsz] = '\0';
}
return res;
}
void InitCallstackCritical()
{
}