#include <stdio.h>
#include <ctype.h>
#include <sys/types.h> 
#include <netinet/in.h>
#include "hash.h"
#include "thesaurus.h"

int hashindex(char *word, int limit) 
{
  int retval = 0;
  int i = 0;

  if(!word || !*word) return NULL;

  do {

    retval ^= ((*word - 'a')<<i);
    i++;
    retval ^= ((*word - 'a')<<((3*i) % 20 ));

  } while(*(++word));
  return retval % limit;
}

lower(char *word)
{

  while(*word) {
    if(isupper(*word)) {
      *word = toupper(*word);
    }
    word++;
  }
}

int getindex(FILE *fptr, char *word, int limit) 
{
  Entry entry = NULL;
  struct entry_s e;
  int indx , items;
  long retval; 

  entry = &e;
  indx = hashindex(word, limit); 
  debug_printf("%d = hashindex(%s, %d)\n", indx, word, limit);
  fseek(fptr, sizeof(struct entry_s) * indx, 0);

  do {
    debug_printf("enter do\n");
    if(items = fread(entry, sizeof(struct entry_s), 1, fptr) != 1) {
      debug_printf("%d items read\n",items);
      perror("fread");
      return NULL;
    } 
    debug_printf("got item\n");
    if(!strcmp(word, entry->str)) {
      debug_printf("found match at %d\n", ntohl(entry->indx));
      break;
    }
    indx ++;
    indx %= limit;
  } while(*(entry->str));

  if(!(*(entry->str))) return -1;
  retval = ntohl(entry->indx);
  return retval ;
}

noop()
{
}
