passant linters a vore si trobe variables sense inicialitzar
This commit is contained in:
@@ -20,16 +20,16 @@
|
||||
namespace SystemUtils {
|
||||
|
||||
// Función auxiliar para crear una carpeta individual
|
||||
Result createSingleFolder(const std::string& path, int permissions) {
|
||||
struct stat st = {0};
|
||||
|
||||
// Verificar si ya existe
|
||||
if (stat(path.c_str(), &st) == 0) {
|
||||
return Result::SUCCESS; // Ya existe, no es error por defecto
|
||||
}
|
||||
auto createSingleFolder(const std::string& path, int permissions) -> Result {
|
||||
struct stat st = {0};
|
||||
|
||||
// Intentar crear la carpeta
|
||||
int result;
|
||||
// Verificar si ya existe
|
||||
if (stat(path.c_str(), &st) == 0) {
|
||||
return Result::SUCCESS; // Ya existe, no es error por defecto
|
||||
}
|
||||
|
||||
// Intentar crear la carpeta
|
||||
int result;
|
||||
#ifdef _WIN32
|
||||
result = _mkdir(path.c_str());
|
||||
#else
|
||||
@@ -50,63 +50,63 @@ namespace SystemUtils {
|
||||
}
|
||||
|
||||
return Result::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
// Función auxiliar para crear carpetas padre recursivamente
|
||||
Result createParentFolders(const std::string& path, int permissions) {
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = path.find('/', pos + 1)) != std::string::npos) {
|
||||
std::string parent = path.substr(0, pos);
|
||||
if (!parent.empty() && !folderExists(parent)) {
|
||||
Result result = createSingleFolder(parent, permissions);
|
||||
if (result != Result::SUCCESS && result != Result::ALREADY_EXISTS) {
|
||||
return result;
|
||||
}
|
||||
auto createParentFolders(const std::string& path, int permissions) -> Result {
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = path.find('/', pos + 1)) != std::string::npos) {
|
||||
std::string parent = path.substr(0, pos);
|
||||
if (!parent.empty() && !folderExists(parent)) {
|
||||
Result result = createSingleFolder(parent, permissions);
|
||||
if (result != Result::SUCCESS && result != Result::ALREADY_EXISTS) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return Result::SUCCESS;
|
||||
}
|
||||
|
||||
Result createApplicationFolder(const std::string& app_name, std::string& out_path) {
|
||||
FolderConfig config;
|
||||
return createApplicationFolder(app_name, out_path, config);
|
||||
return Result::SUCCESS;
|
||||
}
|
||||
|
||||
auto createApplicationFolder(const std::string& app_name, std::string& out_path) -> Result {
|
||||
FolderConfig config;
|
||||
return createApplicationFolder(app_name, out_path, config);
|
||||
}
|
||||
|
||||
auto createApplicationFolder(const std::string& app_name, std::string& out_path, const FolderConfig& config) -> Result {
|
||||
out_path = getApplicationDataPath(app_name);
|
||||
return createFolder(out_path, config);
|
||||
}
|
||||
|
||||
auto createFolder(const std::string& path) -> Result {
|
||||
FolderConfig config;
|
||||
return createFolder(path, config);
|
||||
}
|
||||
|
||||
auto createFolder(const std::string& path, const FolderConfig& config) -> Result {
|
||||
if (path.empty()) {
|
||||
return Result::INVALID_PATH;
|
||||
}
|
||||
|
||||
Result createApplicationFolder(const std::string& app_name, std::string& out_path, const FolderConfig& config) {
|
||||
out_path = getApplicationDataPath(app_name);
|
||||
return createFolder(out_path, config);
|
||||
// Verificar si ya existe y si eso es un error
|
||||
if (folderExists(path) && config.fail_if_exists) {
|
||||
return Result::ALREADY_EXISTS;
|
||||
}
|
||||
|
||||
Result createFolder(const std::string& path) {
|
||||
FolderConfig config;
|
||||
return createFolder(path, config);
|
||||
}
|
||||
|
||||
Result createFolder(const std::string& path, const FolderConfig& config) {
|
||||
if (path.empty()) {
|
||||
return Result::INVALID_PATH;
|
||||
// Crear carpetas padre si es necesario
|
||||
if (config.create_parents) {
|
||||
Result parent_result = createParentFolders(path, config.permissions);
|
||||
if (parent_result != Result::SUCCESS) {
|
||||
return parent_result;
|
||||
}
|
||||
|
||||
// Verificar si ya existe y si eso es un error
|
||||
if (folderExists(path) && config.fail_if_exists) {
|
||||
return Result::ALREADY_EXISTS;
|
||||
}
|
||||
|
||||
// Crear carpetas padre si es necesario
|
||||
if (config.create_parents) {
|
||||
Result parent_result = createParentFolders(path, config.permissions);
|
||||
if (parent_result != Result::SUCCESS) {
|
||||
return parent_result;
|
||||
}
|
||||
}
|
||||
|
||||
// Crear la carpeta final
|
||||
return createSingleFolder(path, config.permissions);
|
||||
}
|
||||
|
||||
std::string getApplicationDataPath(const std::string& app_name) {
|
||||
// Crear la carpeta final
|
||||
return createSingleFolder(path, config.permissions);
|
||||
}
|
||||
|
||||
auto getApplicationDataPath(const std::string& app_name) -> std::string {
|
||||
#ifdef _WIN32
|
||||
char* appdata = getenv("APPDATA");
|
||||
if (appdata) {
|
||||
@@ -127,33 +127,33 @@ namespace SystemUtils {
|
||||
std::string home = getHomeDirectory();
|
||||
return home + "/." + app_name;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
bool folderExists(const std::string& path) {
|
||||
struct stat st = {0};
|
||||
return (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
|
||||
}
|
||||
auto folderExists(const std::string& path) -> bool {
|
||||
struct stat st = {0};
|
||||
return (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
|
||||
}
|
||||
|
||||
const char* resultToString(Result result) {
|
||||
switch (result) {
|
||||
case Result::SUCCESS:
|
||||
return "Operación exitosa";
|
||||
case Result::PERMISSION_DENIED:
|
||||
return "Error: Permisos insuficientes";
|
||||
case Result::PATH_TOO_LONG:
|
||||
return "Error: Ruta demasiado larga";
|
||||
case Result::ALREADY_EXISTS:
|
||||
return "Error: La carpeta ya existe";
|
||||
case Result::INVALID_PATH:
|
||||
return "Error: Ruta inválida";
|
||||
case Result::UNKNOWN_ERROR:
|
||||
return "Error desconocido";
|
||||
default:
|
||||
return "Error no identificado";
|
||||
}
|
||||
auto resultToString(Result result) -> const char* {
|
||||
switch (result) {
|
||||
case Result::SUCCESS:
|
||||
return "Operación exitosa";
|
||||
case Result::PERMISSION_DENIED:
|
||||
return "Error: Permisos insuficientes";
|
||||
case Result::PATH_TOO_LONG:
|
||||
return "Error: Ruta demasiado larga";
|
||||
case Result::ALREADY_EXISTS:
|
||||
return "Error: La carpeta ya existe";
|
||||
case Result::INVALID_PATH:
|
||||
return "Error: Ruta inválida";
|
||||
case Result::UNKNOWN_ERROR:
|
||||
return "Error desconocido";
|
||||
default:
|
||||
return "Error no identificado";
|
||||
}
|
||||
}
|
||||
|
||||
std::string getHomeDirectory() {
|
||||
auto getHomeDirectory() -> std::string {
|
||||
#ifdef _WIN32
|
||||
char* userprofile = getenv("USERPROFILE");
|
||||
if (userprofile) {
|
||||
@@ -162,20 +162,20 @@ namespace SystemUtils {
|
||||
return "C:/Users/Default";
|
||||
#else
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw && pw->pw_dir) {
|
||||
if ((pw != nullptr) && (pw->pw_dir != nullptr)) {
|
||||
return std::string(pw->pw_dir);
|
||||
}
|
||||
|
||||
|
||||
// Fallback
|
||||
char* home = getenv("HOME");
|
||||
if (home) {
|
||||
if (home != nullptr) {
|
||||
return std::string(home);
|
||||
}
|
||||
return "/tmp";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
std::string getTempDirectory() {
|
||||
auto getTempDirectory() -> std::string {
|
||||
#ifdef _WIN32
|
||||
char* temp = getenv("TEMP");
|
||||
if (temp) {
|
||||
@@ -185,6 +185,6 @@ namespace SystemUtils {
|
||||
#else
|
||||
return "/tmp";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace SystemUtils
|
||||
Reference in New Issue
Block a user