From 8cc43284bd3518981adb2132d119bfc702b64a9c Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 2 Sep 2022 01:23:14 +0200 Subject: [PATCH] Add path normalization function. --- public/client/TracyCallstack.cpp | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/public/client/TracyCallstack.cpp b/public/client/TracyCallstack.cpp index a8d9b6f5..3a96fe5e 100644 --- a/public/client/TracyCallstack.cpp +++ b/public/client/TracyCallstack.cpp @@ -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() { }