/***************************************************************************
*   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_storage_driver_cfg.h"
#include <me_debug.h>
#include <me_c_storage.h>
#include <me_c_parser.h>

namespace _ME_n_Storage
{
	void _read_block( std::istream& stream, std::map< std :: string, ME_n_Storage::ME_c_Parser * > arg1, ME_n_Storage::ME_c_Storage* storage, const std::string blockname )
	{
		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG::_read_block, blockname=" << blockname << std::endl;
		std::string text, text1, tmp;
		std::vector<std::pair<std::string, std::string> > atrs;
		while ( stream >> text )
		{
			tmp = text.substr( 0, 1 );
			//Анализ выхода из блока
			if ( tmp == "[" )
			{
				if ( text.substr( 1, 1 ) != "/" )
							//Имеется подблок. Обрабатываем его
					_read_block( stream, arg1, storage, blockname + "->" + text.substr( text.find( "[" ) + 1, text.find( "]" ) - 1 ) );
				// Обнаружено окончание блока
				else
				{
					ME_n_Storage::ME_c_Parser *z;
					// Проверка на наличие указанной группы либо группы '*'
					ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG:blockname=" << blockname << std::endl;
					std::string catname = blockname;
					if ( !( z = arg1[ blockname ] ) )//Проверяем наличие группы blockname
					{
						if ( !( z = arg1[ "*" ] ) )//Проверяем наличие группы * т.к. отсутствует 	blockname
						{
							//Ниодна из групп ненайдена
							ME_n_Log::_debug( ME_n_Log::ME_DEBUG_WARNING ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG: нет группы blockname= " << blockname << " z=" << z << std::endl;
							return ;
						}
					}
					//Найдена группа. Производим добавление значений в нее.
					ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG: atrs.size= " << atrs.size() << std::endl;
					z->_parse( atrs, storage, catname );
					atrs.clear();
					return ;
				}

			}
			//Пропуск комментария
			else if ( tmp == "/" )
			{
				std::getline( stream, text );
			}
			else
			{
				std::getline( stream, text1 );
				ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG:atrs.push_back(std::make_pair( " << text << "," << text1.substr(1) << ");" << std::endl;
				atrs.push_back( std::make_pair( text, text1.substr(1) ) );
			}

		}

	}
}
namespace ME_n_Storage
{
	bool ME_c_Storage_Driver_CFG::_parse_stream( std::istream& stream, std::map< std :: string, ME_c_Parser * > arg1, ME_c_Storage* storage )
	{
		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG::_parse_stream" << std::endl;
		std::string text, text1;
		std::map< std::string, ME_c_Parser*>::iterator pos;
		for ( pos = arg1.begin();pos != arg1.end();++pos )
		{
			ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG:installed parsers:" << pos->first << pos->second << std::endl;
		}
		while ( stream >> text )
		{

			ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " -  ME_c_Storage_Driver_CFG:text=" << text << std::endl;
			text1 = text.substr( 0, 1 );
			if ( text1 == "[" )
			{
				if ( text.substr( 1, 2 ) != "/" )
					_ME_n_Storage::_read_block( stream, arg1, storage, text.substr( text.find( "[" ) + 1, text.find( "]" ) - 1 ) );
			}
			else if ( text1 == "/" )
			{
				std::getline( stream, text );
			}
			// Пропуск неверных символов (т.е. отсутствие блока)
			else
			{
				std::getline( stream, text );
			}
		}
		return true;
	}

	ME_c_Storage_Driver_Interface* _create_storage_driver()
	{
		return new ME_c_Storage_Driver_CFG;
	}
}

