path:添加home和exe路径获取。
This commit is contained in:
parent
f8adaf2386
commit
837a4c8dd6
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -85,6 +85,8 @@
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"xhash": "cpp"
|
||||
"xhash": "cpp",
|
||||
"map": "cpp",
|
||||
"xtree": "cpp"
|
||||
}
|
||||
}
|
@ -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
|
||||
|
||||
|
@ -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
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "of_def.hpp"
|
||||
|
@ -1,6 +1,19 @@
|
||||
#include "of_path.h"
|
||||
#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 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<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
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "of_win.h"
|
||||
#include <windows.h>
|
||||
|
||||
constexpr auto g_VSReg = ofT("");
|
||||
constexpr auto g_EnvReg = ofT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
|
||||
|
Loading…
x
Reference in New Issue
Block a user