User:Fizz/Hero Parser.c


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 


 * 1) define NAMELIMIT 256
 * 2) define HEROLIMIT 256
 * 3) define LINELIMIT 1024
 * 4) define TOKENLIMIT 256


 * 1) define OUTFILENAME "table.txt"

struct hero_struct { char name[NAMELIMIT]; int RBErating;

int melee; int ranged;

int agility; int intelligence; int strength;

int carry; int disabler; int durable; int escape; int initiator; int jungler; int lanesupport; int nuker; int pusher; int support; };

// There's a flaw in strncpy: if max chars are read and no // null encountered, it won't put one in. Fix this... char * mod_strncpy (char * s1, const char * s2, int max) {	char * ret;

ret = strncpy(s1, s2, max); s1[max - 1] = '\0';

return ret; }

int main (int argc, char ** argv) {	FILE * infile; FILE * outfile; struct hero_struct hero[HEROLIMIT]; char inputline[LINELIMIT]; char * eofcheck; int n;	int heroes; char * token; if (argc < 2) {		printf("Usage: %s filename\n", argv[0]); return 1; }	infile = fopen(argv[1], "r"); if (infile == NULL) {		fprintf(stderr, "Error opening input file %s\n", argv[1]); return 2; }	outfile = fopen(OUTFILENAME, "w"); if (outfile == NULL) {		fprintf(stderr, "Error opening output file %s\n", argv[1]); return 2; }

for (n = 0; n < HEROLIMIT; n++) {		mod_strncpy(hero[n].name, "Unknown", NAMELIMIT); hero[n].RBErating = -1; hero[n].melee = 0; hero[n].ranged = 0; hero[n].agility = 0; hero[n].intelligence = 0; hero[n].strength = 0; hero[n].carry = 0; hero[n].disabler = 0; hero[n].durable = 0; hero[n].escape = 0; hero[n].initiator = 0; hero[n].jungler = 0; hero[n].lanesupport = 0; hero[n].nuker = 0; hero[n].pusher = 0; hero[n].support = 0; };	n = 0; while (1) {		eofcheck = fgets(inputline, LINELIMIT, infile); if (eofcheck == NULL) break; if (strlen(inputline) < 10) continue; token = strtok(inputline, ". \t\n\r"); mod_strncpy(hero[n].name, token, NAMELIMIT); while (1) {			token = strtok(NULL, ". \t\n\r"); if (token == NULL) break; if (strlen(token) == 2 && isdigit(token[0]) && isdigit(token[1])) { hero[n].RBErating = atoi(token); } else if (strcmp(token, "melee") == 0) { hero[n].melee = 1; } else if (strcmp(token, "ranged") == 0) { hero[n].ranged = 1; } else if (strcmp(token, "agility") == 0) { hero[n].agility = 1; } else if (strcmp(token, "intelligence") == 0) { hero[n].intelligence = 1; } else if (strcmp(token, "strength") == 0) { hero[n].strength = 1; } else if (strcmp(token, "carry") == 0) { hero[n].carry = 1; } else if (strcmp(token, "disabler") == 0) { hero[n].disabler = 1; } else if (strcmp(token, "durable") == 0) { hero[n].durable = 1; } else if (strcmp(token, "escape") == 0) { hero[n].escape = 1; } else if (strcmp(token, "initiator") == 0) { hero[n].initiator = 1; } else if (strcmp(token, "jungler") == 0) { hero[n].jungler = 1; } else if (strcmp(token, "lane-support") == 0) { hero[n].lanesupport = 1; } else if (strcmp(token, "nuker") == 0) { hero[n].nuker = 1; } else if (strcmp(token, "pusher") == 0) { hero[n].pusher = 1; } else if (strcmp(token, "support") == 0) { hero[n].support = 1; } else { strncat(hero[n].name, " ", NAMELIMIT - strlen(hero[n].name)); strncat(hero[n].name, token, NAMELIMIT - strlen(hero[n].name)); }		}		n++; }	heroes = n; fprintf(outfile, "{| class=\"wikitable sortable\"\n|-\n! Hero !! RBE !! Type !! Att !! Car !! Dis !! Dur !! Esc !! Ini !! Jun !! Lan !! Nuk !! Pus !! Sup\n|-\n"); for (n = 0; n < heroes; n++) {		fprintf(outfile, "| "); fprintf(outfile, "%s", hero[n].name); fprintf(outfile, " || style=\"background: %s\" | ", hero[n].RBErating <= 16 ? "#ddffdd" : hero[n].RBErating <= 22 ? "#ffffdd" : "#ffdddd"); fprintf(outfile, "%d", hero[n].RBErating); fprintf(outfile, " || "); if (hero[n].melee == 1 && hero[n].ranged == 0) {			fprintf(outfile, "Melee"); } else if (hero[n].melee == 0 && hero[n].ranged == 1) { fprintf(outfile, "Ranged"); } else { fprintf(outfile, "???"); }		fprintf(outfile, " || "); if (hero[n].agility == 1 && hero[n].intelligence == 0 && hero[n].strength == 0) {			fprintf(outfile, "Agi "); } else if (hero[n].agility == 0 && hero[n].intelligence == 1 && hero[n].strength == 0) { fprintf(outfile, "Int "); } else if (hero[n].agility == 0 && hero[n].intelligence == 0 && hero[n].strength == 1) { fprintf(outfile, "Str "); } else { fprintf(outfile, "???"); }		fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].carry ? "car" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].disabler ? "dis" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].durable ? "dur" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].escape ? "esc" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].initiator ? "ini" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].jungler ? "jun" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].lanesupport ? "lan" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].nuker ? "nuk" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].pusher ? "pus" : ""); fprintf(outfile, " || "); fprintf(outfile, "%s", hero[n].support ? "sup" : ""); fprintf(outfile, "\n|-\n"); }	fprintf(outfile, "|}\n"); return 0; }