// File:            prntFileLLst.cc
// ---------------------------------------------------------------------
//          Author: Russell Lowke,  lowke@fas.harvard.edu
//              TF: Francis Crick
//            Date: 7/29/2002
//      Assignment: Unit 8 Easy Supp programming exercises
//         Version: 1
// 

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <values.h>
#include "list.hpp"


//
// prints the array
//
void printArray(int * array, int n)
{
	cout << "{ ";
	for (int i = 0; i < n; i++) {
		cout << array[i] << " ";
	}
	cout << "}" << endl;
}

void openFile(char * fileName, list<int> &mList)
{
	ifstream  inStream;
	inStream.open(fileName, ios::binary | ios::in );
	
	int mInt, num_compares = 0;
	node<int> * placeNode = mList.first();
	
	// ensure file exists
	if (inStream == NULL) {					// file not found
	
		cout << inStream << " -- Input file not found!\n";
		
	} else {									// file OK
		
		while (! inStream.eof() ) {				// read file
					
			inStream.read((char*) &mInt, sizeof(int));
			
			// to add in order
			// placeNode = mList.search_ordered(mInt, num_compares);
			
			mList.insert(placeNode, mInt);
		}
		
		inStream.close();
		
		cout << endl;
	}
}


/*
// Note: not used.  prints the list
//  via retrieved index
void printListSlow(list<int> mList)
{
	int n = mList.get_num_items();
	node<int> * temp;
	
	for(int i = 0; i < n; i++ ) {
		temp = mList.retrieve(i);
		cout << temp->data << " ";
	}
	cout << endl;
	
}
*/

//
//  Prints list via
//  incrementing the pointer
void printList(list<int> mList)
{
	node<int> * ptr  = mList.first();
	node<int> * last = mList.last();
	
	while(ptr != last) {
		cout << ptr->data << " ";
		ptr = mList.next_node(ptr);
	}
	cout << endl;
	
}


int main()
{

	list<int> mList;
	
	char fileName[] = {"int64.dat"};
	
	openFile(fileName, mList);
	
	cout << "contents of file: " << fileName << endl;
	printList(mList);
	
	exit (0);
}