From 71edc226d9b568d9740c54ea73005d8baea1ca8d Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 25 Sep 2011 22:11:04 -0700 Subject: [PATCH] Remove the enumeration of challenge types. 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 | 28 ++++++++++++---------------- mnemon.c | 42 +++++++----------------------------------- mnemon.h | 10 +--------- 3 files changed, 20 insertions(+), 60 deletions(-) diff --git a/main.c b/main.c index b583d39..8198789 100644 --- 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 diff --git a/mnemon.c b/mnemon.c index 0dc4538..7b057db 100644 --- 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; diff --git a/mnemon.h b/mnemon.h index 396c995..b66bb44 100644 --- 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; -- 2.43.0