#include <cstdio>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#include "exceptions.h"

namespace fft
{
	//-------------------------------------------------------------------------
	// 
	// BaseException class implementation
	// 
	//-------------------------------------------------------------------------

	BaseException::BaseException(const char* fmt, ...) throw()
	: message(0)
	{
		va_list args;
		va_start(args,fmt);
		vasprintf(&message, fmt, args);
		va_end(args);
	}

	BaseException::BaseException(const BaseException& src)
	: exception(), message(0)
	{
		message = strdup(src.message);
	}

	BaseException& BaseException::operator=(const BaseException& rhs)
	{
		if (message)
		{
			free(message);
		}
		message = strdup(rhs.message);

		return *this;
	}

	BaseException::~BaseException() throw()
	{
		free(message);
	}


	//-------------------------------------------------------------------------
	// 
	// OutOfRangeException class implementation
	// 
	//-------------------------------------------------------------------------
	OutOfRangeException::OutOfRangeException(u32 rgMin, u32 rgMax, u32 curVal)
		: BaseException("Value %d gets out of range [%d, %d]", curVal, rgMin, rgMax),
		rangeMin(rgMin), rangeMax(rgMax), currentVal(curVal)
	{
	}

	OutOfRangeException::OutOfRangeException(const OutOfRangeException& src)
		: BaseException(src), rangeMin(src.rangeMin), rangeMax(src.rangeMax),
		currentVal(src.currentVal)
	{
	}

	OutOfRangeException& OutOfRangeException::operator=(const OutOfRangeException& rhs)
	{
		if (message)
		{
			free(message);
		}

		message = strdup(rhs.message);
		rangeMin = rhs.rangeMin;
		rangeMax = rhs.rangeMax;
		currentVal = rhs.currentVal;

		return *this;
	}

	//-------------------------------------------------------------------------
	// 
	// FileNotFoundException class implementation
	// 
	//-------------------------------------------------------------------------

	FileNotFoundException::FileNotFoundException(const char* file)
	: BaseException("File %s not found", file), fileName(file)
	{
	}

	FileNotFoundException::FileNotFoundException(const FileNotFoundException& src)
	: BaseException(src),fileName(src.fileName)
	{
	}

	FileNotFoundException& FileNotFoundException::operator=(const FileNotFoundException& rhs)
	{
		if (message)
		{
			free(message);
		}

		message = strdup(rhs.message);
		fileName = rhs.fileName;

		return *this;
	}

}

