93 lines
2.5 KiB
C++
93 lines
2.5 KiB
C++
// path_utils.cpp - Implementació de utilitats de rutes
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "path_utils.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
|
|
namespace Utils {
|
|
|
|
// Variables globals per guardar argv[0]
|
|
static std::string executable_path_;
|
|
static std::string executable_directory_;
|
|
|
|
// Inicialitzar el sistema de rutes amb argv[0]
|
|
void initializePathSystem(const char* argv0) {
|
|
if (!argv0) {
|
|
std::cerr << "[PathUtils] ADVERTÈNCIA: argv[0] és nullptr\n";
|
|
executable_path_ = "";
|
|
executable_directory_ = ".";
|
|
return;
|
|
}
|
|
|
|
executable_path_ = argv0;
|
|
|
|
// Extreure el directori
|
|
std::filesystem::path path(argv0);
|
|
executable_directory_ = path.parent_path().string();
|
|
|
|
if (executable_directory_.empty()) {
|
|
executable_directory_ = ".";
|
|
}
|
|
|
|
std::cout << "[PathUtils] Executable: " << executable_path_ << "\n";
|
|
std::cout << "[PathUtils] Directori: " << executable_directory_ << "\n";
|
|
}
|
|
|
|
// Obtenir el directori de l'executable
|
|
std::string getExecutableDirectory() {
|
|
if (executable_directory_.empty()) {
|
|
std::cerr << "[PathUtils] ADVERTÈNCIA: Sistema de rutes no inicialitzat\n";
|
|
return ".";
|
|
}
|
|
return executable_directory_;
|
|
}
|
|
|
|
// Detectar si estem dins un bundle de macOS
|
|
bool isMacOSBundle() {
|
|
#ifdef MACOS_BUNDLE
|
|
return true;
|
|
#else
|
|
// Detecció en temps d'execució
|
|
// Cercar ".app/Contents/MacOS" a la ruta de l'executable
|
|
std::string exe_dir = getExecutableDirectory();
|
|
return exe_dir.find(".app/Contents/MacOS") != std::string::npos;
|
|
#endif
|
|
}
|
|
|
|
// Obtenir la ruta base dels recursos
|
|
std::string getResourceBasePath() {
|
|
std::string exe_dir = getExecutableDirectory();
|
|
|
|
if (isMacOSBundle()) {
|
|
// Bundle de macOS: recursos a ../Resources des de MacOS/
|
|
std::cout << "[PathUtils] Detectat bundle de macOS\n";
|
|
return exe_dir + "/../Resources";
|
|
} else {
|
|
// Executable normal: recursos al mateix directori
|
|
return exe_dir;
|
|
}
|
|
}
|
|
|
|
// Normalitzar ruta (convertir barres, etc.)
|
|
std::string normalizePath(const std::string& path) {
|
|
std::string normalized = path;
|
|
|
|
// Convertir barres invertides a normals
|
|
std::ranges::replace(normalized, '\\', '/');
|
|
|
|
// Simplificar rutes amb filesystem
|
|
try {
|
|
std::filesystem::path fs_path(normalized);
|
|
normalized = fs_path.lexically_normal().string();
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "[PathUtils] Error normalitzant ruta: " << e.what() << "\n";
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
|
|
} // namespace Utils
|