/*

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.

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

*/
/*!
	\file
	\brief Класс потока ошибок, который отображает содержимое
		стека разбираемых тегов.
	
*/

#include <assert.h>
#include <string.h>
#include <stdio.h>

#include <cls_2/h/es_tag_stack.hpp>

namespace cls_2 {

//
// es_tag_stack_t
//

es_tag_stack_t::es_tag_stack_t(
	error_stream_t & real_err_stream,
	const parser_t & parser )
:
	m_real_err_stream(
		nondestroyable_t( real_err_stream ) ),
	m_parser( parser ),
	m_is_new_line( true )
{
}

es_tag_stack_t::es_tag_stack_t(
	const auto_ptr_3::obj_destruction_t< error_stream_t > & to,
	const parser_t & parser )
:
	m_real_err_stream( to ),
	m_parser( parser ),
	m_is_new_line( true )
{
}

es_tag_stack_t::~es_tag_stack_t() {
}

error_stream_t &
es_tag_stack_t::out( const char * str )
{
	if( m_is_new_line )
	{
		// Tag stack must be printed
		show_tag_stack();
	}

	m_real_err_stream->out( str );
	if( str )
	{
		size_t l = strlen( str );
		if( l && '\n' == str[ l - 1 ] )
			m_is_new_line = true;
		else
			m_is_new_line = false;
	}

	return *this;
}

void
es_tag_stack_t::show_tag_stack()
{
	size_t tag_count = m_parser.tag_stack_size();
	if( tag_count )
	{
		m_real_err_stream->out( "'" );

		for( size_t i = 0; i < tag_count; )
		{
			m_real_err_stream->out( "{" ).
				out( m_parser.tag_stack_at( i )->query_name() );
			if( ++i < tag_count )
				m_real_err_stream->out( " " );
		}

		m_real_err_stream->out( "':\n" );
	}
}

} /* namespace cls_2 */
