path:添加home和exe路径获取。

This commit is contained in:
taynpg 2024-11-29 10:53:51 +08:00
parent f8adaf2386
commit 837a4c8dd6
6 changed files with 73 additions and 2 deletions

View File

@ -85,6 +85,8 @@
"xtr1common": "cpp", "xtr1common": "cpp",
"xutility": "cpp", "xutility": "cpp",
"unordered_map": "cpp", "unordered_map": "cpp",
"xhash": "cpp" "xhash": "cpp",
"map": "cpp",
"xtree": "cpp"
} }
} }

View File

@ -5,6 +5,7 @@
#ifdef UNICODE_OFSTR #ifdef UNICODE_OFSTR
using ofString = std::wstring; using ofString = std::wstring;
using ofChar = wchar_t;
#define ofT(text) L##text #define ofT(text) L##text
#ifndef UNICODE #ifndef UNICODE
#define UNICODE #define UNICODE
@ -14,6 +15,7 @@ using ofString = std::wstring;
#endif #endif
#else #else
using ofString = std::string; using ofString = std::string;
using ofChar = char;
#define ofT(text) text #define ofT(text) text
#endif #endif

View File

@ -14,6 +14,8 @@ public:
static bool isSamePath(const ofString& pa, const ofString& pb); static bool isSamePath(const ofString& pa, const ofString& pb);
static ofString normalizePath(const ofString& path); static ofString normalizePath(const ofString& path);
static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to); static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to);
static ofString getFullRunPath();
static ofString getHome();
}; };
}; // namespace ofen }; // namespace ofen
#endif #endif

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h>
#include <string> #include <string>
#include <map> #include <map>
#include "of_def.hpp" #include "of_def.hpp"

View File

@ -1,6 +1,19 @@
#include "of_path.h" #include "of_path.h"
#include <filesystem> #include <filesystem>
#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
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace ofen { namespace ofen {
COfPath::COfPath() COfPath::COfPath()
@ -38,4 +51,56 @@ ofString COfPath::replaceStr(const ofString& str, const ofString& from, const of
} }
return result; return result;
} }
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
}
} // namespace ofen } // namespace ofen

View File

@ -1,4 +1,5 @@
#include "of_win.h" #include "of_win.h"
#include <windows.h>
constexpr auto g_VSReg = ofT(""); constexpr auto g_VSReg = ofT("");
constexpr auto g_EnvReg = ofT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"); constexpr auto g_EnvReg = ofT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");