#include "of_path.h"
#include <filesystem>

namespace fs = std::filesystem;
namespace ofen {
COfPath::COfPath()
{
}

COfPath::~COfPath()
{
}

bool COfPath::isSamePath(const ofString& pa, const ofString& pb)
{
    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