/*
   Name: your name here
   Date: Feb 25, 2003
   Course: CSci 170
*/

#include "concordance.h"

/* This program reads the input file and produces a concordance.  The 
concordance alphabetically lists every word that appears in the file 
along with a list of numbers indicating where it occurs.  Only strings
of letters are considered words.  Digits, punctuation, etc. are ignored.
For example, if the input is
   My dog is a dog.
then the output would be
   a 4
   dog 2 5
   is 3
   my 1
Note that the words are all converted to lowercase. */

/* A number list is a list of numbers.  A numbernode is a node of a
number list which consists of a number and a link to the next numbernode. */  
typedef struct numbernode {
  int number;
  struct numbernode *link;
} numbernode;

/* The wordlist is a list of words, and associated to each word is a list of 
numbers (the numbers indicating where the word occurs). Thus, a wordnode 
specifies the word, the list of numbers, and a link to the next word. A tail 
pointer is included since each time a new number is inserted, it is inserted 
at the end of the list.  The wordlist is kept in alphabetical order. */
typedef struct wordnode {
  char *word;
  numbernode *numberhead;
  numbernode *numbertail;
  struct wordnode *link;
} wordnode;

/* concordance is the pointer to the beginning of the word list.  Initially,
of course, it is NULL. */
wordnode *concordance = NULL;

/* getword reads a word from the input stream.  It returns true if
it finds a word, but false when it reaches the end of the file.  The
parameter w points to a string buffer to store the word in. */
int getword(char *w) {
  char c;
  while ((c=getchar()) != EOF && !isalpha(c));  // skip over nonletters
  if (c==EOF)                                   // finally, we're done!
    return false;
  else {
    *w++ = tolower(c);                          // save the first char
    while ((c=getchar()) != EOF && isalpha(c))
      *w++ = tolower(c);                        // save succeeding chars
    *w = '\0';                                  // nil terminate the string
    return true;
  } // if/else
} // getword

/* makewordnode creates a new word node where the word being
stored therein is referred to by w. */
wordnode *makewordnode (char *w) {
  wordnode *temp;
  
  temp = (wordnode *) malloc(sizeof(wordnode));
  temp->link = NULL;
  temp->numberhead = NULL;
  temp->numbertail = NULL;
  temp->word = (char*) malloc(strlen(w));
  strcpy(temp->word,w);
  return temp;
} // makewordnode

/* findword looks through the list of words for the word specified
by wordbuffer.  If the word is not found, then a new word node is
created and inserted into the list of words.  In any case, a pointer
to the wordnode (either newly created or already existing) is
returned. */
wordnode *findword(char *wordbuffer) {

  /* Fill in the body of this function */

} // findword

/* makenumbernode creates a node with a given number it, and
returns a pointer to that node. */
numbernode *makenumbernode(int n) {

  /* Fill in the body of this function */

} // makenumbernode

/* insertnumber inserts a new numbernode with a given number into
the list for a given word. */
void insertnumber(wordnode *w, int n) {
  if (w->numberhead) {
    w->numbertail->link = makenumbernode(n);
    w->numbertail = w->numbertail->link;
  }
  else
    w->numberhead = w->numbertail = makenumbernode(n);
} // insertnumber

/* printword prints to the output stream a word along with its
list of numbers */
void printword(wordnode *w) {
  numbernode *n;
  printf("%s", w->word);
  for (n=w->numberhead; n; n=n->link)
    printf(" %d", n->number);
  printf("\n");
} // printword

/* printconcordance prints the entire concordance to the output
stream, word by word, with the list of numbers printed for each word. */
void printconcordance(void) {
  wordnode *w;
  for (w=concordance; w; w=w->link)
    printword(w);
} // printconcordance

int main(void) {
  char wordbuffer[80];   // we're in trouble if there's a word of length 81!
  int wordnumber = 0;    // we'll count the words as we read them
  
  printf("\nThis program produces a word concordance for text you enter.\n");
  printf("\nPlease enter the text.  Finish with a <ctrl>d character.\n\n");
  while (getword(wordbuffer)) {  // get the next word
    wordnumber++;                // keep track of the count,
    // and insert the number where the word occurs into the list
    insertnumber(findword(wordbuffer),wordnumber);
  }
  printf("\nHere is the concordance for your text.\n");
  printconcordance();
  printf("\n--------------------\n");
} // main
