fdfb84170f
Cambios aplicados: - readability-braces-around-statements (añadir llaves en ifs/fors) - readability-implicit-bool-conversion (puntero → bool explícito) - readability-container-size-empty (.empty() en lugar de .size()==0) - readability-container-contains (.contains() C++20) - readability-make-member-function-const (métodos const) - readability-else-after-return (5 casos adicionales) - Añadido #include <cmath> en defaults.hpp Checks excluidos (justificados): - identifier-naming: Cascada de 300+ cambios - identifier-length: Nombres cortos son OK en este proyecto - magic-numbers: Demasiados falsos positivos - convert-member-functions-to-static: Rompe encapsulación - use-anyofallof: C++20 ranges no universal - function-cognitive-complexity: Complejidad aceptable - clang-analyzer-security.insecureAPI.rand: rand() suficiente para juegos
91 lines
2.5 KiB
C++
91 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 == nullptr) {
|
|
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";
|
|
} // 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
|