- Proves de lectura dels nivells en format text
This commit is contained in:
83
tools/level_reader.cpp
Normal file
83
tools/level_reader.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user