/*

args_4: Command line arguments parsing library
Yauheni A. Akhotnikau (C) 2002-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.

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

*/
/*!
	Args-4
*/
#include <cassert>
#include <cstring>

#include <args_4/h/args_iostream.hpp>

namespace args_4
{

/*
** iostream_error_stream_t
*/

iostream_error_stream_t::iostream_error_stream_t() :
	m_stream( &std::cerr ) {
}

iostream_error_stream_t::iostream_error_stream_t(
	std::ostream * stream ) :
	m_stream( stream ) {

	assert( m_stream );
}

iostream_error_stream_t::~iostream_error_stream_t() {
}

error_stream_t &
iostream_error_stream_t::out( const char * str ) {
	if( m_stream ) {
		*m_stream << ( str ? str : "NULL" );
	}

	return *this;
}

/*
** iostream_with_file_line_num_t
*/
iostream_with_file_line_num_t::
iostream_with_file_line_num_t(
	const words_from_file_t & file ) :
	m_file( file ),
	m_is_new_line( true ) {
}

iostream_with_file_line_num_t::
iostream_with_file_line_num_t(
	const words_from_file_t & file,
	std::ostream * stream ) :
	iostream_error_stream_t( stream ),
	m_file( file ),
	m_is_new_line( true ) {
}

iostream_with_file_line_num_t::
~iostream_with_file_line_num_t() {
}

error_stream_t &
iostream_with_file_line_num_t::
out( const char * str ) {
	if( m_is_new_line ) {
		// File name and line number must be printed
		if( m_stream ) {
			*m_stream << m_file.query_file_name()
				<< ":" << m_file.query_line() << ": ";
		}
		m_is_new_line = false;
	}

	iostream_error_stream_t::out( str );
	if( str ) {
		int l = strlen( str );
		if( l && '\n' == str[ l - 1 ] )
			m_is_new_line = true;
	}

	return *this;
}

} /* namespace args_4 */

