fix:修正当前目录不正确的问题。
This commit is contained in:
parent
af258785dc
commit
19e8a2da6a
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
build
|
build
|
||||||
.cache
|
.cache
|
||||||
.vs
|
.vs
|
||||||
vs*
|
vs*
|
||||||
|
*.exe
|
8
main.cpp
8
main.cpp
@ -381,13 +381,13 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
#if _DEBUG
|
#if _DEBUG
|
||||||
std::cout << "Debug Mode." << std::endl;
|
std::cout << "Debug Mode." << std::endl;
|
||||||
fs::path exe_path(argv[0]);
|
fs::path exe_path(CUtil::get_exe_path());
|
||||||
gParam.exe_dir = fs::current_path().parent_path().string();
|
gParam.exe_dir = exe_path.parent_path().parent_path().string();
|
||||||
std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n";
|
std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n";
|
||||||
#else
|
#else
|
||||||
std::cout << "Release Mode." << std::endl;
|
std::cout << "Release Mode." << std::endl;
|
||||||
fs::path exe_path(argv[0]);
|
fs::path exe_path(CUtil::get_exe_path());
|
||||||
gParam.exe_dir = fs::current_path().string();
|
gParam.exe_dir = exe_path.parent_path().string();
|
||||||
std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n";
|
std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
40
src/pub.cpp
40
src/pub.cpp
@ -1,6 +1,15 @@
|
|||||||
#include "pub.h"
|
#include "pub.h"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#elif defined(__clang__) && defined(__APPLE__)
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <mach-o/dyld.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
CUtil::CUtil() = default;
|
CUtil::CUtil() = default;
|
||||||
@ -81,4 +90,33 @@ std::string CUtil::get_keybindings_file()
|
|||||||
#endif
|
#endif
|
||||||
path = ret.append("keybindings.json").string();
|
path = ret.append("keybindings.json").string();
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string CUtil::get_exe_path()
|
||||||
|
{
|
||||||
|
std::string path;
|
||||||
|
#ifdef _WIN32
|
||||||
|
char buffer[MAX_PATH];
|
||||||
|
DWORD length = GetModuleFileName(NULL, buffer, MAX_PATH);
|
||||||
|
if (length == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return std::string(buffer, length);
|
||||||
|
#elif defined(__clang__) && defined(__APPLE__)
|
||||||
|
uint32_t size = 0;
|
||||||
|
_NSGetExecutablePath(NULL, &size); // 获取路径缓冲区的大小
|
||||||
|
std::vector<char> buffer(size); // 创建缓冲区
|
||||||
|
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return std::string(buffer.data());
|
||||||
|
#else
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
||||||
|
if (len == -1) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
buffer[len] = '\0'; // 确保字符串以null终止
|
||||||
|
return std::string(buffer);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@ -16,6 +16,7 @@ public:
|
|||||||
static std::string replace(const std::string& content, const std::string& from, const std::string& to);
|
static std::string replace(const std::string& content, const std::string& from, const std::string& to);
|
||||||
static std::string get_home();
|
static std::string get_home();
|
||||||
static std::string get_keybindings_file();
|
static std::string get_keybindings_file();
|
||||||
|
static std::string get_exe_path();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user