#include <fstream.h>
#include "FileInterface.h"
#include "MaxLengthConstants.h"

int FileInterface::loadWordFile(char fileName[], WordList & aWordList)
{
	char str[MaxWordLength];	// temp string holder.
	Word tempWord;				// temp word holder.
	int counter = 0;

	aWordList.resetList();	// Empty list of any previous contents.

	int fileOpenedStatus = 1;

	fstream datafile;

	datafile.open(fileName, ios::in | ios::nocreate);

	// tests to see if the opening of the file
	// was succesful or not.

	if (datafile.fail())
	{
		fileOpenedStatus = 0;
	}
	else
	{
		while ((!datafile.eof()) && (counter < MaxListLength))	// counter prevents too many words
																// being added to the list.
		{
			datafile.getline(str,MaxWordLength);

			// Create temporary word to pass as parameter into aWordList;

			if (!(str[0] == '\0'))	// if the string isn't a blank line.
			{
			tempWord.setWord(str);
			
			// add word to WordList

			aWordList.addWord(tempWord);

			counter++;

			}

		}
	}

	datafile.close();

	return fileOpenedStatus;
}
