// // File: wordParse.c // wordParse.c builds a table of words for pass() to use. It either uses // predefined words, or words read in from the config file. // @author Joshua Bicking // // // // // // // // // // // // // // // // // // // // // // // #include "getline.h" #include #include #include #include "wordParse.h" #include char **wordArr; int numWords; int wordsToChoose; char * victoryProg; char * completeProg; void readWordsFromFile(FILE* fp){ // If there's no config file, default to very easy if(fp == NULL) { setVeryEasy(); return; } // Check each line for valid words. Stop once launching is reached. char * buf; size_t n = 0; numWords = 0; while(getline(&buf, &n, fp)){ if(buf[0] == '#' || !strcmp(buf, "\n")) continue; // Remove the \n at the end of buff buf[strlen(buf)-1] = '\0'; // Stop on :LAUNCH_ON_* if(buf[0] == ':' ) { if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) { sscanf(buf+17, "%d", &wordsToChoose); continue; } else break; } // Check all chars in buf are A-Z or a-z int invalidChars = 0; for(int i=0; (unsigned long)i 64 && buf[i] < 91) continue; else if(buf[i] > 96 && buf[i] < 123) buf[i] = toupper(buf[i]); else invalidChars = 1; } if(invalidChars) continue; // Check all words are the same size as the first word if(numWords != 0) { if(strlen(buf) != strlen(*wordArr)) continue; // Check the word doesn't already exist in the list int dupes = 0; for(int i=0; i < numWords; i++) if(!strcmp(*(wordArr+i), buf)) dupes = 1; if(dupes) continue; } // Read the word into wordArr wordArr = realloc(wordArr, sizeof(char*) * (numWords+1)); *(wordArr+numWords) = malloc(sizeof(char) * (strlen(buf)+1)); strcpy(*(wordArr+numWords),buf); numWords++; } // If words to choose wasn't specified or found, default to 7 if(wordsToChoose == 0) wordsToChoose = 7; if(wordsToChoose > numWords){ // Too few valid words recieved: default to very easy free(wordArr); setVeryEasy(); } free(buf); } void readLaunches(FILE* fp){ // Rewind the file rewind(fp); // If the file doesn't exist, stop if(fp == NULL){ return; } char * buf; size_t n = 0; // Look or the parameters. Stop once LAUNCH_ON_COMPLETE is read. while(getline(&buf, &n, fp)){ // Remove the \n at the end of buff buf[strlen(buf)-1] = '\0'; // Search for :LAUNCH_ON_* if(!strncmp(buf, ":LAUNCH_ON_VICTORY=", 19) && victoryProg == NULL) { victoryProg = malloc(sizeof(char) * strlen(buf)-19+1); strcpy(victoryProg, buf+19); } if(!strncmp(buf, ":LAUNCH_ON_COMPLETE=", 20) && completeProg == NULL) { completeProg = malloc(sizeof(char) * strlen(buf)-20+1); strcpy(completeProg, buf+20); break; } } free(buf); } void setWordArr(char *words[]){ wordArr = malloc(numWords * sizeof(char*)); for(int i=0; i