123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- //
- // 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 <josh1147582>
- // // // // // // // // // // // // // // // // // // // // // // //
- #include "getline.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "wordParse.h"
- #include <ctype.h>
- char **wordArr;
- int numWords;
- int wordsToChoose;
- char * victoryProg;
- char * completeProg;
- void readWordsFromFile(FILE* fp){
- if(fp == NULL) {
- setVeryEasy();
- return;
- }
- // Check if FalloutTerminal.cfg is invalid
- 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<strlen(buf); i++){
- if(buf[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(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(fp);
- if(fp == NULL){
- return;
- }
- char * buf;
- size_t n = 0;
- 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<numWords; i++) {
- // TODO replace numWords with wordsize
- *(wordArr+i) = malloc(sizeof(char) * numWords);
- strcpy(*(wordArr+i), *(words+i));
- }
- }
- void setVeryEasy() {
- char * words[] = {
- "FRIED",
- "TREES",
- "RIGID",
- "HIRED",
- "TRIES",
- "WRITE",
- "TRIED",
- "GREED",
- "DRIED",
- "BRAIN",
- "SKIES",
- "LAWNS",
- "GHOST",
- "CAUSE",
- "PAINT",
- "SHINY",
- "MAKES",
- "GAINS",
- "THIEF",
- "BASES",
- "RAISE",
- "REFER",
- "CARES",
- "TAKEN",
- "WAKES",
- "WAVES",
- "WARNS",
- "SAVES"
- };
- numWords = 28;
- wordsToChoose = 10;
- setWordArr(words);
- }
- void setEasy() {
- char * words[] = {
- "STATING",
- "HEALING",
- "COSTING",
- "REASONS",
- "SEASIDE",
- "SPARING",
- "CAUSING",
- "CRAFTED",
- "PRISONS",
- "PRESENT",
- "DEALING",
- "SETTING",
- "LEAVING",
- "VERSION",
- "DEATHLY",
- "BLAZING",
- "GRANITE",
- "TESTING",
- "TRAITOR",
- "STAMINA",
- "TRINITY",
- "CALLING",
- "TALKING",
- "ACQUIRE",
- "WELCOME",
- "DECRIES",
- "FALLING",
- "PACKING",
- "ALLOWED",
- "SELLING",
- "AFFRONT",
- "WALKING"
- };
- numWords = 32;
- wordsToChoose = 11;
- setWordArr(words);
- }void setAverage() {
- char * words[] = {
- "CONQUORER",
- "CONSISTED",
- "WONDERFUL",
- "COMMITTEE",
- "SURRENDER",
- "SUBJECTED",
- "CONVICTED",
- "FORBIDDEN",
- "FORTIFIED",
- "COLLECTED",
- "CONTINUED",
- "PERIMETER",
- "SOUTHEAST",
- "RELEASING",
- "SOMETHING",
- "ACCEPTING",
- "MUTATIONS",
- "GATHERING",
- "LITERALLY",
- "REPAIRING",
- "INCESSANT",
- "INTERIORS",
- "REGARDING",
- "TELEPHONE",
- "OBTAINING",
- "EXTENSIVE",
- "DEFEATING",
- "REQUIRING",
- "UNLOCKING",
- "RECYCLING",
- "INSTINCTS",
- "BARTERING",
- "LEUTENANT",
- "COMMUNITY",
- "BATTERIES",
- "RECIEVING",
- "INCLUDING",
- "INITIALLY",
- "INVOLVING",
- "MOUNTAINS"
- };
- numWords = 40;
- wordsToChoose = 14;
- setWordArr(words);
- }void setHard() {
- char * words[] = {
- "DISCOVERING",
- "ELIMINATING",
- "UNIMPORTANT",
- "MISTRUSTING",
- "MANUFACTURE",
- "RADIOACTIVE",
- "EXCLUSIVELY",
- "BOMBARDMENT",
- "DECEPTIVELY",
- "INDEPENDENT",
- "UNBELIEVERS",
- "EFFECTIVELY",
- "IMMEDIATELY",
- "INFESTATION",
- "DESCRIPTION",
- "INFORMATION",
- "REMEMBERING",
- "NIGHTVISION",
- "DESTRUCTION",
- "OVERLOOKING"
- };
- numWords = 20;
- wordsToChoose = 7;
- setWordArr(words);
- }void setVeryHard() {
- char * words[] = {
- "INFILTRATION",
- "ORGANIZATION",
- "AUTHENTICITY",
- "APPRECIATION",
- "SPOKESPERSON",
- "LABORATORIES",
- "INITIATEHOOD",
- "SUBTERRANEAN",
- "PURIFICATION",
- "TRANSMISSION",
- "CIVILIZATION",
- "CONSTRUCTION",
- "RESURRECTION",
- "REPRIMANDING",
- "ACCOMPANYING",
- "OVERWHELMING",
- "CONVERSATION",
- "NORTHERNMOST",
- "TRANSCRIBING",
- "ANNOUNCEMENT",
- "SECLUTIONIST"
- };
- numWords = 21;
- wordsToChoose = 13;
- setWordArr(words);
- }
- char ** getWordArr(){
- return wordArr;
- }
- int getNumWords() {
- return numWords;
- }
- int getWordsToChoose() {
- return wordsToChoose;
- }
- int getWordLength() {
- return strlen(*wordArr);
- }
- char * getVictoryProg() {
- if(victoryProg == NULL)
- return "";
- return victoryProg;
- }
- char * getCompleteProg() {
- if(completeProg == NULL)
- return "";
- return completeProg;
- }
- void freeAll() {
- for(int i=0; i<numWords; i++)
- free(*(wordArr+i));
- free(wordArr);
- free(victoryProg);
- free(completeProg);
- }
- // end
|