2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
26 #include <sys/types.h>
32 #include <readline/readline.h>
33 #include <readline/history.h>
37 typedef struct _item {
50 typedef struct _category {
57 typedef struct _mnemon {
62 category_t *categories;
76 fprintf (stderr, "Error: out of memory\n");
84 xrealloc (void *ptr, size_t size)
88 ret = realloc (ptr, size);
90 fprintf (stderr, "Error: out of memory\n");
98 xstrdup (const char *s)
104 fprintf (stderr, "Error: out of memory\n");
112 xasprintf (char **strp, const char *fmt, ...)
118 ret = vasprintf (strp, fmt, ap);
122 fprintf (stderr, "Error: out of memory\n");
128 item_init (item_t *item,
130 const char *challenge,
131 const char *response)
135 item->challenge = xmalloc (strlen (challenge) + 1 +
136 strlen (response) + 1);
137 item->response = item->challenge + strlen (challenge) + 1;
139 strcpy (item->challenge, challenge);
140 strcpy (item->response, response);
144 item_fini (item_t *item)
146 /* item->response shares allocation with item->challenge, so
147 * doesn't require a separate call to free */
148 free (item->challenge);
152 category_init (category_t *category,
155 category->name = xstrdup (name);
157 category->items_size = 0;
158 category->num_items = 0;
159 category->items = NULL;
163 category_fini (category_t *category)
167 for (i = 0; i < category->num_items; i++)
168 item_fini (&category->items[i]);
170 free (category->items);
172 free (category->name);
176 category_grow (category_t *category)
178 if (category->items_size)
179 category->items_size *= 2;
181 category->items_size = 1;
183 category->items = xrealloc (category->items,
184 category->items_size * sizeof (item_t));
188 category_add_item (category_t *category,
190 const char *challenge,
191 const char *response)
195 if (category->num_items == category->items_size)
196 category_grow (category);
198 item = &category->items[category->num_items++];
200 item_init (item, count, challenge, response);
206 category_print (category_t *category,
212 for (i = 0; i < category->num_items; i++) {
213 item = &category->items[i];
215 fprintf (file, "\n");
216 fprintf (file, "%d\n%s\n%s\n",
224 bin_init (bin_t *bin,
235 bin_fini (bin_t *bin)
241 bin_grow (bin_t *bin)
244 bin->items_size *= 2;
248 bin->items = xrealloc (bin->items,
249 bin->items_size * sizeof (item_t*));
253 bin_add_item (bin_t *bin,
256 assert (item->count == bin->count);
258 if (bin->num_items == bin->items_size)
261 bin->items[bin->num_items++] = item;
265 bin_remove_item (bin_t *bin,
268 /* Replace the current item with the last item, (no need to shift
269 * any more than that since we don't care about the order of the
270 * items within a bin). */
273 bin->items[item_index] = bin->items[bin->num_items];
277 mnemon_init (mnemon_t *mnemon)
281 home = getenv ("HOME");
285 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
287 mnemon->categories_size = 0;
288 mnemon->num_categories = 0;
289 mnemon->categories = NULL;
291 mnemon->bins_size = 0;
292 mnemon->num_bins = 0;
297 mnemon_fini (mnemon_t *mnemon)
301 for (i = 0; i < mnemon->num_bins; i++)
302 bin_fini (&mnemon->bins[i]);
305 for (i = 0; i < mnemon->num_categories; i++)
306 category_fini (&mnemon->categories[i]);
307 free (mnemon->categories);
309 free (mnemon->dir_name);
313 mnemon_categories_grow (mnemon_t *mnemon)
315 if (mnemon->categories_size)
316 mnemon->categories_size *= 2;
318 mnemon->categories_size = 1;
320 mnemon->categories = xrealloc (mnemon->categories,
321 mnemon->categories_size * sizeof (category_t));
325 mnemon_get_category (mnemon_t *mnemon,
329 category_t *category;
331 for (i = 0; i < mnemon->num_categories; i++)
332 if (strcmp (mnemon->categories[i].name, name) == 0)
333 return &mnemon->categories[i];
335 mnemon_categories_grow (mnemon);
337 category = &mnemon->categories[mnemon->num_categories++];
339 category_init (category, name);
345 mnemon_bins_grow (mnemon_t *mnemon)
347 if (mnemon->bins_size)
348 mnemon->bins_size *= 2;
350 mnemon->bins_size = 1;
352 mnemon->bins = xrealloc (mnemon->bins,
353 mnemon->bins_size * sizeof (bin_t));
357 mnemon_get_bin (mnemon_t *mnemon,
363 for (i = 0; i < mnemon->num_bins; i++)
364 if (mnemon->bins[i].count == count)
365 return &mnemon->bins[i];
366 else if (mnemon->bins[i].count > count)
369 if (mnemon->num_bins == mnemon->bins_size)
370 mnemon_bins_grow (mnemon);
372 bin = &mnemon->bins[i];
374 /* Make room to insert new bin at its sorted location. */
375 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
378 bin_init (bin, count);
384 mnemon_remove_bin (mnemon_t *mnemon,
387 int i = bin - mnemon->bins;
391 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
398 int len = strlen (s);
401 if (s[len - 1] == '\n')
406 mnemon_load_category (mnemon_t *mnemon,
410 char *line = NULL, *end;
411 size_t line_size = 0;
415 category_t *category;
418 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
419 sprintf (path, "%s/%s", mnemon->dir_name, name);
421 file = fopen (path, "r");
423 fprintf (stderr, "Error: Failed to open %s: %s\n",
424 path, strerror (errno));
428 category = mnemon_get_category (mnemon, name);
432 char *challenge, *response;
434 /* Read bin number (ignoring blank separator lines) */
436 bytes_read = getline (&line, &line_size, file);
437 if (bytes_read == -1)
441 } while (*line == '\0');
443 count = strtol (line, &end, 10);
445 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
446 line, path, line_count);
451 bytes_read = getline (&line, &line_size, file);
452 if (bytes_read == -1)
456 challenge = strdup (line);
459 bytes_read = getline (&line, &line_size, file);
460 if (bytes_read == -1)
466 category_add_item (category, count, challenge, response);
476 /* Resize category items to fit exactly. */
477 category->items_size = category->num_items;
478 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
480 /* Now that the category is completely loaded, with stable
481 * pointers to every item, we can add each item to its appropriate
483 for (i = 0; i < category->num_items; i++) {
484 item_t *item = &category->items[i];
485 bin_t *bin = mnemon_get_bin (mnemon, item->count);
487 bin_add_item (bin, item);
492 mnemon_load (mnemon_t *mnemon)
495 struct dirent *dirent;
497 dir = opendir (mnemon->dir_name);
499 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
500 mnemon->dir_name, strerror (errno));
505 dirent = readdir (dir);
509 if (dirent->d_type == DT_REG) {
510 /* Ignore files matching *~, (yes, this shouldn't be
511 * hard-coded in such an ad-hoc way, but there you go. */
512 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
513 mnemon_load_category (mnemon, dirent->d_name);
521 mnemon_save (mnemon_t *mnemon)
524 char *filename, *lock_filename;
526 category_t *category;
528 for (i = 0; i < mnemon->num_categories; i++) {
529 category = &mnemon->categories[i];
531 xasprintf (&filename, "%s/%s",
532 mnemon->dir_name, category->name);
533 xasprintf (&lock_filename, "%s/.#%s",
534 mnemon->dir_name, category->name);
536 file = fopen (lock_filename, "w");
538 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
539 lock_filename, strerror (errno));
543 category_print (category, file);
547 err = rename (lock_filename, filename);
549 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
550 lock_filename, filename, strerror (errno));
555 free (lock_filename);
559 /* Return a uniformly-distributed pseudo-random integer within the
562 * 0 <= result < num_values
565 rand_within (int num_values)
567 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
570 /* Return an exponentially-distributed pseudo-random integer within
573 * 0 <= result < num_values
575 * The distribution is such that each successively larger value will
576 * occur with a probability of half of the previous value.
579 rand_within_exponential (int num_values)
582 static uint32_t mask = 0;
586 /* Optimize the constant case. */
596 while (mask > RAND_MAX)
603 if (ones == num_values)
612 mnemon_select_item (mnemon_t *mnemon,
619 bin_index = rand_within_exponential (mnemon->num_bins);
621 bin = &mnemon->bins[bin_index];
624 *item_index_ret = rand_within (bin->num_items);
628 mnemon_do_challenges (mnemon_t *mnemon,
639 /* Count the number of items with negative counts. */
641 for (i = 0; i < mnemon->num_bins; i++) {
642 bin = &mnemon->bins[i];
645 unlearned += bin->num_items;
648 to_introduce -= unlearned;
649 if (to_introduce < 0)
653 printf ("You've got %d items to learn already. ", unlearned);
655 printf ("I'll introduce %d more as we go.", to_introduce);
658 printf ("Introducing %d new items.\n", to_introduce);
663 mnemon_select_item (mnemon, &bin, &item_index);
668 item = bin->items[item_index];
670 printf ("%s\n", item->challenge);
672 response = readline ("> ");
673 if (response == NULL) {
678 correct = (strcmp (response, item->response) == 0);
680 bin_remove_item (bin, item_index);
682 /* If the bin is now empty, we must remove it. Also if we just
683 * picked the last word we'll ever pick from the bin with
684 * count 0, then we can remove that as well. */
685 if (bin->num_items == 0 ||
686 (bin->count == 0 && to_introduce == 0))
688 mnemon_remove_bin (mnemon, bin);
693 /* We reserve an item count of 0 for an item that has
694 * never been asked. */
695 if (item->count == 0) {
698 printf ("You got it!");
699 } else if (item->count < 0) {
700 printf ("Yes---just give me %d more.",
702 } else if (item->count == 1) {
703 printf ("On your first try, no less!");
705 printf ("Masterful.");
708 printf (" %s is the correct answer.",
710 if (item->count >= 0)
713 /* Penalize an incorrect response by forcing the count
715 if (item->count >= 0) {
717 printf ( " Oops, you knew that, right?\n ");
723 printf ("%d to come.", to_introduce);
724 if (to_introduce && unlearned)
727 printf ("%d still unlearned.", unlearned);
728 if (to_introduce == 0 && unlearned == 0)
729 printf ("Great job!");
732 bin = mnemon_get_bin (mnemon, item->count);
734 bin_add_item (bin, item);
735 } while (unlearned || to_introduce);
739 main (int argc, char *argv[])
745 mnemon_init (&mnemon);
747 mnemon_load (&mnemon);
749 mnemon_do_challenges (&mnemon, 10);
751 mnemon_save (&mnemon);
753 mnemon_fini (&mnemon);