// // 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; keyConfig_type keyConfig = ARROWS; void readWordsFromFile(){ // Open the config file FILE * fp = fopen("FalloutTerminal.cfg", "r"); // 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) != -1){ if(buf[0] == '#' || !strcmp(buf, "\n")) continue; // Remove the \n at the end of buff buf[strlen(buf)-1] = '\0'; // Check for :WORDS_TO_CHOOSE if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) { sscanf(buf+17, "%d", &wordsToChoose); continue; } // 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); fclose(fp); } void readLaunches(){ // Reopen the file FILE * fp = fopen("FalloutTerminal.cfg", "r"); // If the file doesn't exist, stop if(fp == NULL){ return; } char * buf; size_t n = 0; // Look for the parameters. while(getline(&buf, &n, fp) != -1){ // 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); } } free(buf); fclose(fp); } void readKeys(){ // Reopen the file FILE * fp = fopen("FalloutTerminal.cfg", "r"); // If the file doesn't exist, stop if(fp == NULL){ return; } char * buf; size_t n = 0; while(getline(&buf, &n, fp) != -1){ // Remove the \n at the end of buff buf[strlen(buf)-1] = '\0'; // Search for :KEYS= if(!strncmp(buf, ":KEYS=", 6)) { if(!strcmp(buf+6,"ARROWS")) { keyConfig = ARROWS; }else if(!strcmp(buf+6,"WASD")) { keyConfig = WASD; }else if(!strcmp(buf+6,"HJKL")) { keyConfig = HJKL; } } } free(buf); fclose(fp); } void setWordArr(char *words[]){ wordArr = malloc(numWords * sizeof(char*)); for(int i=0; i