Estandaritzats els accessos a fitxers

This commit is contained in:
2024-09-27 19:41:45 +02:00
parent 20de9e4b72
commit 0a8d0479a0
5 changed files with 33 additions and 36 deletions

View File

@@ -66,7 +66,7 @@ Game::~Game()
manager->saveToFile(asset->get("score.bin")); manager->saveToFile(asset->get("score.bin"));
delete manager; delete manager;
#ifdef RECORDING #ifdef RECORDING
saveDemoFile(); saveDemoFile(asset->get("demo1.bin"));
#endif #endif
// Elimina todos los objetos contenidos en vectores // Elimina todos los objetos contenidos en vectores
@@ -642,30 +642,29 @@ void Game::unloadMedia()
} }
// Carga el fichero de datos para la demo // Carga el fichero de datos para la demo
bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]) bool Game::loadDemoFile(std::string filePath, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
{ {
// Indicador de éxito en la carga // Indicador de éxito en la carga
bool success = true; bool success = true;
const std::string filename = f.substr(f.find_last_of("\\/") + 1); const std::string fileName = filePath.substr(filePath.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(f.c_str(), "r+b");
// El fichero no existe SDL_RWops *file = SDL_RWFromFile(filePath.c_str(), "r+b");
if (file == nullptr) if (file == nullptr)
{ { // El fichero no existe
if (options.console) if (options.console)
{ {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; std::cout << "Warning: Unable to open " << fileName.c_str() << " file" << std::endl;
} }
// Creamos el fichero para escritura // Creamos el fichero para escritura
file = SDL_RWFromFile(f.c_str(), "w+b"); file = SDL_RWFromFile(filePath.c_str(), "w+b");
// Si no existe el fichero // Si ha creado el fichero
if (file != nullptr) if (file != nullptr)
{ {
if (options.console) if (options.console)
{ {
std::cout << "New file (" << filename.c_str() << ") created!" << std::endl; std::cout << "New file (" << fileName.c_str() << ") created!" << std::endl;
} }
// Inicializas los datos y los guarda en el fichero // Inicializas los datos y los guarda en el fichero
@@ -689,7 +688,7 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
{ // Si no puede crear el fichero { // Si no puede crear el fichero
if (options.console) if (options.console)
{ {
std::cout << "Error: Unable to create file " << filename.c_str() << std::endl; std::cout << "Error: Unable to create file " << fileName.c_str() << std::endl;
} }
success = false; success = false;
} }
@@ -700,7 +699,7 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
// Mensaje de proceder a la carga de los datos // Mensaje de proceder a la carga de los datos
if (options.console) if (options.console)
{ {
std::cout << "Reading file: " << filename.c_str() << std::endl; std::cout << "Reading file: " << fileName.c_str() << std::endl;
} }
// Lee todos los datos del fichero y los deja en el destino // Lee todos los datos del fichero y los deja en el destino
@@ -720,13 +719,12 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
#ifdef RECORDING #ifdef RECORDING
// Guarda el fichero de datos para la demo // Guarda el fichero de datos para la demo
bool Game::saveDemoFile() bool Game::saveDemoFile(std::string filePath)
{ {
bool success = true; bool success = true;
const std::string p = asset->get("demo1.bin"); const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b"); SDL_RWops *file = SDL_RWFromFile(filePath.c_str(), "w+b");
if (file != nullptr) if (file != nullptr)
{ {
// Guardamos los datos // Guardamos los datos

View File

@@ -222,10 +222,10 @@ private:
void unloadMedia(); void unloadMedia();
// Carga el fichero de datos para la demo // Carga el fichero de datos para la demo
bool loadDemoFile(std::string fileName, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]); bool loadDemoFile(std::string filePath, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]);
#ifdef RECORDING #ifdef RECORDING
// Guarda el fichero de datos para la demo // Guarda el fichero de datos para la demo
bool saveDemoFile(); bool saveDemoFile(std::string filePath);
#endif #endif
// Crea una formación de enemigos // Crea una formación de enemigos
void deployEnemyFormation(); void deployEnemyFormation();

View File

@@ -59,14 +59,13 @@ void ManageHiScoreTable::sort()
} }
// Carga la tabla con los datos de un fichero // Carga la tabla con los datos de un fichero
bool ManageHiScoreTable::loadFromFile(std::string filepath) bool ManageHiScoreTable::loadFromFile(std::string filePath)
{ {
clear(); clear();
bool success = true; bool success = true;
const std::string p = filepath; const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
const std::string filename = p.substr(p.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(filePath.c_str(), "r+b");
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
if (file) if (file)
{ {
@@ -116,12 +115,11 @@ bool ManageHiScoreTable::loadFromFile(std::string filepath)
} }
// Guarda la tabla en un fichero // Guarda la tabla en un fichero
bool ManageHiScoreTable::saveToFile(std::string filepath) bool ManageHiScoreTable::saveToFile(std::string filePath)
{ {
bool success = true; bool success = true;
const std::string p = filepath; const std::string fileName = filePath.substr(filePath.find_last_of("\\/") + 1);
const std::string filename = p.substr(p.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(filePath.c_str(), "w+b");
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file) if (file)
{ {
@@ -135,7 +133,7 @@ bool ManageHiScoreTable::saveToFile(std::string filepath)
} }
#ifdef DEBUG #ifdef DEBUG
std::cout << "Writing file: " << filename.c_str() << std::endl; std::cout << "Writing file: " << fileName.c_str() << std::endl;
#endif #endif
// Cierra el fichero // Cierra el fichero
SDL_RWclose(file); SDL_RWclose(file);
@@ -143,7 +141,7 @@ bool ManageHiScoreTable::saveToFile(std::string filepath)
else else
{ {
#ifdef DEBUG #ifdef DEBUG
std::cout << "Error: Unable to save " << filename.c_str() << " file! " << SDL_GetError() << std::endl; std::cout << "Error: Unable to save " << fileName.c_str() << " file! " << SDL_GetError() << std::endl;
#endif #endif
} }
return success; return success;

View File

@@ -35,8 +35,8 @@ public:
void add(hiScoreEntry_t entry); void add(hiScoreEntry_t entry);
// Carga la tabla con los datos de un fichero // Carga la tabla con los datos de un fichero
bool loadFromFile(std::string filepath); bool loadFromFile(std::string filePath);
// Guarda la tabla en un fichero // Guarda la tabla en un fichero
bool saveToFile(std::string filepath); bool saveToFile(std::string filePath);
}; };

View File

@@ -94,7 +94,7 @@ bool loadOptionsFile(std::string filePath)
bool success = true; bool success = true;
// Variables para manejar el fichero // Variables para manejar el fichero
std::string line; const std::string fileName = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath); std::ifstream file(filePath);
// Si el fichero se puede abrir // Si el fichero se puede abrir
@@ -103,8 +103,9 @@ bool loadOptionsFile(std::string filePath)
// Procesa el fichero linea a linea // Procesa el fichero linea a linea
if (options.console) if (options.console)
{ {
std::cout << "Reading file: " << filePath << std::endl; std::cout << "Reading file: " << fileName << std::endl;
} }
std::string line;
while (std::getline(file, line)) while (std::getline(file, line))
{ {
// Comprueba que la linea no sea un comentario // Comprueba que la linea no sea un comentario
@@ -117,7 +118,7 @@ bool loadOptionsFile(std::string filePath)
{ {
if (options.console) if (options.console)
{ {
std::cout << "Warning: file " << filePath << std::endl; std::cout << "Warning: file " << fileName << std::endl;
std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl; std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl;
} }
success = false; success = false;
@@ -160,21 +161,21 @@ bool loadOptionsFile(std::string filePath)
// Guarda el fichero de configuración // Guarda el fichero de configuración
bool saveOptionsFile(std::string filePath) bool saveOptionsFile(std::string filePath)
{ {
const std::string filename = filePath; const std::string fileName = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.good()) if (!file.good())
{ {
if (options.console) if (options.console)
{ {
std::cout << filename << " can't be opened" << std::endl; std::cout << fileName << " can't be opened" << std::endl;
} }
return false; return false;
} }
if (options.console) if (options.console)
{ {
std::cout << "Writing file: " << filename << std::endl; std::cout << "Writing file: " << fileName << std::endl;
} }
// Opciones de video // Opciones de video