-/*
- * Copyright © 2006 Carl Worth
+/* mnemon - A memory training library
+ *
+ * Copyright © 2006,2011 Carl Worth
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
+ * the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
*/
+#include "mnemon.h"
+
/* for asprintf */
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
assert (NOT_REACHED); \
} while (0)
+#define unused(foo) foo __attribute__((unused))
+
typedef int bool_t;
typedef struct _item {
char *response;
} item_t;
-typedef struct _bin {
+struct _bin {
int score;
int items_size;
int num_items;
item_t **items;
-} bin_t;
+};
typedef enum {
CATEGORY_ORDER_RANDOM,
CHALLENGE_TYPE_TEXT_TO_SPEECH
} challenge_type_t;
-typedef struct _category {
+struct _category {
char *name;
int items_size;
int num_items;
challenge_type_t challenge_type;
/* Whether to repeat afterwards (for a little extra reinforcement) */
bool_t repeat;
-} category_t;
-
-typedef struct _mnemon {
- char *dir_name;
-
- int categories_size;
- int num_categories;
- category_t *categories;
-
- int bins_size;
- int num_bins;
- bin_t *bins;
-
- int to_introduce;
- int to_master;
- int unlearned;
- int mastered;
-} mnemon_t;
+};
static void *
xmalloc (size_t size)
return num_items;
}
-static void
+void
mnemon_init (mnemon_t *mnemon)
{
char *home;
mnemon->mastered = -1;
}
-static void
+void
mnemon_fini (mnemon_t *mnemon)
{
int i;
return string;
}
-static void
+void
mnemon_load_category (mnemon_t *mnemon,
const char *name)
{
char *path;
category_t *category;
int i;
+ struct stat st;
path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
sprintf (path, "%s/%s", mnemon->dir_name, name);
exit (1);
}
+ fstat (fileno(file), &st);
+ if (! S_ISREG(st.st_mode)) {
+ fprintf (stderr, "Error: File %s is not a regular file.\n", path);
+ exit (1);
+ }
+
category = mnemon_get_category (mnemon, name);
#define READ_LINE do { \
}
}
-static void
+void
mnemon_load (mnemon_t *mnemon)
{
DIR *dir;
closedir (dir);
}
-static void
+void
mnemon_save (mnemon_t *mnemon)
{
int i, err;
item_in_category_of_length_closure_t *iicolc = closure;
mnemon_t *mnemon = iicolc->mnemon;
category_t *category = iicolc->category;
- int length = iicolc->length;
+ unsigned int length = iicolc->length;
if (mnemon_item_category (mnemon, item) != category)
return 0;
printf ("\n");
}
-static void
+void
mnemon_print_histogram (mnemon_t *mnemon,
const char *category_name,
int length)
}
static void
-mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
+mnemon_hide_challenge (unused (mnemon_t *mnemon),
+ challenge_type_t challenge_type)
{
char * command;
free (command);
}
-static void
+void
mnemon_do_challenges (mnemon_t *mnemon)
{
bin_t *bin;
mnemon->unlearned ||
mnemon->to_master > 0);
}
-
-int
-main (int argc, char *argv[])
-{
- mnemon_t mnemon;
- char *response;
-
- srand (time (NULL));
-
- mnemon_init (&mnemon);
-
- mnemon_load (&mnemon);
-
- mnemon_do_challenges (&mnemon);
-
- mnemon_save (&mnemon);
-
- mnemon_fini (&mnemon);
-
- mnemon_init (&mnemon);
- mnemon_load (&mnemon);
-
- printf ("Great job.\nHere are your current results:\n");
- mnemon_print_histogram (&mnemon, NULL, 0);
- response = readline ("Press enter to quit.\n");
- free (response);
-
- mnemon_fini (&mnemon);
-
- return 0;
-}