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 mnemon_bins_grow (mnemon);
371 bin = &mnemon->bins[i];
373 /* Make room to insert new bin at its sorted location. */
374 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
377 bin_init (bin, count);
383 mnemon_remove_bin (mnemon_t *mnemon,
386 int i = bin - mnemon->bins;
388 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
395 int len = strlen (s);
398 if (s[len - 1] == '\n')
403 mnemon_load_category (mnemon_t *mnemon,
407 char *line = NULL, *end;
408 size_t line_size = 0;
412 category_t *category;
415 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
416 sprintf (path, "%s/%s", mnemon->dir_name, name);
418 file = fopen (path, "r");
420 fprintf (stderr, "Error: Failed to open %s: %s\n",
421 path, strerror (errno));
425 category = mnemon_get_category (mnemon, name);
429 char *challenge, *response;
431 /* Read bin number (ignoring blank separator lines) */
433 bytes_read = getline (&line, &line_size, file);
434 if (bytes_read == -1)
438 } while (*line == '\0');
440 count = strtol (line, &end, 10);
442 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
443 line, path, line_count);
448 bytes_read = getline (&line, &line_size, file);
449 if (bytes_read == -1)
453 challenge = strdup (line);
456 bytes_read = getline (&line, &line_size, file);
457 if (bytes_read == -1)
463 category_add_item (category, count, challenge, response);
473 /* Resize category items to fit exactly. */
474 category->items_size = category->num_items;
475 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
477 /* Now that the category is completely loaded, with stable
478 * pointers to every item, we can add each item to its appropriate
480 for (i = 0; i < category->num_items; i++) {
481 item_t *item = &category->items[i];
482 bin_t *bin = mnemon_get_bin (mnemon, item->count);
484 bin_add_item (bin, item);
489 mnemon_load (mnemon_t *mnemon)
492 struct dirent *dirent;
494 dir = opendir (mnemon->dir_name);
496 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
497 mnemon->dir_name, strerror (errno));
502 dirent = readdir (dir);
506 if (dirent->d_type == DT_REG) {
507 /* Ignore files matching *~, (yes, this shouldn't be
508 * hard-coded in such an ad-hoc way, but there you go. */
509 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
510 mnemon_load_category (mnemon, dirent->d_name);
518 mnemon_save (mnemon_t *mnemon)
521 char *filename, *lock_filename;
523 category_t *category;
525 for (i = 0; i < mnemon->num_categories; i++) {
526 category = &mnemon->categories[i];
528 xasprintf (&filename, "%s/%s",
529 mnemon->dir_name, category->name);
530 xasprintf (&lock_filename, "%s/.#%s",
531 mnemon->dir_name, category->name);
533 file = fopen (lock_filename, "w");
535 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
536 lock_filename, strerror (errno));
540 category_print (category, file);
544 err = rename (lock_filename, filename);
546 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
547 lock_filename, filename, strerror (errno));
552 free (lock_filename);
556 /* Return a uniformly-distributed pseudo-random integer within the
559 * 0 <= result < num_values
562 rand_within (int num_values)
564 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
567 /* Return an exponentially-distributed pseudo-random integer within
570 * 0 <= result < num_values
572 * The distribution is such that each successively larger value will
573 * occur with a probability of half of the previous value.
576 rand_within_exponential (int num_values)
579 static uint32_t mask = 0;
583 /* Optimize the constant case. */
593 while (mask > RAND_MAX)
600 if (ones == num_values)
609 mnemon_select_item (mnemon_t *mnemon,
616 bin_index = rand_within_exponential (mnemon->num_bins);
618 bin = &mnemon->bins[bin_index];
621 *item_index_ret = rand_within (bin->num_items);
625 mnemon_do_challenges (mnemon_t *mnemon)
634 mnemon_select_item (mnemon, &bin, &item_index);
635 item = bin->items[item_index];
637 printf ("%s\n", item->challenge);
639 response = readline ("> ");
640 if (response == NULL) {
645 correct = (strcmp (response, item->response) == 0);
647 bin_remove_item (bin, item_index);
648 if (bin->num_items == 0)
649 mnemon_remove_bin (mnemon, bin);
652 printf ("Correct! (Moving from %d to ", item->count);
654 /* We reserve an item count of 0 for an item that has
655 * never been asked. */
656 if (item->count == 0)
658 printf ("%d)\n\n", item->count);
660 printf (" %s is the correct answer. (Moving from %d to ",
661 item->response, item->count);
663 /* Penalize an incorrect response by forcing the count
665 if (item->count >= 0)
667 printf ("%d)\n\n", item->count);
670 bin = mnemon_get_bin (mnemon, item->count);
672 bin_add_item (bin, item);
677 main (int argc, char *argv[])
683 mnemon_init (&mnemon);
685 mnemon_load (&mnemon);
687 mnemon_do_challenges (&mnemon);
689 mnemon_save (&mnemon);
691 mnemon_fini (&mnemon);