Browse Source

added key configuration

Joshua Bicking 8 years ago
parent
commit
152ef5622d
3 changed files with 74 additions and 21 deletions
  1. 11 3
      FalloutTerminal.cfg
  2. 55 16
      wordParse.c
  3. 8 2
      wordParse.h

+ 11 - 3
FalloutTerminal.cfg

@@ -1,6 +1,7 @@
-# Lines that with #, and empty lines, will be ignored.
-# The config file consists of two parts:
+# Lines that start with # and empty lines will be ignored.
+# The config file consists of: 
 #   The word list
+#   The key config
 #   The ending action(s)
 # The words list starts with
 #   :WORDS_TO_CHOOSE=X
@@ -12,7 +13,12 @@
 #
 #   The words must between 5 and 12 letters.
 #
-# After the word list, there are two optional parameters
+# The key config (:KEYS=) is how you navigate the screen. It may be set as:
+#   ARROWS (for arrow keys) (default)
+#   WASD
+#   HJKL (vimlike)
+#
+# For the ending actions, there are two optional parameters
 #   :LAUNCH_ON_VICTORY="X:\path\to\program.exe"
 #   :LAUNCH_ON_COMPLETE="X:\path\to\program.exe"
 #
@@ -23,5 +29,7 @@
 :WORDS_TO_CHOOSE=7
 # WORDS GO HERE
 
+:KEYS=ARROWS
+
 :LAUNCH_ON_VICTORY=""
 :LAUNCH_ON_COMPLETE=""

+ 55 - 16
wordParse.c

@@ -21,7 +21,12 @@ int wordsToChoose;
 char * victoryProg;
 char * completeProg;
 
-void readWordsFromFile(FILE* fp){
+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) {
@@ -35,21 +40,17 @@ void readWordsFromFile(FILE* fp){
     
     numWords = 0;
 
-    while(getline(&buf, &n, fp)){
+    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';
 
-        // Stop on :LAUNCH_ON_*
-        if(buf[0] == ':' ) {
-            if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) {
-                sscanf(buf+17, "%d", &wordsToChoose);
-                continue;
-            }
-            else
-                break;
+        // 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
@@ -104,11 +105,13 @@ void readWordsFromFile(FILE* fp){
     }
 
     free(buf);
+    fclose(fp);
 }
 
-void readLaunches(FILE* fp){
-    // Rewind the file
-    rewind(fp);
+void readLaunches(){
+
+    // Reopen the file
+    FILE * fp = fopen("FalloutTerminal.cfg", "r");
 
     // If the file doesn't exist, stop
     if(fp == NULL){
@@ -118,8 +121,8 @@ void readLaunches(FILE* fp){
     char * buf;
     size_t n = 0;
 
-    // Look or the parameters. Stop once LAUNCH_ON_COMPLETE is read.
-    while(getline(&buf, &n, fp)){
+    // Look for the parameters.
+    while(getline(&buf, &n, fp) != -1){
 
         // Remove the \n at the end of buff
         buf[strlen(buf)-1] = '\0';
@@ -133,15 +136,47 @@ void readLaunches(FILE* fp){
         if(!strncmp(buf, ":LAUNCH_ON_COMPLETE=", 20) && completeProg == NULL) {
            completeProg = malloc(sizeof(char) * strlen(buf)-20+1); 
            strcpy(completeProg, buf+20);
-           break;
         }
 
     }
 
     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*));
 
@@ -379,6 +414,10 @@ char * getCompleteProg() {
     return completeProg;
 }
 
+keyConfig_type getKeyConfig(){
+    return keyConfig;
+}
+
 void freeAll() {
     for(int i=0; i<numWords; i++)
         free(*(wordArr+i));

+ 8 - 2
wordParse.h

@@ -3,9 +3,13 @@
 #ifndef FILEPARSE_H
 #define FILEPARSE_H
 
-void readWordsFromFile(FILE *fp);
+typedef enum {ARROWS, WASD, HJKL} keyConfig_type;
 
-void readLaunches(FILE* fp);
+void readWordsFromFile();
+
+void readLaunches();
+
+void readKeys();
 
 void setVeryEasy();
 
@@ -29,6 +33,8 @@ char * getVictoryProg();
 
 char * getCompleteProg();
 
+keyConfig_type getKeyConfig();
+
 void freeAll();
 
 #endif