User:Fizz/Hero Parser.c


 * 1) include 
 * 2) include 


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

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

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; 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) {		printf("Error opening input file %s\n", argv[1]); return 2; }	for (n = 0; n < HEROLIMIT; n++) {		mod_strncpy(hero[n].name, "Unknown", NAMELIMIT); hero[n].difficulty = 0; 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 (strcmp(token, "0") == 0) {				hero[n].difficulty = 0; } else if (strcmp(token, "1") == 0) { hero[n].difficulty = 1; } else if (strcmp(token, "2") == 0) { hero[n].difficulty = 2; } else if (strcmp(token, "3") == 0) { hero[n].difficulty = 3; } else if (strcmp(token, "4") == 0) { hero[n].difficulty = 4; } 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; printf("{| class=\"wikitable sortable\"\n|-\n! Hero !! Diff !! Type !! Att !! Car !! Dis !! Dur !! Esc !! Ini !! Jun !! Lan !! Nuk !! Pus !! Sup\n|-\n"); for (n = 0; n < heroes; n++) {		printf("| "); printf("%s", hero[n].name); printf(" || "); printf("%d", hero[n].difficulty); printf(" || "); if (hero[n].melee == 1 && hero[n].ranged == 0) {			printf("melee"); } else if (hero[n].melee == 0 && hero[n].ranged == 1) { printf("ranged"); } else { printf("???"); }		printf(" || "); if (hero[n].agility == 1 && hero[n].intelligence == 0 && hero[n].strength == 0) {			printf("AGI "); } else if (hero[n].agility == 0 && hero[n].intelligence == 1 && hero[n].strength == 0) { printf("INT "); } else if (hero[n].agility == 0 && hero[n].intelligence == 0 && hero[n].strength == 1) { printf("STR "); } else { printf("???"); }		printf(" || "); printf("%s", hero[n].carry ? "car" : ""); printf(" || "); printf("%s", hero[n].disabler ? "dis" : ""); printf(" || "); printf("%s", hero[n].durable ? "dur" : ""); printf(" || "); printf("%s", hero[n].escape ? "esc" : ""); printf(" || "); printf("%s", hero[n].initiator ? "ini" : ""); printf(" || "); printf("%s", hero[n].jungler ? "jun" : ""); printf(" || "); printf("%s", hero[n].lanesupport ? "lan" : ""); printf(" || "); printf("%s", hero[n].nuker ? "nuk" : ""); printf(" || "); printf("%s", hero[n].pusher ? "pus" : ""); printf(" || "); printf("%s", hero[n].support ? "sup" : ""); printf("\n|-\n"); }	printf("|}\n"); return 0; }