#include "SupportFunctions.h"
#include <sstream>


string intToString( int value, int minimalDigitCount )
{
	char buf[10];
	itoa( value, buf, 10 );

	string number( minimalDigitCount, '0' );
	
	for ( int i=0; i< min( (int) strlen( buf ), minimalDigitCount) ; ++i)
	{
		number[ minimalDigitCount-1 -i] = buf[ strlen( buf )-1 -i];	//replace valid digits by real value
	}

	return number;
}

string intToString( int value )
{
	char buf[10];
	return itoa( value, buf, 10 );
}

string longToString( long value )
{
	std::stringstream ss( std::stringstream::in | std::stringstream::out);

	ss<<value;
	return ss.str();
}

long stringToLong( const string& str)
{
	std::stringstream ss( std::stringstream::in | std::stringstream::out);

	long value;
	ss>>value;
	return value;
}

long stringToLongLong( const string& str)
{
	std::stringstream ss( std::stringstream::in | std::stringstream::out);

	long long value;
	ss>>value;
	return value;
}

string longlongToString( long long value )
{
	std::stringstream ss( std::stringstream::in | std::stringstream::out);

	ss<<value;
	return ss.str();
}

string floatToString( float value )
{
	char buf[20];
	
    sprintf( buf, "%.4g", value ); 

	return buf;
}

int stringToInt( const string& str)
{
	return atoi( str.c_str() );
}

float stringToFloat( const string& str)
{
	return atof( str.c_str() );
}


bool getNthStringFromFile( const string& filePath, int n, string &dstText )
{
	fstream f;
	f.open( filePath, fstream::in );
	
	if ( f.fail() )
	{
		return false; //operation was unsuccesfull
	}

	for ( int i=0; i<n; ++i)
	{
		if ( f.eof() )
		{
			return false;
		}

		f>>dstText;
	}

	return true; //everythink ok
}