//CTextReader.cpp

#include <winsock2.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <fstream>
#include <cstdlib>


#include "CTextReader.h"




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

STRINGC CTextReader::getString(STRINGC name, STRINGC file, int nbr)
{
	
	STRINGC tmp = getLineWithValue(name, file,nbr);
	return tmp;

}

int CTextReader::getInt(STRINGC name, STRINGC file, int nbr)
{
	STRINGC tmp = getLineWithValue(name,file,nbr);
	int i=atoi(tmp.GetString());	
	return i;


}

float CTextReader::getFloat(STRINGC name, STRINGC file, int nbr)
{
	STRINGC tmp = getLineWithValue(name,file,nbr);	
	float i=atof(tmp.GetString());		
	
	return i;


}



STRINGC CTextReader::getLineWithValue(STRINGC name, STRINGC file, int nbr)
{
	STRINGC value="NULL";

	
	std::ifstream from(file);
	
	int found=0;
		
	char tmp[100];
	if(!from)
	{
		printf("ERROR");
	}
	

	while(from)
	{
		
		from.getline(tmp, MAX_LENGTH,'\n');
		STRINGC line(tmp);
		
	

	
		if(line.LeftStr(name.Len())==name)
		{
			found++;
			value=line.ExtractValue();			
			if(found==nbr)
			{
				from.close();	
				return value;
			}

		}
	}	
	
	/*

	FILE *filein;

	filein = fopen(file.GetString(), "r");
	name += "=%s";
	char tmp[100];

	//while (!feof(filein)) 
	
	for(int i=0; i < nbr-1; i++)
	{
		memset(tmp, 0, 100);		
		fscanf(filein, name.GetString(), &tmp);		
	}

	if(fscanf(filein, name.GetString(), &tmp)!=EOF)
	{
		value=tmp;
	}
	
	
	

	
	

	fclose(filein);
	

	return value;
	*/

	return STRINGC("NULL");



}