#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>

typedef struct node{
	int count;
	char *word;
	struct node *left;
	struct node *right;
} node_t;

node_t root;

int word_count = 0;

char *get_word(FILE *);
int verbose = 0;
int silent = 0;
int timecount = 0;

main(int argc, char *argv[])
{
	FILE *file = stdin;
	char buffer[128];
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;
	int size;
	char *word;
	int i = 1;

	root.count = 0;
	root.left = root.right = (node_t *) NULL;
	root.word = (char *) NULL;

	while (i < argc) {
		if (strcmp(argv[i], "-v") == 0) {
  		  verbose = 1;
  		  i++;
  		  continue;
		}
		if (strcmp(argv[i], "-s") == 0) {
	      silent = 1;
	      i++;
	      continue;
	    }
	  if (strcmp(argv[i], "-t") == 0) {
	    timecount = 1;
	    i++;
	    continue;
	  }
		if ((file = fopen(argv[i], "rb")) == NULL) {
			perror(argv[1]);
			exit(1);
		} else {
			i++;
			continue;
		}
	}
    if (timecount) {
      gettimeofday(&tv, &tz);
      time_start = (double)tv.tv_sec +
			(double)tv.tv_usec / 1000000.0;
	}
	while (1) {
		word = get_word(file);
		if (word == (char *)NULL)
			continue;
		if (word == (char *)EOF)
			break;
		insert(&root, word);
	}
	printf("Word count:\n");
	list(&root);
	printf("Total words: %d\n", word_count);

    if (timecount) {
      gettimeofday(&tv, &tz);
      time_end = (double)tv.tv_sec +
			(double)tv.tv_usec / 1000000.0;
	  printf("\nSerial Time Elapsed %lf sec.\n", time_end - time_start);
	}
}

insert(node_t *node, char *word)
{
    int position;

	if (node == (node_t *) NULL) {
		node = (node_t *) calloc(1, sizeof(node_t));
	}
	if (node->word == (char *) NULL) {
	    if (verbose)
        	printf("Inserting %s\n", word);
		node->word = word;
		node->count = 1;
		return;
	}
	position = strcmp(word, node->word);
	if (position == 0) {
		node->count++;
	    if (verbose)
	    	printf("Adding %s count %d\n", word, node->count);
		return;
	} else if (position < 0) {
	  if (node->left == (node_t *)NULL)
	    node->left = (node_t *) calloc(1, sizeof(node_t));
	  insert(node->left, word);
	} else if (position > 0) {
	  if (node->right == (node_t *)NULL)
	    node->right = (node_t *) calloc(1, sizeof(node_t));
	  insert(node->right, word);
	}
}

list(node_t *node)
{
	if (node->word != (char *)NULL) {
	  if (silent == 0)
		printf("%s %d\n", node->word, node->count);
	  word_count++;
	}
	if (node->left != (node_t *)NULL)
		list(node->left);
	if (node->right != (node_t *)NULL)
		list(node->right);
}

char *get_word (FILE *file)
{
	char word [32];
	static char line[256];
	static int count = 0;
	static char *p;
	char *q;
    int inword, size;

	// bufzero(word, sizeof(word));
	memset(word, 0, sizeof(word));
	if (count == 0) { /* Read more from file */
		//bufzero(line, sizeof(line));
		memset(line, 0, sizeof(line));
		p = fgets(line, sizeof(line), file);
		if (p == 0)
			return (char*)EOF;
		count = strlen(p);
//		printf("count: %d\n", count);
		if (count == 1) {
			if (*p == '\n' || *p == '\r')
				return (char*)NULL;
		}
//		p[count - 1] = '\0'; // Remove newline from fgets
		inword = 0;
	}
    q = &word[0];
	while (count) {
		// if (*p == '.' || *p == ',' || *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') {
			if ((*p|0x20) < 0x61 || (*p|0x20) > 0x7A ) {
			p++; // // skip all chars but literals
			*q = '\0';
			count--;
			if (inword == 1)
				break;
			else
				continue;
		}
       *q++ = *p++;
       count--;
		inword = 1;
    }
    size = strlen(word);
	if (size == 0)
      return (char*)NULL;
//    printf("word %s\n", word);
	q = (char *) calloc(1, size+1);
	strncpy(q, word, size+1);
    return q;
}

int getword (char *buffer, int bufsize)
{
   int count = 0;
   char *p;

   //bufzero(buffer, bufsize);
   memset(buffer, 0, bufsize);
   p = fgets(buffer, bufsize, stdin);

   if (p != 0) {
      count = strlen(p);
      p[count - 1] = '\0'; // Remove newline from fgets
      //      printf("getword: %s %d\n", buffer, count);
   } else {
     count = 0;
   }
   return count;
}

