Browse Source

reads words from the config file (if they are valid)

Joshua Bicking 8 years ago
parent
commit
63ab130d08
5 changed files with 116 additions and 29 deletions
  1. 9 5
      FalloutTerminal.cfg
  2. 16 14
      main.c
  3. 4 3
      pass.c
  4. 84 6
      wordParse.c
  5. 3 1
      wordParse.h

+ 9 - 5
FalloutTerminal.cfg

@@ -1,11 +1,13 @@
-# Lines that with # will be ignored.
+# Lines that with #, and empty lines, will be ignored.
 # The config file consists of two parts:
 #   The word list
 #   The ending action(s)
 # The words list starts with
-# :WORDS
+# :WORDS_TO_CHOOSE=X
 # And is followed by a list of words, one per line, all the same length, that
 # you'd like in the game.
+# The words must between 5 and 12 letters.
+# You must have a minimum # of words for whatever you give to WORDS_TO_CHOOSE (default 7)
 #
 # After the word list, there are two optional parameters
 # :LAUNCH_ON_VICTORY="X:\path\to\program.exe"
@@ -13,10 +15,12 @@
 #
 # If declared, they will launch the program specified when the game
 # completes/is won, respectively.
+# If you don't want to use them, just leave them blank.
 
-:WORDS
+:WORDS_TO_CHOOSE=7
 # WORDS GO HERE
 
-:LAUNCH_ON_VICTORY="X:\path\to\program.exe"
-:LAUNCH_ON_COMPLETE="X:\path\to\program.exe"
+
+:LAUNCH_ON_VICTORY=""
+:LAUNCH_ON_COMPLETE=""
 

+ 16 - 14
main.c

@@ -26,19 +26,6 @@ int main(int argc, char * argv[]){
         exit(0);
     }
 
-    srand ( (unsigned)time(NULL) );
-    initscr();
-    noecho();
-    refresh();
-    attron(A_BOLD);
-    nodelay(stdscr, 1);
-    if(has_colors() == 1){
-        /* Colors */
-        start_color();
-        init_pair(1,COLOR_GREEN,COLOR_BLACK);
-        attron(COLOR_PAIR(1));
-    }
-
     FILE *fp = NULL;
 
     if(argc > 1){
@@ -62,10 +49,25 @@ int main(int argc, char * argv[]){
 
         fp = fopen("FalloutTerminal.cfg", "r");
 
-        readFile(fp);
+        readWordsFromFile(fp);
+    }
+
+
+    srand ( (unsigned)time(NULL) );
+    initscr();
+    noecho();
+    refresh();
+    attron(A_BOLD);
+    nodelay(stdscr, 1);
+    if(has_colors() == 1){
+        /* Colors */
+        start_color();
+        init_pair(1,COLOR_GREEN,COLOR_BLACK);
+        attron(COLOR_PAIR(1));
     }
 
 
+
     intro();
     pass();
 

+ 4 - 3
pass.c

@@ -219,7 +219,7 @@ void pass(){
                     mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
                     refresh();
                     getch();
-                    exit(0);
+                    exit(EXIT_FAILURE);
         }
         refresh();
         move(y,x);
@@ -521,7 +521,7 @@ void pass(){
                     refresh();
                     SLEEP(3000000);
                     endwin();
-                    exit(0);
+                    exit(EXIT_SUCCESS);
                     
                 }
                 else{
@@ -549,5 +549,6 @@ void pass(){
     }
 
     endwin();
-    exit(0);
+    freeWordArr();
+    exit(EXIT_SUCCESS);
 }

+ 84 - 6
wordParse.c

@@ -1,13 +1,18 @@
 // 
 // 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 <stdlib.h>
 #include <string.h>
 #include "wordParse.h"
+#include <ctype.h>
+
+#define MAX_WORD_SIZE 12
 
 char **wordArr;
 
@@ -15,23 +20,92 @@ int numWords;
 
 int wordsToChoose;
 
-void readFile(FILE* fp){
+void readWordsFromFile(FILE* fp){
 
     if(fp == NULL) {
         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[]){
     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));
     }
@@ -253,4 +327,8 @@ int getWordLength() {
     return strlen(*wordArr);
 }
 
+void freeWordArr() {
+    free(wordArr);
+}
+
 // end

+ 3 - 1
wordParse.h

@@ -3,7 +3,7 @@
 #ifndef FILEPARSE_H
 #define FILEPARSE_H
 
-void readFile(FILE *fp);
+void readWordsFromFile(FILE *fp);
 
 void setVeryEasy();
 
@@ -23,4 +23,6 @@ int getWordsToChoose();
 
 int getWordLength();
 
+void freeWordArr();
+
 #endif