class MyBag
{
private:
    int nLines;
    std::string *lineArray;
    void ResizeArray(int newLength);
public:
    MyBag();
    int size();
    void add (std::string line);
    void lineNum (int index);
    void lineSearch (std::string key);
};

MyBag::MyBag()
{
    nLines = 0;
    std::string *lineArray = new std::string[0]();
}
void MyBag::ResizeArray(int newLength)
{
    std::string *newArray = new std::string[newLength];
    //create new array with new length
    for (int nIndex=0; nIndex < nLines; nIndex++)
    {
        newArray[nIndex] = lineArray[nIndex];
        //copy the old array into the new array
    }
    //THE CODE FAILS HERE AT THIS DELETE (I think)
    delete[] lineArray; //delete the old array
    lineArray = newArray; //point the old array to the new array
    nLines = newLength; //set new array size
}
void MyBag::add(std::string line)
{
    ResizeArray(nLines+1); //add one to the array size
    lineArray[nLines] = line; //add the new line to the now extended array
    nLines++;
}
int MyBag::size()
{
    return nLines;
    //return size of array
}
void MyBag::lineNum (int index)
{
    std::cout << lineArray[index-1] <<  std::endl;
    //return current array index
}
void MyBag::lineSearch (std::string key)
{
    int counter = 0;
    for (int n=nLines-1; n>0; n--)
    {
        if (lineArray[n].find(key) != std::string::npos)
        {
            std::cout << lineArray[n] << std::endl;
            counter++;
        }
    }
    if (counter == 0)
    {
        std::cout << "No matching lines" << std::endl;
    }
    //search for a specific string within a line
}

/*
 * weblogclient.cpp
 *
 *
 */

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include "dynamic_array_header.h"
#include "using_vectors_header.h"

using namespace std;

void getData(MyBag& webLog);
void printResults(MyBag& webLog);

int main()
{
	MyBag webLog;

    getData(webLog);
    printResults(webLog);

    return 0;
}

//********************************************************************

void getData(MyBag& webLog)
// This function gets the data from a file.

{
	ifstream inFile;
	string webLine;

	inFile.open("weblog_medium.txt");
	assert(inFile);

	while (getline(inFile, webLine))
	   {
		webLog.add(webLine);
		std::cout << webLine << std::endl;
	   }
        inFile.close();
        inFile.clear();

}

//********************************************************************

void printResults(MyBag& webLog)


{

	cout << "There were " << webLog.size()
		 << " total page requests." << endl << endl;


	cout << "Line number 5: \n";
      webLog.lineNum(5);

	cout << "Search for IP 24.138.48.78: \n";
      webLog.lineSearch("24.138.48.78");

      cout << "Search for IP 24.138.48.79: \n";
      webLog.lineSearch("24.138.48.79");
}