//-----------------------------------------------------------------------------
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include "boolean.h"
#include "stringclass.h"
#include "linkedlistclass.cpp"
//-----------------------------------------------------------------------------
typedef struct {
    string TheWord;
    int NumberOfOccurrences;
    } wordcounter;
//-----------------------------------------------------------------------------
int WordChar(int Char);
void AddWordToList(linkedlist<wordcounter>& ListOfWords,const string& Word);
//-----------------------------------------------------------------------------
int main(int argc,char* argv[]) {

ifstream InputFile;
string MyString(100);
linkedlist<wordcounter> ListOfWords;
linkedlistiterator<wordcounter> ListOfWordsIterator(ListOfWords);

//----Check arguments
if (argc == 2) {
    InputFile.open(argv[1]);
    if (InputFile.fail()) {
        cerr << "ERROR: Cannot open file " << argv[1] << " for reading" 
             << endl;
        exit(-1);
        }
    }
//----Otherwise error (bloody C++ is not perl)
else {
    cerr << "ERROR: Usage - ReadWordsFromFile <Filename>" << endl;
    exit(-1);
    }

InputFile >> MyString;
while (! InputFile.eof()) {
    if (MyString.CharsAreAll(WordChar)) {
        AddWordToList(ListOfWords,MyString);
        }
//DEBUG else cout << "Gunk: " << MyString << endl;
    InputFile >> MyString;
    }

InputFile.close();

//----Print out linked list
cout << "Linked list is" << endl;
ListOfWordsIterator.ToStart();
cout << ListOfWordsIterator.CurrentData().TheWord 
     << "(" << ListOfWordsIterator.CurrentData().NumberOfOccurrences << ") ";
// cout << endl;
while (ListOfWordsIterator.NextNodeExists()) {
    ++ListOfWordsIterator;
    cout << ListOfWordsIterator.CurrentData().TheWord 
         << "(" << ListOfWordsIterator.CurrentData().NumberOfOccurrences 
         << ") ";
//     cout << endl;
    }
cout << endl;
}
//-----------------------------------------------------------------------------
int WordChar(int Char) {

return(isalpha(Char) || Char == '\'');
}
//-----------------------------------------------------------------------------
void AddWordToList(linkedlist<wordcounter>& ListOfWords,const string& Word) {

linkedlistiterator<wordcounter> ListOfWordsIterator(ListOfWords);
wordcounter WordCount;

//DEBUG cout << "Adding " << Word << " to list" << endl;

WordCount.TheWord = Word;
WordCount.NumberOfOccurrences = 1;

if (ListOfWords.Empty())
    ListOfWords.AddAtStart(WordCount);
else {
    ListOfWordsIterator.ToStart();
    while (ListOfWordsIterator.CurrentData().TheWord < Word &&
ListOfWordsIterator.NextNodeExists())
        ++ListOfWordsIterator;
//----Check if stopped at a larger word
    if (ListOfWordsIterator.CurrentData().TheWord > Word)
        ListOfWordsIterator.AddBefore(WordCount);
//----Check if stopped at end of list
    else if (ListOfWordsIterator.CurrentData().TheWord < Word)
            ListOfWordsIterator.AddAfter(WordCount);
        else {
//DEBUG cout << "Seen " << Word << " before" << endl;
            WordCount = ListOfWordsIterator.CurrentData();
            WordCount.NumberOfOccurrences++;
            ListOfWordsIterator.SetCurrentData(WordCount);
            }
    }
}
//-----------------------------------------------------------------------------
