/*

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.

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

*/
#if !defined( _CLS_2__DEF_TAG_FORMATTER_HPP_ )
#define _CLS_2__DEF_TAG_FORMATTER_HPP_

#include <cls_2/h/cls.hpp>

namespace cls_2 {

//
// def_tag_formatter_t
//

/*
	Default implementation of tag formatter.
	Object of this type use Writter << operator
	for output tag names, tag values and curly braces.
	Object of this type use tab character (\t) to
	make indentation for children tag

	Requirement:

	Writter & operator<<( Writter &, const char * )

	must be defined.
*/

template< class Writter >
class	def_tag_formatter_t :
	public tag_formatter_t {
	public :
		def_tag_formatter_t( Writter & writter );
		virtual ~def_tag_formatter_t();

		virtual void
		start_tag( const char * tag_name );

		virtual void
		finish_tag();

		virtual void
		out_value( const char * value );

	protected :
		Writter &	m_writter;
		unsigned int	m_deep;

		void
		make_indent( unsigned int deep );
};

template< class Writter >
def_tag_formatter_t< Writter >::def_tag_formatter_t(
	Writter & writter ) :
	m_writter( writter ),
	m_deep( 0 ) {
}

template< class Writter >
def_tag_formatter_t< Writter >::~def_tag_formatter_t() {
}

template< class Writter >
void
def_tag_formatter_t< Writter >::start_tag(
	const char * tag_name ) {
	m_writter << "\n";
	make_indent( m_deep++ );

	m_writter << "{" << tag_name << "\t";
}

template< class Writter >
void
def_tag_formatter_t< Writter >::finish_tag() {
	if( m_deep )
		--m_deep;

	m_writter << "}";
}

template< class Writter >
void
def_tag_formatter_t< Writter >::out_value( const char * value ) {
	m_writter << value << " ";
}

template< class Writter >
void
def_tag_formatter_t< Writter >::make_indent( unsigned int deep ) {
	for( ; deep; --deep ) {
		m_writter << "\t";
	}
}

} /* namespace cls_2 */

#endif
