]> git.cworth.org Git - mnemon/commitdiff
Remove the enumeration of challenge types.
authorCarl Worth <cworth@cworth.org>
Mon, 26 Sep 2011 05:11:04 +0000 (22:11 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 26 Sep 2011 05:11:04 +0000 (22:11 -0700)
With the move toward a more general library, I'd like each application
to be able to support its own challenge types, (without the library
being aware of them). We move toward that by having the library simply
store and save a string for the challenge type.

A next step will include the library providing support to load only
categories of a particular challenge type.

main.c
mnemon.c
mnemon.h

diff --git a/main.c b/main.c
index b583d394b7010724862b8f33a803b90b29652268..819878919690335901fa2b9d58d9e02a74d55fe8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -84,13 +84,13 @@ xasprintf (char **strp, const char *fmt, ...)
 
 static void
 _show_challenge (mnemon_t *mnemon,
-                challenge_type_t challenge_type,
+                const char *challenge_type,
                 const char *challenge)
 {
     const char *program;
     char *command;
 
-    if (challenge_type == CHALLENGE_TYPE_TEXT) {
+    if (strcmp (challenge_type, "text") == 0) {
        printf ("%s\n", challenge);
        return;
     }
@@ -100,22 +100,18 @@ _show_challenge (mnemon_t *mnemon,
      * have a custom external program that accepts image filenames on
      * stdin.
      */
-    switch (challenge_type) {
-    case CHALLENGE_TYPE_TEXT:
-       ASSERT_NOT_REACHED;
-       break;
-    case CHALLENGE_TYPE_IMAGE:
+    if (strcmp (challenge_type, "image") == 0) {
        program = "xli -gamma 2.2";
-       break;
-    case CHALLENGE_TYPE_AUDIO:
+    } else if (strcmp (challenge_type, "audio") == 0) {
        program = "play";
-       break;
-    case CHALLENGE_TYPE_MIDI:
+    } else if (strcmp (challenge_type, "midi") == 0) {
        program = "timidity -Os";
-       break;
-    case CHALLENGE_TYPE_TEXT_TO_SPEECH:
+    } else if (strcmp (challenge_type, "text-to-speech") == 0) {
        program = "mnemon-tts";
-       break;
+    } else {
+       fprintf (stderr, "Error: unsupported challenge type: %s\n",
+                challenge_type);
+       exit (1);
     }
 
     xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
@@ -128,11 +124,11 @@ _show_challenge (mnemon_t *mnemon,
 
 static void
 _hide_challenge (unused (mnemon_t *mnemon),
-                challenge_type_t challenge_type)
+                const char *challenge_type)
 {
     char * command;
 
-    if (challenge_type != CHALLENGE_TYPE_IMAGE)
+    if (strcmp (challenge_type, "image"))
        return;
 
     /* XXX: And this is just embarrassing (obviously wrong in several
index 0dc45380760d3d3592a9c875a11b343f100d50eb..7b057db25a5c44c18cddd22aa07529951442dbab 100644 (file)
--- a/mnemon.c
+++ b/mnemon.c
@@ -139,7 +139,7 @@ category_init (category_t *category,
     category->order = CATEGORY_ORDER_RANDOM;
     category->time_limit = 0.0;
     category->bin_zero_head = 0;
-    category->challenge_type = CHALLENGE_TYPE_TEXT;
+    category->challenge_type = xstrdup("");
     category->repeat = 0;
 }
 
@@ -154,6 +154,8 @@ category_fini (category_t *category)
     free (category->items);
 
     free (category->name);
+
+    free (category->challenge_type);
 }
 
 static void
@@ -210,25 +212,7 @@ category_print (category_t *category,
     fprintf (file, "time = %f\n\n",
             category->time_limit);
 
-    fprintf (file, "challenge = ");
-    switch (category->challenge_type) {
-    case CHALLENGE_TYPE_TEXT:
-       fprintf (file, "text");
-       break;
-    case CHALLENGE_TYPE_IMAGE:
-       fprintf (file, "image");
-       break;
-    case CHALLENGE_TYPE_AUDIO:
-       fprintf (file, "audio");
-       break;
-    case CHALLENGE_TYPE_MIDI:
-       fprintf (file, "midi");
-       break;
-    case CHALLENGE_TYPE_TEXT_TO_SPEECH:
-       fprintf (file, "text-to-speech");
-       break;
-    }
-    fprintf (file, "\n\n");
+    fprintf (file, "challenge = %s\n\n", category->challenge_type);
 
     fprintf (file, "repeat = %d\n\n", category->repeat);
 
@@ -575,21 +559,9 @@ mnemon_load_category (mnemon_t             *mnemon,
                exit (1);
            }
        } else if (strcmp (name, "challenge") == 0) {
-           if (strcmp (value, "text") == 0) {
-               category->challenge_type = CHALLENGE_TYPE_TEXT;
-           } else if (strcmp (value, "image") == 0) {
-               category->challenge_type = CHALLENGE_TYPE_IMAGE;
-           } else if (strcmp (value, "audio") == 0) {
-               category->challenge_type = CHALLENGE_TYPE_AUDIO;
-           } else if (strcmp (value, "midi") == 0) {
-               category->challenge_type = CHALLENGE_TYPE_MIDI;
-           } else if (strcmp (value, "text-to-speech") == 0) {
-               category->challenge_type = CHALLENGE_TYPE_TEXT_TO_SPEECH;
-           } else {
-               fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
-                        value, path, line_count);
-               exit (1);
-           }
+           /* XXX: Need to switch to talloc here. */
+           free (category->challenge_type);
+           category->challenge_type = xstrdup (value);
        } else if (strcmp (name, "repeat") == 0) {
            if (strcmp (value, "0") == 0) 
                category->repeat = 0;
index 396c9951c9682f0aa23b517142a4fdd1b8bb868f..b66bb44ec6d4ca7d1449fb197f583933e868afb8 100644 (file)
--- a/mnemon.h
+++ b/mnemon.h
@@ -42,14 +42,6 @@ typedef enum {
     CATEGORY_ORDER_SEQUENTIAL
 } category_order_t;
 
-typedef enum {
-    CHALLENGE_TYPE_TEXT,
-    CHALLENGE_TYPE_IMAGE,
-    CHALLENGE_TYPE_AUDIO,
-    CHALLENGE_TYPE_MIDI,
-    CHALLENGE_TYPE_TEXT_TO_SPEECH
-} challenge_type_t;
-
 typedef struct _category {
     char *name;
     int items_size;
@@ -62,7 +54,7 @@ typedef struct _category {
     double time_limit;
     int bin_zero_head;
     /* Support challenges of non-text types (image, audio, etc.) */
-    challenge_type_t challenge_type;
+    char *challenge_type;
     /* Whether to repeat afterwards (for a little extra reinforcement) */
     bool_t repeat;
 } category_t;