/*

cls_2: Curl Like Syntax
Yauheni A. Akhotnikau (C) 2001-2003
eao197@yahoo.com
-------------------------------------------------

Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:

1. This software 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.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.

3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

-------------------------------------------------

*/
/*
	cls-2
*/

#include <assert.h>

#include <cls_2/h/tag_by_factory.hpp>

namespace cls_2 {

//
// tfactory_t
//

tfactory_t::~tfactory_t()
{
}

//
// tfactory_constraint_t
//

tfactory_constraint_t::~tfactory_constraint_t()
{
}

//
// tag_by_factory_t
//
tag_by_factory_t::tag_by_factory_t(
	const char * name,
	bool mandatory ) :
	tag_t( name ),
	m_is_mandatory( mandatory ),
	m_constraint( 0 )
{
}

tag_by_factory_t::~tag_by_factory_t()
{
	destroy_contents();
}

void
tag_by_factory_t::add_factory(
	const char * tag_name,
	tfactory_t & factory )
{
	assert( tag_name );

	m_factories.insert( tfactory_map_t::value_type(
		std::string( tag_name ), &factory ) );
}

tfactory_constraint_t *
tag_by_factory_t::set_constraint(
	tfactory_constraint_t * constraint )
{
	tfactory_constraint_t * old_constraint = m_constraint;
	m_constraint = constraint;

	return old_constraint;
}

int
tag_by_factory_t::on_start(
	parser_context_t & context ) {

	if( m_cur_tag.query() ) {
		return m_cur_tag.query()->on_start( context );
	}
	else {
		context.show_error( "tag_by_factory_t::on_start: "
			"m_cur_tag.query() == 0\n" );
		return c_semantic_error;
	}
}

int
tag_by_factory_t::on_finish(
	parser_context_t & context ) {

	if( m_cur_tag.query() ) {
		int ret_code = m_cur_tag.query()->on_finish( context );
		if( c_ok == ret_code ) {
			// Тег успешно разобрали.
			m_values.push_back( m_cur_tag.query() );
			m_cur_tag.clear();
		}
		else
			m_cur_tag.destroy();

		return ret_code;
	}
	else {
		context.show_error( "tag_by_factory_t::on_finish: "
			"m_cur_tag.query() == 0\n" );
		return c_semantic_error;
	}
}

int
tag_by_factory_t::on_tag(
	parser_context_t & context,
	tag_t & tag )
{
	if( m_cur_tag.query() ) {
		return m_cur_tag.query()->on_tag( context, tag );
	}
	else {
		context.show_error( "tag_by_factory_t::on_tag: "
			"m_cur_tag.query() == 0\n" );
		return c_semantic_error;
	}
}

int
tag_by_factory_t::on_tok_space(
	parser_context_t & context,
	const char * tok_value )
{
	if( m_cur_tag.query() )
		return m_cur_tag.query()->on_tok_space( context, tok_value );
	else
		return c_ok;
}

int
tag_by_factory_t::on_tok_nonspace(
	parser_context_t & context,
	const char * tok_value )
{
	if( m_cur_tag.query() )
		return m_cur_tag.query()->on_tok_nonspace( context, tok_value );
	else
		return c_ok;
}

int
tag_by_factory_t::on_tok_string(
	parser_context_t & context,
	const char * tok_value )
{
	if( m_cur_tag.query() )
		return m_cur_tag.query()->on_tok_string( context, tok_value );
	else
		return c_ok;
}

tag_t **
tag_by_factory_t::query_tags() {
	if( m_cur_tag.query() )
		return m_cur_tag.query()->query_tags();
	else
		return 0;
}

const char *
tag_by_factory_t::query_name() const
{
	if( m_cur_tag.query() )
		return m_cur_tag.query()->query_name();
	else
		return tag_t::query_name();
}

bool
tag_by_factory_t::compare_name( const char * name ) const
{
	assert( name );

	std::string sname( name );
	tfactory_map_t::const_iterator it = m_factories.find( sname );
	if( it != m_factories.end() ) {
		if( m_constraint && c_ok != m_constraint->check(
			*this, name ) )
			// Ограничения не позволяют создавать тег.
			return false;

		// Можно попробовать создать тег.
		//ПРИМЕЧАНИЕ. Здесь нужно избавляться от модификатора const.
//FIX ME: избавляться от модификатора const не пришлось бы,
//если бы в метод on_start передавалось имя создаваемого тега.

		tfactory_t & factory = *((*it).second);

		tag_by_factory_t * p = const_cast< tag_by_factory_t * >( this );
		p->m_cur_tag.destroy();
		p->m_cur_tag.set( factory( *this, name ) );

		return true;
	}

	return false;
}

bool
tag_by_factory_t::is_mandatory() const {
	return m_is_mandatory;
}

bool
tag_by_factory_t::is_defined() const {
	return ( 0 != size() );
}

void
tag_by_factory_t::reset() {
	destroy_contents();
}

size_t
tag_by_factory_t::size() const {
	return m_values.size();
}

const tag_t &
tag_by_factory_t::at( size_t index ) const {
	return *( m_values[ index ] );
}

void
tag_by_factory_t::make_defined( tag_t * tag ) {
	m_values.push_back( tag );
}

void
tag_by_factory_t::format(
	tag_formatter_t & fmt ) {
	for( vector_t::iterator
		it = m_values.begin(), it_end = m_values.end();
		it != it_end; ++it ) {
		(*it)->format( fmt );
	}
}

void
tag_by_factory_t::destroy_contents() {
	m_cur_tag.destroy();

	for( vector_t::iterator
		it = m_values.begin(), it_end = m_values.end();
		it != it_end; ++it ) {
		delete (*it);
	}

	m_values.clear();
}

} /* namespace cls_2 */
