/*

Copyright (c) 2002-2005, Yauheni Akhotnikau
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

- The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

*/

#include <iostream>
#include <fstream>
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <functional>

#include <oess_1/defs/log/h/err.hpp>

#include <oess_1/defs/h/ex.hpp>
#include <oess_1/scheme/h/scheme.hpp>
#include <oess_1/scheme/h/text_parser.hpp>

#include <oess_1/scheme/impl/h/serializer_gen.hpp>

#include <oess_1/defs/util/h/logo.hpp>

using namespace std;

void
load_ddl(
	const char * ddl_file_name,
	oess_1::scheme::scheme_t & sch )
{
	std::ifstream in( ddl_file_name, std::ios::in );
	if( in ) {
		std::string error_desc;

		int ddl_parse_error = oess_1::scheme::parse_text(
			in, sch, error_desc, ddl_file_name );
		if( ddl_parse_error )
			OESS_THROW_LOGIC( ddl_parse_error, error_desc )
	}
	else
		OESS_THROW_PHYSIC( errno,
			ddl_file_name << ": " << errno
			<< ", " << strerror( errno ) );
}

class	gen_code_for_type_t :
	public std::unary_function<
		const oess_1::scheme::type_t &, void >
{
	private :
		std::ostream &	m_to;
	public :
		inline
		gen_code_for_type_t(
			std::ostream & to ) : m_to( to ) {
		}

		inline result_type
		operator()( argument_type a ) {
			if( a.is_defined() ) {
				oess_1::scheme::impl::generate_cpp_serializer(
					a, m_to );
				m_to << std::endl;
			}
		}
};

void
gen_code( const char * cpp_file_name,
	const oess_1::scheme::scheme_t & sch )
{
	std::ofstream to( cpp_file_name );
	if( to ) {
		oess_1::scheme::pseudo_type_list_t types( sch );
		std::for_each( types.begin(), types.end(),
			gen_code_for_type_t( to ) );
	}
	else
		OESS_THROW_PHYSIC( errno,
			cpp_file_name << ": " << errno
			<< ", " << strerror( errno ) );
}

int
main( int argc, char ** argv ) {
	if( 3 == argc ) {
		try {
			oess_1::scheme::scheme_auto_ptr_t sch_ptr;

			load_ddl( argv[ 1 ], *sch_ptr );
			gen_code( argv[ 2 ], *sch_ptr );

			return 0;
		}
		catch( oess_1::ex_t & x ) {
			std::cerr << x.query_err_code() << std::endl;
		}
		catch( ... ) {
			OESS_1_LOG_ERR( "Oops! Unknown exception!" << std::endl )
		}
	}
	else {
		std::cerr << OESS_1__LOGO << std::endl
			<< "Generate C++ code for object serialization\n"
			"\n"
			"oess_cpp_serializer <ddl-file> <cpp-file>"
			<< std::endl;
	}

	return 1;
}