/*********************************************************************
*	parsing
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 21.7.2008
*	Soubor: parsing.cpp
*	Popis: soubor funkci, ktere se pouzivaji k analyze retezcu
**********************************************************************/

#include "parsing.h"

using namespace std;

string parsing::readField( ifstream &fin, const char separator )
{
	string data;
	char c = 0;
	while( (c = fin.get()) != -1 && c != separator )
	{
		if( c == '\n' )
			continue;
		data += c;
	}
	return data;
}

std::string parsing::readTillEndOfLine( ifstream &fin )
{
	return parsing::readField(fin, '\n' );
	
}

std::string parsing::skipEmptyCharsAndReadTillEndOfLine( std::ifstream &fin )
{
	std::string result;
	if( fin.is_open())
	{
		char c = 0;
		while( isSpace(c = fin.get()) );
		do
		{
			if( fin.eof() || c == '\n' )
				break;
			
			result+=c;
		}while( (c = fin.get()) );
			
	}
	return result;
}

std::string parsing::readPosibleQuotedData( std::ifstream &fin )
{
	std::string result;
	if( fin.is_open())
	{
		char c = 0;
		while( isSpace(c = fin.get()) );	//skip empty

		if( fin.eof() )
			return result;

		if( c == '\"' )
		{
			return parsing::readField( fin, '\"' );
		}
		else
		{
			result += c;
			while( (c = fin.get() )  )
			{
				if( fin.eof() || isSpace( c ) )
					break;
				result += c;
			}
		}
	}
	return result;
}


std::string parsing::readField( istringstream &in, const char separator )
{
	string data;
	char c = 0;
	while( (c = in.get()) != -1 && c != separator )
	{
		if( c == '\n' )
			continue;
		data += c;
	}
	return data;


}




std::string parsing::readPosibleQuotedData(  istringstream &in )
{
	std::string result;
	if( !in.eof() )
	{
		char c = 0;
		while( isSpace(c = in.get()) );	//skip empty

		if( in.eof() )
			return result;

		if( c == '\"' )
		{
			return parsing::readField( in, '\"' );
		}
		else
		{
			result += c;
			while( (c = in.get() )  )
			{
				if( in.eof() || isSpace( c )  )
					break;
				result += c;
			}
		}
	}
	return result;
}

std::string parsing::readCommand( std::istringstream &in, bool &endOfCommand )
{
	
	char c = 0;
	while( isSpace( (c = in.get() ) )  );
	if( in.eof() )
		return "";
	string result;
	endOfCommand = true;
	do
	{
		if( c == ';' )
			break;
		else if( isSpace( c ) )
		{
			endOfCommand = false;
			break;
		}
		else 
			result += c;	
	}
	while( (c = in.get() ) != -1 );

	return result;
}

//std::string parsing::readWholeCommand( std::istringstream &in )
//{
//	string result;
//	char c = 0;
//	while( isSpace( (c = in.get() ) )  );
//	bool nextEnd = true, correct = false;
//	int bracket = 0;
//	do
//	{
//		if( bracket % 2 == 0 )
//		{
//			if( c == ';' || c == -1 )
//			{
//				correct = true;
//				break;
//			}
//
//		}
//		if( c == '\"' )
//		{
//			bracket++;
//		}
//		else if( c == -1 )
//			break;
//
//		result += c;
//	}
//	while((c=in.get()) );
//
//	
//	if( !correct )
//		result.clear();
//	return result;
//
//}



std::string parsing::readPosibleQuotedCommandParm( std::istringstream &in )
{
	string result;
	char c = 0;
	while( isSpace( (c = in.get() ) )  );
	bool nextEnd = true, correct = false;
	if( c == '\"' )
	{
		int bracket = 1;
		while( (c=in.get()) != -1 )
		{
			if( bracket % 2 == 0 && nextEnd)
			{
				if( c == ';' || c == -1 )
				{
					correct = true;
					break;
				}
				else
					nextEnd = false;
			}
			if( c == '\"' )
			{
				bracket++;
				nextEnd = true;
			}

			result += c;
		}

		if( nextEnd )
			correct = true;
		if( result.size() > 0 )
			result.resize( result.size() - 1 );

	}
	else
	{
		correct = true;
		do
		{
			if( c == ';' )
				break;
			else 
				result += c;	
		}
		while( (c = in.get() ) != -1 );
	}
	if( !correct )
		result.clear();
	return result;
}



int parsing::isSpace( unsigned char c )
{
	return isspace( c );
}

int parsing::isAlpha( unsigned char c )
{
	return isalpha( c );
}

int parsing::isUpper( unsigned char c )
{
	return isupper( c );
}
int parsing::isLower( unsigned char c )
{
	return islower( c );
}
int parsing::isDigit( unsigned char c )
{
	return isdigit( c );
}

int parsing::toUpper( unsigned char c )
{
	return toupper( c );
}

int parsing::toLower(  unsigned char c )
{
	return tolower( c );
}



bool parsing::stringEndsWith( std::string tested, std::string end )
{
	const char *A = tested.c_str();
	const char *B = end.c_str();
	for( size_t i = tested.length(), j = end.length(); i > 0 && j > 0; --i, --j )
		if( A[i-1] != B[j-1] )
			return false;
	return true;
}


std::string parsing::formatTime( unsigned int m_uiCurrentValue )
{
	ostringstream buf;
	int mins = m_uiCurrentValue / 60000;
	int secs = (m_uiCurrentValue - mins * 60000) / 1000;
	int ms = m_uiCurrentValue - mins * 60000 - secs*1000;
	buf << mins << ":";
	if( secs < 10 )
		buf << "0" << secs;
	else
		buf	<< secs;
	
	buf << ".";
	buf.width(3);  
	buf.fill('0');
	buf << ms;
	return buf.str();
}
