Browse Source

added program launching after victory/completion. works fine on Linux. still need to test it a bit on other platforms

Joshua Bicking 8 years ago
parent
commit
a230bf4126
5 changed files with 95 additions and 21 deletions
  1. 11 10
      FalloutTerminal.cfg
  2. 12 7
      main.c
  3. 12 2
      pass.c
  4. 53 1
      wordParse.c
  5. 7 1
      wordParse.h

+ 11 - 10
FalloutTerminal.cfg

@@ -3,24 +3,25 @@
 #   The word list
 #   The ending action(s)
 # The words list starts with
-# :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)
+#   :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.
+#
+#   You must have a minimum # of words for whatever you give to WORDS_TO_CHOOSE
+#   (default 7)
+#
+#   The words must between 5 and 12 letters.
 #
 # After the word list, there are two optional parameters
-# :LAUNCH_ON_VICTORY="X:\path\to\program.exe"
-# :LAUNCH_ON_COMPLETE="X:\path\to\program.exe"
+#   :LAUNCH_ON_VICTORY="X:\path\to\program.exe"
+#   :LAUNCH_ON_COMPLETE="X:\path\to\program.exe"
 #
 # 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.
+#   If you don't want to use them, just leave them blank.
 
 :WORDS_TO_CHOOSE=7
 # WORDS GO HERE
 
-
 :LAUNCH_ON_VICTORY=""
 :LAUNCH_ON_COMPLETE=""
-

+ 12 - 7
main.c

@@ -22,13 +22,21 @@ int main(int argc, char * argv[]){
                 "--average,\t14 words, 9 letters per word\n\n"
                 "--hard,\t\t7 words, 11 letters per word\n\n"
                 "--veryHard,\t13 words, 12 letters per word\n\n"
+                "If no difficulty is provided, this program will read input "
+                "from the FalloutTerminal.cfg file. If this file cannot be found "
+                "or the configuration is invalid, it will default to Very Easy." 
                 , argv[0]);
         exit(0);
     }
 
     FILE *fp = NULL;
 
+    fp = fopen("FalloutTerminal.cfg", "r");
+
     if(argc > 1){
+        if(!strcmp(argv[1], "--veryEasy")) {
+            setVeryEasy();
+        }
         if(!strcmp(argv[1], "--easy")) {
            setEasy();
         } 
@@ -42,16 +50,15 @@ int main(int argc, char * argv[]){
            setVeryHard();
         } 
         else {
-            setVeryEasy();
+            printf("Invalid command. Type \"%s --help\" for usage and a list of commands.\n", argv[0]);
+            exit(EXIT_FAILURE);
         }
     }
     else {    
-
-        fp = fopen("FalloutTerminal.cfg", "r");
-
         readWordsFromFile(fp);
     }
 
+    readLaunches(fp);
 
     srand ( (unsigned)time(NULL) );
     initscr();
@@ -71,9 +78,7 @@ int main(int argc, char * argv[]){
     intro();
     pass();
 
-    if(fp != NULL) {
-        fclose(fp);
-    }
+    fclose(fp);
 
     return 0;
 }

+ 12 - 2
pass.c

@@ -218,7 +218,11 @@ void pass(){
                     mvprintw(10,20,"TERMINAL LOCKED");
                     mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
                     refresh();
-                    getch();
+                    SLEEP(3000000);
+                    endwin();
+                    if(strlen(getCompleteProg())> 2)
+                        system(getCompleteProg());
+                    freeAll();
                     exit(EXIT_FAILURE);
         }
         refresh();
@@ -521,6 +525,12 @@ void pass(){
                     refresh();
                     SLEEP(3000000);
                     endwin();
+                    if(strlen(getVictoryProg()) > 2)
+                        system(getVictoryProg());
+                    else if(strlen(getCompleteProg())> 2)
+                        system(getCompleteProg());
+ 
+                    freeAll();
                     exit(EXIT_SUCCESS);
                     
                 }
@@ -549,6 +559,6 @@ void pass(){
     }
 
     endwin();
-    freeWordArr();
+    freeAll();
     exit(EXIT_SUCCESS);
 }

+ 53 - 1
wordParse.c

@@ -20,6 +20,9 @@ int numWords;
 
 int wordsToChoose;
 
+char * victoryProg;
+char * completeProg;
+
 void readWordsFromFile(FILE* fp){
 
     if(fp == NULL) {
@@ -99,6 +102,41 @@ void readWordsFromFile(FILE* fp){
         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[]){
@@ -327,8 +365,22 @@ int getWordLength() {
     return strlen(*wordArr);
 }
 
-void freeWordArr() {
+char * getVictoryProg() {
+    if(victoryProg == NULL)
+        return "";
+    return victoryProg;
+}
+
+char * getCompleteProg() {
+    if(completeProg == NULL)
+        return "";
+    return completeProg;
+}
+
+void freeAll() {
     free(wordArr);
+    free(victoryProg);
+    free(completeProg);
 }
 
 // end

+ 7 - 1
wordParse.h

@@ -5,6 +5,8 @@
 
 void readWordsFromFile(FILE *fp);
 
+void readLaunches(FILE* fp);
+
 void setVeryEasy();
 
 void setEasy();
@@ -23,6 +25,10 @@ int getWordsToChoose();
 
 int getWordLength();
 
-void freeWordArr();
+char * getVictoryProg();
+
+char * getCompleteProg();
+
+void freeAll();
 
 #endif