/********************************************************************************************
example 2 - readint.cpp
    read integers into an array, then display them in either direction
********************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

#include "safearray.h"

//prototypes
void readFrom(string file, safearray&, int& count);
void showForward(const safearray&, const int& count);
void showBackward(const safearray&, const int& count);

//------- The MAIN point of entry -------
void main() {
    safearray Array;
    int count = 0;

    string filename;
    cout << "What is the file to read: ";
    cin >> filename;

    readFrom(filename, Array, count);
    cout << "Total Elements Used: " << count << endl
         << "Out of a maximum of: " << maxsize << endl;

    showForward(Array, count);
    showBackward(Array, count);
}

//------- Load a list of integers from a text file -------
void readFrom(string file, safearray& list, int& count) {
    ifstream fin(file.c_str());

    for (int i = 0; i < maxsize; i++) {
        int x;
        if(fin >> x) {
            list.put(i, x);
            count++;
        }
        else
            break;
    }
}

//------- Display the list of integers in the order they are stored -------
void showForward(const safearray& list, const int& count) {
    cout << "Forward:" << endl;

    for (int i = 0; i < count; i++)
        cout << list.get(i) << endl;

    cout << endl;
}

//------- Display the list of integers in the reverse of the order they are stored -------
void showBackward(const safearray& list, const int& count) {
    cout << "Backward:" << endl;

    for (int i = count; i > 0; i--)
        cout << list.get(i-1) << endl;

    cout << endl;
}