Otherwise the category will keep reallocing the items array which
means the pointers to each item won't be stable so we must not
save them into bins.
-static void
-mnemon_add_item (mnemon_t *mnemon,
- category_t *category,
- int count,
- const char *challenge,
- const char *response)
-{
- item_t *item;
- bin_t *bin;
-
- item = category_add_item (category, count, challenge, response);
-
- bin = mnemon_get_bin (mnemon, count);
-
- bin_add_item (bin, item);
-}
-
static void
chomp (char *s)
{
static void
chomp (char *s)
{
int line_count = 0;
char *path;
category_t *category;
int line_count = 0;
char *path;
category_t *category;
path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
sprintf (path, "%s/%s", mnemon->dir_name, name);
path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
sprintf (path, "%s/%s", mnemon->dir_name, name);
chomp (line);
response = line;
chomp (line);
response = line;
- mnemon_add_item (mnemon, category, count, challenge, response);
+ category_add_item (category, count, challenge, response);
free (line);
fclose (file);
free (path);
free (line);
fclose (file);
free (path);
+
+ /* Resize category items to fit exactly. */
+ category->items_size = category->num_items;
+ category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
+
+ /* Now that the category is completely loaded, with stable
+ * pointers to every item, we can add each item to its appropriate
+ * bin. */
+ for (i = 0; i < category->num_items; i++) {
+ item_t *item = &category->items[i];
+ bin_t *bin = mnemon_get_bin (mnemon, item->count);
+
+ bin_add_item (bin, item);
+ }