Compare commits

...

2 Commits

Author SHA1 Message Date
3f203fee62 - Proves de lectura dels nivells en format text 2023-10-17 20:15:11 +02:00
771e7abb65 - [NEW] jfile::getFileStream() 2023-10-17 20:14:53 +02:00
3 changed files with 159 additions and 0 deletions

View File

@@ -192,6 +192,74 @@ namespace file
}
}
// Obté un "std::ifstream" al arxiu que se li demana, independentment de la font (arxius individual en carpeta, o arxiu de recursos)
std::ifstream getFileStream(const std::string resourcename, int *filesize, const bool binary)
{
// Si tenim configurat agafar els recursos de arxiu, pero encara no tenim la taula de continguts carregada...
if (file_source == SOURCE_FILE and toc.size() == 0)
{
// Si fallem al intentar carregar la taula de continguts de l'arxiu de recursos, canviem a pillar els recursos de carpeta
if (not getTableOfContents())
{
setSource(SOURCE_FOLDER);
}
}
std::ifstream f;
// Si estem pillant els recursos de un arxiu de recursos...
if (file_source == SOURCE_FILE)
{
// Busquem el recurs en la taula de continguts usant la ruta
bool found = false;
uint32_t count = 0;
while (!found && count < toc.size())
{
found = (resourcename == toc[count].path);
if (!found)
{
count++;
}
}
// Si no trobem el recurs, petem el mame
if (!found)
{
// [TODO] Donar mes informació de quin recurs no havem trobat
perror("El recurs no s'ha trobat en l'arxiu de recursos");
exit(1);
}
// Agafem el tamany del recurs de la taula de continguts
if (filesize) *filesize = toc[count].size;
// obrim l'arxiu de recursos
f.open(resource_filename.c_str(), binary ? std::ios::binary : std::ios::in);
if (!f.is_open()) // En el raruno cas de que a este altures pete al obrir el arxiu de recursos, petem el mame
{
// [TODO] Donar mes informació de quin recurs no havem trobat
perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1);
}
// Anem a la posició on està el recurs que volem. Amb açò "f" ja està preparat per a ser tornar.
// Ojo, realment estic tornant un FILE* al arxiu de recursos, pero ja apuntant al moment en que comença el recurs que volem.
// Ho dic perque si fem fseek(f, 0, SEEK_SET) tornarà al principi de l'arxiu de recursos, no del recurs. Tindre-ho en compte.
f.seekg(toc[count].offset);
}
else
{
// Si estem pillant els recursos de carpeta, simplement obrim el arxiu en questió i tornem el FILE* associat.
f.open((resource_folder + resourcename), binary ? std::ios::binary : std::ios::in);
f.seekg(0, std::ios_base::end);
if (filesize) *filesize = f.tellg();
f.seekg(0, std::ios_base::beg);
}
// Tornar el punter FILE* al arxiu. OJO! Tenim que tancar-lo quan acabem amb ell
return f;
}
// Obté un "FILE*" al arxiu que se li demana, independentment de la font (arxius individual en carpeta, o arxiu de recursos)
FILE *getFilePointer(const std::string resourcename, int *filesize, const bool binary)
{

View File

@@ -1,5 +1,6 @@
#pragma once
#include <stdio.h>
#include <fstream>
#include <string>
#define SOURCE_FILE 0
@@ -23,6 +24,13 @@ namespace file
/// @param src SOURCE_FILE o SOURCE_FOLDER, si es vol que se pillen recursos de arxiu o de carpeta
void setSource(const int src);
/// @brief Obté un "std::ifstream" al arxiu que se li demana, independentment de la font (arxius individual en carpeta, o arxiu de recursos). Recordar tancar-lo al acabar amb ell.
/// @param resourcename el nom de l'arxiu que volem obrir
/// @param filesize paràmetre de retorn. Ací es torna el tamany de l'arxiu
/// @param binary si volem obrir el arxiu en format binary
/// @return un std::ifstream al arxiu
std::ifstream getFileStream(const std::string resourcename, int *filesize = nullptr, const bool binary = false);
/// @brief Obté un "FILE*" al arxiu que se li demana, independentment de la font (arxius individual en carpeta, o arxiu de recursos). Recordar tancar-lo al acabar amb ell.
/// @param resourcename el nom de l'arxiu que volem obrir
/// @param filesize paràmetre de retorn. Ací es torna el tamany de l'arxiu

83
tools/level_reader.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>
namespace textfile
{
char *buffer = nullptr;
int fsize = 0;
int p = 0;
const bool open(std::string filename)
{
FILE *f = fopen(filename.c_str(), "rb");
if (!f) { printf("ERROR AL OBRIR EL ARXIU\n"); return false; }
fseek(f, 0, SEEK_END);
fsize = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = (char*)malloc(fsize);
fread(buffer, fsize, 1, f);
fclose(f);
p = 0;
return true;
}
void close()
{
free(buffer);
}
std::string getNextToken()
{
char token[255];
int tpos = 0;
// Ignore whitespace
while (p<fsize && buffer[p]<=32 ) p++;
// Grab token
while (p<fsize && buffer[p]>32 ) token[tpos++]=buffer[p++];
token[tpos]=0;
return std::string(token);
}
const bool searchToken(std::string token)
{
while (p<fsize && getNextToken() != token) p++;
return p<fsize;
}
std::string getStringValue(std::string token)
{
textfile::searchToken(token);
textfile::searchToken("=");
return textfile::getNextToken();
}
const int getIntValue(std::string token)
{
return std::stoi(getStringValue(token));
}
}
int main(int argc, char *argv[])
{
textfile::open("../data/mapes.txt");
int num;
do {
textfile::searchToken("LEVEL");
std::string token = textfile::getNextToken();
num = std::stoi(token);
} while (num != std::stoi(std::string(argv[1])));
printf("tileset = %i\n", textfile::getIntValue("tileset") );
printf("orientacio = %i\n", textfile::getIntValue("orientacio") );
printf("arounders = %i\n", textfile::getIntValue("arounders") );
printf("necessaris = %i\n", textfile::getIntValue("necessaris") );
textfile::close();
return 0;
}