From dbb18e586d5303b73906e3d9eeeb9d591db316e2 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 10 Jul 2023 19:16:12 +0200 Subject: [PATCH] Add 'V_ComparePath' Add function to compare paths for equality, while ignoring case and path separators. --- r5dev/public/tier1/strtools.h | 2 ++ r5dev/tier1/strtools.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/r5dev/public/tier1/strtools.h b/r5dev/public/tier1/strtools.h index e1b09264..15aa9233 100644 --- a/r5dev/public/tier1/strtools.h +++ b/r5dev/public/tier1/strtools.h @@ -82,6 +82,8 @@ typedef enum // String matching using wildcards (*) for partial matches. bool V_StringMatchesPattern(const char* szString, const char* szPattern, int flags = 0); +bool V_ComparePath(const char* a, const char* b); + void V_FixSlashes(char* pname, char separator = CORRECT_PATH_SEPARATOR); // Adds a path separator to the end of the string if there isn't one already and the string is not empty. diff --git a/r5dev/tier1/strtools.cpp b/r5dev/tier1/strtools.cpp index 6c0cbfd0..44f6fa1a 100644 --- a/r5dev/tier1/strtools.cpp +++ b/r5dev/tier1/strtools.cpp @@ -395,6 +395,40 @@ bool V_StringMatchesPattern(const char* pszSource, const char* pszPattern, int n } } +//----------------------------------------------------------------------------- +// Purpose: Compares file paths, ignores case and path separators +// Input : *a - +// *b - +// Output : true if equal, false otherwise +//----------------------------------------------------------------------------- +bool V_ComparePath(const char* a, const char* b) +{ + if (strlen(a) != strlen(b)) + { + return false; + } + + // Case and separator invariant + for (; *a; a++, b++) + { + if (*a == *b) + { + continue; + } + if (tolower_fast(*a) == tolower_fast(*b)) + { + continue; + } + if ((*a == '/' || *a == '\\') && + (*b == '/' || *b == '\\')) + { + continue; + } + return false; + } + return true; +} + //----------------------------------------------------------------------------- // Purpose: Changes all '/' or '\' characters into separator // Input : *pName -