/*

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.

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

*/
#include <assert.h>
#include <string.h>

#include <cls_2/h/tag_text.hpp>
#include <cls_2/h/to_cls_format.hpp>

namespace cls_2 {

//
// tag_text_t
//
const size_t	c_text_capacity = 32;

tag_text_t::tag_text_t(
	const char * name,
	bool mandatory )
:
	tag_t( name ),
	m_len( 0 ),
	m_is_mandatory( mandatory ),
	m_is_defined( false ),
	m_is_space_found( false )
{
	m_value.set( new char[ c_text_capacity ],
		c_text_capacity );
	m_value.query()[ m_len ] = 0;
}

tag_text_t::tag_text_t(
	cls_2::tag_t & owner,
	const char * name,
	bool mandatory )
:
	tag_t( owner, name ),
	m_len( 0 ),
	m_is_mandatory( mandatory ),
	m_is_defined( false ),
	m_is_space_found( false )
{
	m_value.set( new char[ c_text_capacity ],
		c_text_capacity );
	m_value.query()[ m_len ] = 0;
}

tag_text_t::~tag_text_t() {
}

int
tag_text_t::on_start(
	parser_context_t & context ) {

	if( m_is_defined ) {
		context.show_error( query_name() );
		context.show_error( context.find_msg(
			c_msg_tag_already_defined ) );
		return c_semantic_error;
	}

	m_is_space_found = false;

	return c_ok;
}


int
tag_text_t::on_finish(
	parser_context_t & context ) {

	int ret_code = tag_t::on_finish( context );

	if( c_ok == ret_code ) {
		m_is_defined = true;
	}

	return ret_code;
}

int
tag_text_t::on_tok_space(
	parser_context_t & context,
	const char * tok_value ) {
	assert( tok_value );

	m_is_space_found = true;

	return c_ok;
}

int
tag_text_t::on_tok_nonspace(
	parser_context_t & context,
	const char * tok_value ) {
	assert( tok_value );

	append_value( tok_value );

	return c_ok;
}

int
tag_text_t::on_tok_string(
	parser_context_t & context,
	const char * tok_value ) {
	assert( tok_value );

	append_value( tok_value );

	return c_ok;
}

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

bool
tag_text_t::is_defined() const {
	return m_is_defined;
}

void
tag_text_t::reset() {
	m_is_defined = false;
	m_is_space_found = false;
	m_value.query()[ m_len = 0 ] = 0;
}

const char *
tag_text_t::query_value() const {
	return m_value.query();
}

void
tag_text_t::make_defined( const char * value )
{
	reset();
	append_value( value );

	m_is_defined = true;
}

void
tag_text_t::append_value(
	const char * value ) {
	assert( value );

	if( m_is_space_found && m_len ) {
		concat( " " );
		m_is_space_found = false;
	}

	concat( value );
}

void
tag_text_t::concat(
	const char * value ) {
	assert( value );

	size_t value_len = strlen( value );
	size_t required_capacity = m_len + value_len + 1;
	// Align to the 8 boundary
	required_capacity +=
		8 - ( required_capacity & 0x7 );

	if( required_capacity >= m_value.size() ) {
		size_t new_capacity =
			required_capacity + ( required_capacity >> 1 );
		auto_ptr_3::vect_ptr_t< char > new_value(
			new char[ new_capacity ],
			new_capacity );

		memcpy( new_value.query(),
			m_value.query(),
			m_value.size() );

		m_value.swap( new_value );
	}

	strcpy( m_value.query() + m_len, value );
	m_len += value_len;
	m_value.query()[ m_len ] = 0;
}

void
tag_text_t::on_format(
	tag_formatter_t & fmt ) {

	auto_ptr_3::vect_ptr_t< char > cls_text(
		to_cls_format_t::format_nonspace( query_value() ), 0 );
	fmt.out_value( cls_text.query() );
}

} /* namespace cls_2 */
