#include <allegro.h>
#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <string> //string, getline()

using std::string;
using std::ifstream;

//******************************************************************************
// Come os tabs na linha a partir da coluna col e devolve a nova coluna
//******************************************************************************
int eat_tabs(string line, int col) {
	while (line[col]=='\t') {
		col++;
	}
	return col;
}

//******************************************************************************
// Pack do TXT de tiles
//******************************************************************************
int pack_tiles() {
	PACKFILE *pfile;
	ifstream dbfile;
	string line, temp;
	
	char *name;
	char *prefix;
	int k, j, num, icon, passable;

	dbfile.open("tiles.txt");
	pfile = pack_fopen("tiles.db", "wp");

	// le o ficheiro
	while (!dbfile.eof()) {
        getline(dbfile, line);
        if (line[0] != '*') {
				j = 0;
				
				/* tab inicial */
				j = eat_tabs(line,j);
				
				// tile number
				for (;line[j]!= '\t';j++){}
				temp = line.substr(0, j);
				num = atoi(temp.c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// tile icon
				for(;line[j]!= '\t';j++){}
				temp = line.substr(k, j-k);
				icon = atoi(temp.c_str());
				k = ++j;

            j = eat_tabs(line,j);
				// tile name
				for (;line[j]!= '\t';j++){}
				name = strdup((line.substr(k, j-k)).c_str());
				k = ++j;
				
				j = eat_tabs(line,j);
				// passable?
				for (;line[j]!= '\t';j++){}
				temp = line.substr(k, j-k);
				passable = atoi(temp.c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// prefix
				for (;line[j]!= '\0';j++) {}
				prefix = strdup((line.substr(k, j-k)).c_str());
				
				// gravar para o packfile
				pack_iputl(num, pfile);
				pack_iputl(icon, pfile);
				pack_iputl(ustrsizez(name),pfile);
				pack_fwrite(name, ustrsizez(name), pfile);
				pack_iputl(passable, pfile);
				pack_iputl(ustrsizez(prefix),pfile);
				pack_fwrite(prefix, ustrsizez(prefix), pfile);
		}
	}
	dbfile.close();
	pack_fclose(pfile);
	textout_ex(screen, font, "+ Tiles packed and saved...", 10, 15, 11, -1);
}

//******************************************************************************
// Pack do TXT de fringe
//******************************************************************************
int pack_fringe() {
	PACKFILE *pfile;
	ifstream dbfile;
	string line, temp;

	char *name;
	char *prefix;
	int k, j, num, icon, passable;

	dbfile.open("fringe.txt");
	pfile = pack_fopen("fringe.db", "wp");

	// le o ficheiro
	while (!dbfile.eof()) {
        getline(dbfile, line);
        if (line[0] != '*') {
				j = 0;
				
				/* tab inicial */
				j = eat_tabs(line,j);
	
				// tile number
				for (;line[j]!= '\t';j++){}
				temp = line.substr(0, j);
				num = atoi(temp.c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// tile icon
				for(;line[j]!= '\t';j++){}
				temp = line.substr(k, j-k);
				icon = atoi(temp.c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// tile name
				for (;line[j]!= '\t';j++){}
				name = strdup((line.substr(k, j-k)).c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// passable?
				for (;line[j]!= '\t';j++){}
				temp = line.substr(k, j-k);
				passable = atoi(temp.c_str());
				k = ++j;

				j = eat_tabs(line,j);
				// prefix
				for (;line[j]!= '\0';j++) {}
				prefix = strdup((line.substr(k, j-k)).c_str());

				// gravar para o packfile
				pack_iputl(num, pfile);
				pack_iputl(icon, pfile);
				pack_iputl(ustrsizez(name),pfile);
				pack_fwrite(name, ustrsizez(name), pfile);
				pack_iputl(passable, pfile);
				pack_iputl(ustrsizez(prefix),pfile);
				pack_fwrite(prefix, ustrsizez(prefix), pfile);
		}
	}
	dbfile.close();
	pack_fclose(pfile);
	textout_ex(screen, font, "+ Fringe packed and saved...", 10, 25, 11, -1);
}

//******************************************************************************
// Pack do TXT de objectos
//******************************************************************************
int pack_objects() {
PACKFILE *pfile;
	ifstream dbfile;
	string line, temp;

	char *name;
	char *prefix;
	int k, j, num, icon, passable;

	dbfile.open("objects.txt");
	pfile = pack_fopen("objects.db", "wp");

	// le o ficheiro
	while (!dbfile.eof()) {
        getline(dbfile, line);
        if (line[0] != '*') {
				j = 0;

				/* tab inicial */
				j = eat_tabs(line,j);

				// tile number
				for (;line[j]!= '\t';j++){}
				temp = line.substr(0, j);
				num = atoi(temp.c_str());
				k = ++j;

				// tile icon
				for(;line[j]!= '\t';j++){}
				temp = line.substr(k, j-k);
				icon = atoi(temp.c_str());
				k = ++j;

				// tile name
				for (;line[j]!= '\t';j++){}
				name = strdup((line.substr(k, j-k)).c_str());
				k = ++j;
				// passable?
				for (;line[j]!= '\t';j++){}
				(line[j-1] == '0') ? passable = 0 : passable = 1;
				k = ++j;

				// prefix
				for (;line[j]!= '\0';j++) {}
				prefix = strdup((line.substr(k, j-k)).c_str());

				// gravar para o packfile
				pack_iputl(num, pfile);
				pack_iputl(icon, pfile);
				pack_iputl(ustrsizez(name),pfile);
				pack_fwrite(name, ustrsizez(name), pfile);
				pack_iputl(passable, pfile);
				pack_iputl(ustrsizez(prefix),pfile);
				pack_fwrite(prefix, ustrsizez(prefix), pfile);
		}
	}
	dbfile.close();
	pack_fclose(pfile);
	textout_ex(screen, font, "+ Fringe packed and saved...", 10, 35, 11, -1);
}


int main() {
	allegro_init();
	install_keyboard();
	set_gfx_mode(GFX_SAFE, 320, 200, 0, 0);

	// pack the txt files
	pack_tiles();
	pack_fringe();
	pack_objects();
	textout_ex(screen, font, "+ All done!", 10, 45, 10, -1);
	
	while (!key[KEY_ESC]){}
	allegro_exit();
	return 0;
	
} END_OF_MAIN();
