X-Git-Url: https://git.cworth.org/git?p=mnemon;a=blobdiff_plain;f=mnemon.c;h=ea2e0a1e06c5ecbf1d18da1943cb09ea1ec58871;hp=c0efa6902e50281e91a5d4cc699ab40a6041bd70;hb=a2a91a3fa4d6cbbfb52b4d6e2233cc39f2488865;hpb=640321106d0b60f1f9ed408537691b90a768a651 diff --git a/mnemon.c b/mnemon.c index c0efa69..ea2e0a1 100644 --- a/mnemon.c +++ b/mnemon.c @@ -379,6 +379,16 @@ mnemon_get_bin (mnemon_t *mnemon, return bin; } +static void +mnemon_remove_bin (mnemon_t *mnemon, + bin_t *bin) +{ + int i = bin - mnemon->bins; + + memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t)); + mnemon->num_bins--; +} + static void chomp (char *s) { @@ -533,7 +543,7 @@ mnemon_save (mnemon_t *mnemon) err = rename (lock_filename, filename); if (err < 0) { - fprintf (stderr, "Error: Failes to rename %s to %s: %s\n", + fprintf (stderr, "Error: Failed to rename %s to %s: %s\n", lock_filename, filename, strerror (errno)); continue; } @@ -627,26 +637,34 @@ mnemon_do_challenges (mnemon_t *mnemon) printf ("%s\n", item->challenge); response = readline ("> "); - if (response == NULL) + if (response == NULL) { + printf ("\n"); break; + } correct = (strcmp (response, item->response) == 0); bin_remove_item (bin, item_index); + if (bin->num_items == 0) + mnemon_remove_bin (mnemon, bin); if (correct) { - printf ("Correct!\n\n"); - if (item->count < 0) + printf ("Correct! (Moving from %d to ", item->count); + item->count++; + /* We reserve an item count of 0 for an item that has + * never been asked. */ + if (item->count == 0) item->count = 1; - else - item->count++; + printf ("%d)\n\n", item->count); } else { - printf (" %s is the correct answer.\n\n", - item->response); - if (item->count > 0) + printf (" %s is the correct answer. (Moving from %d to ", + item->response, item->count); + item->count--; + /* Penalize an incorrect response by forcing the count + * negative. */ + if (item->count >= 0) item->count = -1; - else - item->count--; + printf ("%d)\n\n", item->count); } bin = mnemon_get_bin (mnemon, item->count); @@ -660,7 +678,7 @@ main (int argc, char *argv[]) { mnemon_t mnemon; - srand (time (NULL)); + srand (1); mnemon_init (&mnemon);