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

#include "../ex03/safearray.h"

//prototypes
void readFrom(string file, safearray<string>&);
void showForward(const safearray<string>&);
void showBackward(const safearray<string>&);

//------- The MAIN point of entry -------
void main() {
    safearray<string> Array;

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

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

    showForward(Array);
    showBackward(Array);
}

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

    for (int i = 0; i < list.maxsize; i++) {
        string temp;
        if(fin >> temp) {
            list[i] = temp;
            break;
        }
    }
}

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

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

    cout << endl;
}

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

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

    cout << endl;
}