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

struct hero_struct { char name[NAMELIMIT]; int wikirating; 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; 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].wikirating = -1; 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) == 1 && isdigit(token[0])) {				hero[n].wikirating = atoi(token); } else 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; printf("{| class=\"wikitable sortable\"\n|-\n! Hero !! Wiki !! RBE !! 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].wikirating); printf(" || style=\"background: %s\" | ", hero[n].RBErating <= 16 ? "#ddffdd" : hero[n].RBErating <= 22 ? "#ffffdd" : "#ffdddd"); printf("%d", hero[n].RBErating); 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; }