#include <fstream>
#include <sstream>
#include <iostream>

#include "DataBase.h"

using namespace std;

namespace database {


bool DataBase::loadData( std::string fileName)
{
	ifstream infile(fileName.c_str());
	if( !infile.is_open() )
		return false;

	char buffer[4096];
	infile.getline(buffer,4096,'\n');
	unsigned observationLength = strlen(buffer);
	infile.seekg(0,ios::end);
	long fileLength = infile.tellg();
	infile.close();

	if( observationLength )
	{
		dimension=0;
		observations =vector<const double *>( 1 + fileLength/observationLength, 0);
		istringstream istring(buffer);
		double temporaryDouble=0.0;
	  while(istring >> temporaryDouble)
		{
			dimension++;
		}
	}
	else
		return false;



	infile.open(fileName.c_str());
	infile.getline(buffer,4096,'\n');
	vector<const double *>::iterator current=observations.begin();
	while(!infile.eof())
	{
		istringstream istring(buffer);
		double * tmpDoubleArray = new double[dimension];
		for(unsigned i=0;i<dimension;++i)
		{
			istring >> tmpDoubleArray[i];
		}
		*current = tmpDoubleArray;
		infile.getline(buffer,4096,'\n');
		++current;
	}
  observations.erase(current,observations.end());
  return true;
}

}
