#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hashtable.h>

static void
add_data (HashTable *table)
{
  char key[20];
  char value[20];

  printf ("Input the key ");
  scanf ("%s", key);
  printf ("Input the value ");
  scanf ("%s", value);

  hash_table_insert (table, strdup (key), strdup (value));

  char *key_lookup = strndup (key, strlen (key));
  printf ("The last one inserted was: {'%s': ", key_lookup);
  const char *val = hash_table_lookup (table, key);
  printf ("'%s'}\n", val);

  free (key_lookup);
}

static void
print_all (void *key,
           void *value,
           void *user_data)
{
  printf ("%s:%s\n", (const char *) key, (const char *) value);
}

static unsigned
str_hash (const void *data)
{
  const char *str = (const char *) data;
  return (unsigned) str[0];
}

static bool
str_equal (const void *a,
           const void *b)
{
  return !strcmp (a, b);
}

int
main (void)
{
  HashTable *table;

  table = hash_table_new (str_hash, str_equal, free, free);

  while (1)
  {
    add_data (table);
    hash_table_foreach (table, print_all, NULL);
  }

  return 0;
}