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"));
delete manager;
#ifdef RECORDING
saveDemoFile();
saveDemoFile(asset->get("demo1.bin"));
#endif
// Elimina todos los objetos contenidos en vectores
@@ -642,30 +642,29 @@ void Game::unloadMedia()
}
// 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
bool success = true;
const std::string filename = f.substr(f.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(f.c_str(), "r+b");
const std::string fileName = filePath.substr(filePath.find_last_of("\\/") + 1);
// El fichero no existe
SDL_RWops *file = SDL_RWFromFile(filePath.c_str(), "r+b");
if (file == nullptr)
{
{ // El fichero no existe
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
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 (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
@@ -689,7 +688,7 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
{ // Si no puede crear el fichero
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;
}
@@ -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
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
@@ -720,13 +719,12 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool Game::saveDemoFile()
bool Game::saveDemoFile(std::string filePath)
{
bool success = true;
const std::string p = asset->get("demo1.bin");
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
const std::string filename = filePath.substr(filePath.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)
{
// Guardamos los datos

View File

@@ -222,10 +222,10 @@ private:
void unloadMedia();
// 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
// Guarda el fichero de datos para la demo
bool saveDemoFile();
bool saveDemoFile(std::string filePath);
#endif
// Crea una formación de enemigos
void deployEnemyFormation();

View File

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

View File

@@ -35,8 +35,8 @@ public:
void add(hiScoreEntry_t entry);
// 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
bool saveToFile(std::string filepath);
bool saveToFile(std::string filePath);
};

View File

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