#include <stdio.h>

#include "llist.h"
#include "hashtable.h"

#define MAX_WORD_SIZE 100
#define HASH_SIZE 500

int read_word(FILE *f, char *s)
{
    int c, d, i = 0, status = 0;
    while (1) {
	c = fgetc(f);
	if (c == EOF) { 
	    s[i] = '\0';
	    return 0;
	}
	if (c >= 'A' && c <= 'Z') d = c - 'A';
	else if (c >= 'a' && c <= 'z') d = c - 'a';
	else d = -1;
	if (d == -1) {
	    if (status == 1) {
		s[i] = '\0';
		return 1;
	    }
	}
	else {
	    s[i] = d + 'a';
	    i++;
	    if (status == 0) status = 1;
	}
    }
}


int main(int argc, char **argv)
{
    FILE *f;
    char *infile = argv[1];
    int c, d;
    char buf[MAX_WORD_SIZE];
    llist *H;

    f = fopen(infile, "r");
    if (f == NULL) {
	printf("File %s could not be opened. Exiting.\n", infile);
	return 1;
    }

    H = create_hash_table(HASH_SIZE);
    while (read_word(f, buf)) {
	process_string(H, buf, HASH_SIZE);
    }
    /* print_hash_table(H, HASH_SIZE); */
    summarize_hash_table(H, HASH_SIZE);
    return 0;
}

