|
@@ -1,13 +1,18 @@
|
|
//
|
|
//
|
|
// File: wordParse.c
|
|
// File: wordParse.c
|
|
-// TODO_DOCS_ wordParse.c
|
|
|
|
-// @author Joshua Bicking <jhb2345>
|
|
|
|
|
|
+// 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>
|
|
// // // // // // // // // // // // // // // // // // // // // // //
|
|
// // // // // // // // // // // // // // // // // // // // // // //
|
|
|
|
|
|
|
|
+#define _GNU_SOURCE /* for getline() */
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <string.h>
|
|
#include "wordParse.h"
|
|
#include "wordParse.h"
|
|
|
|
+#include <ctype.h>
|
|
|
|
+
|
|
|
|
+#define MAX_WORD_SIZE 12
|
|
|
|
|
|
char **wordArr;
|
|
char **wordArr;
|
|
|
|
|
|
@@ -15,23 +20,92 @@ int numWords;
|
|
|
|
|
|
int wordsToChoose;
|
|
int wordsToChoose;
|
|
|
|
|
|
-void readFile(FILE* fp){
|
|
|
|
|
|
+void readWordsFromFile(FILE* fp){
|
|
|
|
|
|
if(fp == NULL) {
|
|
if(fp == NULL) {
|
|
setVeryEasy();
|
|
setVeryEasy();
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
- // TODO check if FalloutTerminal.cfg is invalid
|
|
|
|
- if(1){
|
|
|
|
- setVeryEasy();
|
|
|
|
|
|
+ // 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) * sizeof(buf));
|
|
|
|
+ strncpy(*(wordArr+numWords),buf, MAX_WORD_SIZE);
|
|
|
|
+
|
|
|
|
+ numWords++;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if(wordsToChoose == 0)
|
|
|
|
+ wordsToChoose = 7;
|
|
|
|
+
|
|
|
|
+ if(wordsToChoose > numWords){
|
|
|
|
+ // Too few valid words recieved: default to very easy
|
|
|
|
+ free(wordArr);
|
|
|
|
+ setVeryEasy();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
void setWordArr(char *words[]){
|
|
void setWordArr(char *words[]){
|
|
wordArr = malloc(numWords * sizeof(char*));
|
|
wordArr = malloc(numWords * sizeof(char*));
|
|
|
|
|
|
for(int i=0; i<numWords; i++) {
|
|
for(int i=0; i<numWords; i++) {
|
|
|
|
+ // TODO replace numWords with wordsize
|
|
*(wordArr+i) = malloc(sizeof(char) * numWords);
|
|
*(wordArr+i) = malloc(sizeof(char) * numWords);
|
|
strcpy(*(wordArr+i), *(words+i));
|
|
strcpy(*(wordArr+i), *(words+i));
|
|
}
|
|
}
|
|
@@ -253,4 +327,8 @@ int getWordLength() {
|
|
return strlen(*wordArr);
|
|
return strlen(*wordArr);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void freeWordArr() {
|
|
|
|
+ free(wordArr);
|
|
|
|
+}
|
|
|
|
+
|
|
// end
|
|
// end
|