2024-11-14 16:04:54 +08:00
|
|
|
#include "of_path.h"
|
|
|
|
#include <filesystem>
|
|
|
|
|
2024-11-29 10:53:51 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(__clang__) && defined(__APPLE__)
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 256
|
|
|
|
#endif
|
|
|
|
|
2024-11-14 16:04:54 +08:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace ofen {
|
|
|
|
COfPath::COfPath()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
COfPath::~COfPath()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-29 10:39:00 +08:00
|
|
|
bool COfPath::isSamePath(const ofString& pa, const ofString& pb)
|
2024-11-14 16:04:54 +08:00
|
|
|
{
|
2024-11-29 10:39:00 +08:00
|
|
|
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;
|
2024-11-14 16:04:54 +08:00
|
|
|
}
|
2024-11-29 10:53:51 +08:00
|
|
|
ofString COfPath::getFullRunPath()
|
|
|
|
{
|
|
|
|
ofString path;
|
|
|
|
#ifdef _WIN32
|
|
|
|
ofChar buffer[MAX_PATH];
|
|
|
|
DWORD length = GetModuleFileName(NULL, buffer, MAX_PATH);
|
|
|
|
if (length == 0) {
|
|
|
|
return ofT("");
|
|
|
|
}
|
|
|
|
return ofString(buffer, length);
|
|
|
|
#elif defined(__clang__) && defined(__APPLE__)
|
|
|
|
uint32_t size = 0;
|
|
|
|
_NSGetExecutablePath(NULL, &size); // 获取路径缓冲区的大小
|
|
|
|
std::vector<ofChar> buffer(size); // 创建缓冲区
|
|
|
|
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
|
|
|
|
return ofT("");
|
|
|
|
}
|
|
|
|
return ofString(buffer.data());
|
|
|
|
#else
|
|
|
|
ofChar buffer[PATH_MAX];
|
|
|
|
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
|
|
|
if (len == -1) {
|
|
|
|
return ofT("");
|
|
|
|
}
|
|
|
|
buffer[len] = ofT('\0'); // 确保字符串以null终止
|
|
|
|
return ofString(buffer);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ofString COfPath::getHome()
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
ofChar* value = nullptr;
|
|
|
|
std::size_t len = 0;
|
|
|
|
auto err = _dupenv_s(&value, &len, "USERPROFILE");
|
|
|
|
if (err == 0 && value != nullptr) {
|
|
|
|
ofString ret(value);
|
|
|
|
free(value);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ofT("");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ofChar *homedir = getenv("HOME");
|
|
|
|
if (homedir) {
|
|
|
|
return ofString(homedir);
|
|
|
|
}
|
|
|
|
return ofT("");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-11-14 16:04:54 +08:00
|
|
|
} // namespace ofen
|