/***************************************************************************
*   Copyright (C) 2004 by Denis A. Birukov aka Messenger of death         *
*   riper@@fromru.com                                                      *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "me_c_parser.h"
#include <me_debug.h>
#include <me_c_storage.h>
#include <me_c_storageentry.h>
#include <string>
#include <map>
#include <sstream>
namespace ME_n_Storage
{
	ME_c_Parser::ME_c_Parser()
	{}
	bool ME_c_Parser:: _parse( const std::vector<std::pair<std::string, std::string> > &atrs, ME_c_Storage *storage, const std::string &category )
	{

		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: category=" << category << std::endl;

		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: atrs.size=" << atrs.size() << std::endl;
		for ( Uint32 a = 0;a < atrs.size() ; ++a )
		{
			std::string text, text1;
			text = atrs[ a ].second;
			ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: atrs["<<a<<"].second=" << text << std::endl;
			/// @todo Возможно добавить распознавание всех значений
			//Анализ значения
			if ( (text[ 0 ] >= '0' && text[ 0 ] <= '9' || text[0]=='-') &&  text.find( "." ) == text.rfind( "." ) && text.find( "." ) != text.npos )    //возможно double значение
			{
				std::stringstream s1, s2;
				s1.str( text );
				double t;
				s1 >> t;
				s2 << t;
				if ( s1.str() != s2.str() ) break; //строка
				//double значение
				storage->_add_value( category + "->" + atrs[ a ].first, new ME_c_Storage_Entry( ME_STORAGE_DOUBLE, ( void* ) & t ) );
				ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: category=" << category << " " << category + "->" + atrs[ a ].first << "=(double)" << t << std::endl;
			}
			else if ( text[ 0 ] >= '0' && text[ 0 ] <= '9' )  //возможно unsigned
			{
				std::stringstream s1, s2;
				s1.str( text );
				Uint32 t;
				s1 >> t;
				s2 << t;
				if ( s1.str() != s2.str() ) break; //строка
				storage->_add_value( category + "->" + atrs[ a ].first, new ME_c_Storage_Entry( ME_STORAGE_UINT32, ( void* ) & t ) );
				ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: category=" << category << " " << category + "->" + atrs[ a ].first << "=(Uint32)" << t << std::endl;
			}
			else if ( text[ 0 ] == '-' )  //возможно signed
			{
				std::stringstream s1, s2;
				s1.str( text );
				Sint32 t;
				s1 >> t;
				s2 << t;
				if ( s1.str() != s2.str() ) break; //строка
				storage->_add_value( category + "->" + atrs[ a ].first, new ME_c_Storage_Entry( ME_STORAGE_SINT32, ( void* ) & t ) );
				ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: category=" << category << " " << category + "->" + atrs[ a ].first << "=(Sint32)" << t << std::endl;
			}
			else //строка
			{
				storage->_add_value( category + "->" + atrs[ a ].first, new ME_c_Storage_Entry( ME_STORAGE_STRING, ( void* ) & atrs[ a ].second ) );
				ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - ME_c_Parser: category=" << category << " " << category + "->" + atrs[ a ].first << "=" << atrs[ a ].second << std::endl;
			}
		}
		return true;
	}
}
