diff --git a/.vscode/settings.json b/.vscode/settings.json index 4bc533a..e9e13c9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -85,6 +85,8 @@ "xtr1common": "cpp", "xutility": "cpp", "unordered_map": "cpp", - "xhash": "cpp" + "xhash": "cpp", + "map": "cpp", + "xtree": "cpp" } } \ No newline at end of file diff --git a/include/of_def.hpp b/include/of_def.hpp index 5813f7f..b557e12 100644 --- a/include/of_def.hpp +++ b/include/of_def.hpp @@ -5,6 +5,7 @@ #ifdef UNICODE_OFSTR using ofString = std::wstring; +using ofChar = wchar_t; #define ofT(text) L##text #ifndef UNICODE #define UNICODE @@ -14,6 +15,7 @@ using ofString = std::wstring; #endif #else using ofString = std::string; +using ofChar = char; #define ofT(text) text #endif diff --git a/include/of_path.h b/include/of_path.h index e7897ef..85eabed 100644 --- a/include/of_path.h +++ b/include/of_path.h @@ -14,6 +14,8 @@ public: 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); + static ofString getFullRunPath(); + static ofString getHome(); }; }; // namespace ofen #endif \ No newline at end of file diff --git a/include/of_win.h b/include/of_win.h index ba101f1..467cb58 100644 --- a/include/of_win.h +++ b/include/of_win.h @@ -1,7 +1,6 @@ #pragma once #ifdef _WIN32 -#include #include #include #include "of_def.hpp" diff --git a/src/of_path.cpp b/src/of_path.cpp index 0da8c2e..b37c61b 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -1,6 +1,19 @@ #include "of_path.h" #include +#ifdef _WIN32 +#include +#elif defined(__clang__) && defined(__APPLE__) +#include +#include +#else +#include +#endif + +#ifndef PATH_MAX +#define PATH_MAX 256 +#endif + namespace fs = std::filesystem; namespace ofen { COfPath::COfPath() @@ -38,4 +51,56 @@ ofString COfPath::replaceStr(const ofString& str, const ofString& from, const of } 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 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 diff --git a/src/of_win.cpp b/src/of_win.cpp index 05e3342..569a586 100644 --- a/src/of_win.cpp +++ b/src/of_win.cpp @@ -1,4 +1,5 @@ #include "of_win.h" +#include constexpr auto g_VSReg = ofT(""); constexpr auto g_EnvReg = ofT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");