path:路径添加统一化,添加字符串替换。

This commit is contained in:
taynpg 2024-11-29 10:39:00 +08:00
parent da08449531
commit 9c396fc579
2 changed files with 28 additions and 3 deletions

View File

@ -11,7 +11,9 @@ public:
~COfPath(); ~COfPath();
public: 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 }; // namespace ofen
#endif #endif

View File

@ -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 } // namespace ofen