From 9c396fc579d28d0069cbd36bd31406c00ff0426d Mon Sep 17 00:00:00 2001 From: taynpg Date: Fri, 29 Nov 2024 10:39:00 +0800 Subject: [PATCH] =?UTF-8?q?path=EF=BC=9A=E8=B7=AF=E5=BE=84=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BB=9F=E4=B8=80=E5=8C=96=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9B=BF=E6=8D=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/of_path.h | 4 +++- src/of_path.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/of_path.h b/include/of_path.h index ed8fdd1..e7897ef 100644 --- a/include/of_path.h +++ b/include/of_path.h @@ -11,7 +11,9 @@ public: ~COfPath(); public: - static bool is_same_path(const ofString& pa, const ofString& pb); + static bool isSamePath(const ofString& pa, const ofString& pb); + static ofString normalizePath(const ofString& path); + static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to); }; }; // namespace ofen #endif \ No newline at end of file diff --git a/src/of_path.cpp b/src/of_path.cpp index bec68e4..0da8c2e 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -11,8 +11,31 @@ COfPath::~COfPath() { } -bool COfPath::is_same_path(const ofString& pa, const ofString& pb) +bool COfPath::isSamePath(const ofString& pa, const ofString& pb) { - return false; + return normalizePath(pa) == normalizePath(pb); +} + +ofString COfPath::normalizePath(const ofString& path) +{ + ofString normalized = replaceStr(path, ofT("\\"), ofT("/")); + if (!normalized.empty() && normalized.back() == ofT('/')) { + normalized.pop_back(); + } + return normalized; +} + +ofString COfPath::replaceStr(const ofString& str, const ofString& from, const ofString& to) +{ + if (from.empty()) { + return str; + } + ofString result = str; + size_t startPos = 0; + while ((startPos = result.find(from, startPos)) != ofString::npos) { + result.replace(startPos, from.length(), to); + startPos += to.length(); + } + return result; } } // namespace ofen