64 lines
2 KiB
C++
Executable file
64 lines
2 KiB
C++
Executable file
/*
|
|
* InfiniPaint
|
|
* Copyright (C) 2025-2026 Yousef Khadadeh
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "SwitchCWD.hpp"
|
|
#include <iostream>
|
|
#include <filesystem>
|
|
|
|
#ifdef __APPLE__
|
|
#include <CoreFoundation/CFBundle.h>
|
|
#elif _WIN32
|
|
#include <Shlwapi.h>
|
|
#endif
|
|
|
|
|
|
|
|
void switch_cwd() {
|
|
#ifdef __APPLE__
|
|
CFBundleRef mainBundle = CFBundleGetMainBundle();
|
|
CFURLRef resourcesURL = CFBundleCopyBundleURL(mainBundle);
|
|
CFStringRef str = CFURLCopyFileSystemPath(resourcesURL, kCFURLPOSIXPathStyle);
|
|
CFRelease(resourcesURL);
|
|
char path[4096];
|
|
|
|
CFStringGetCString(str, path, 4096, kCFStringEncodingUTF8);
|
|
CFRelease(str);
|
|
std::string newCWD(path);
|
|
#ifdef MACOS_MAKE_BUNDLE
|
|
newCWD += "/Contents/Resources";
|
|
#endif
|
|
|
|
std::filesystem::current_path(std::filesystem::path(newCWD));
|
|
#elif _WIN32
|
|
// https://stackoverflow.com/a/33613252 but UTF-8
|
|
constexpr int MAX_PATH_STEP = 2048;
|
|
std::string pathStr;
|
|
DWORD copied = 0;
|
|
do {
|
|
pathStr.resize(pathStr.size() + MAX_PATH_STEP);
|
|
copied = GetModuleFileName(NULL, pathStr.data(), pathStr.size());
|
|
} while(copied >= pathStr.size());
|
|
pathStr.resize(copied);
|
|
|
|
std::filesystem::path exePath(pathStr);
|
|
std::filesystem::current_path(exePath.parent_path());
|
|
#elif __linux__
|
|
std::filesystem::path exePath = std::filesystem::canonical("/proc/self/exe");
|
|
std::filesystem::current_path(exePath.parent_path());
|
|
#endif
|
|
}
|