//----------------------------------------------------------------------------------------------
// <copyright file="LqrSMACException.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// Implements the LiquidRobotics::SMACFramework::Exception class, which is a base class for all
// exception classes.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------------------

#include "LqrSMACException.h"
#include <sstream>
#include <string.h>

using namespace std;

namespace LiquidRobotics {
namespace SMACFramework {

// **** Exception ****

Exception::Exception(const char* p1) throw()
{
	buffer[0] = '\0';
	Add(p1);
}

Exception::Exception(const char* p1, const char* p2) throw()
{
	buffer[0] = '\0';
	Add(p1);
	Add(p2);
}

Exception::Exception(const char* p1, const char* p2, const char* p3) throw()
{
	buffer[0] = '\0';
	Add(p1);
	Add(p2);
	Add(p3);
}

Exception::Exception(const char* p1, const char* p2, const char* p3, const char* p4) throw()
{
	buffer[0] = '\0';
	Add(p1);
	Add(p2);
	Add(p3);
	Add(p4);
}

Exception::Exception(const char* p1, const char* p2, const char* p3, const char* p4, const char* p5) throw()
{
	buffer[0] = '\0';
	Add(p1);
	Add(p2);
	Add(p3);
	Add(p4);
	Add(p5);
}

Exception::Exception(const char* p1, const char* p2, const char* p3, const char* p4, const char* p5, const char* p6) throw()
{
	buffer[0] = '\0';
	Add(p1);
	Add(p2);
	Add(p3);
	Add(p4);
	Add(p5);
	Add(p6);
}

Exception::Exception(const Exception& other) throw()
{
	strncpy(buffer, other.buffer, sizeof(buffer));
	buffer[sizeof(buffer)-1] = '\0';
}

Exception& Exception::operator=(const Exception& other)
{
	if (this != &other)
	{
		strncpy(buffer, other.buffer, sizeof(buffer));
		buffer[sizeof(buffer)-1] = '\0';
	}

	return *this;
}

const char* Exception::what() const throw()
{
	return buffer;
}

void Exception::Add(const char* data) throw()
{
	if (data && *data)
	{
		int bufferLen = strlen(buffer);
		int appendCount = sizeof(buffer)-bufferLen-1;

		if (appendCount > 0)
		{
			strncat(buffer, data, appendCount);
			buffer[sizeof(buffer)-1] = '\0';
		}
	}
}

// *** AlreadyConnected ***

AlreadyConnected::AlreadyConnected() throw()
: Exception("Connection has already been established")
{
}

// *** AlreadyInitialized ***

AlreadyInitialized::AlreadyInitialized() throw()
: Exception("This object has already been initialized")
{
}

// *** InterfaceNotFound ***

InterfaceNotFound::InterfaceNotFound(const char* message) throw()
: Exception(message)
{
}

InterfaceNotFound::InterfaceNotFound(const char* instanceName, const char* typeName, const char* message) throw()
: Exception("Interface for type ", typeName, " was not found as ", instanceName, message ? ": " :  NULL, message)
{
}

// *** InvalidArgument ***

InvalidArgument::InvalidArgument(const char* parameterName, const char* message) throw()
: Exception(MakeMessage(parameterName, message).c_str())
{
}

string InvalidArgument::MakeMessage(const char* parameterName, const char* message) throw()
{
	stringstream sstm;
	sstm << "Invalid argument " << parameterName;

	if (message && *message)
		sstm << " (" << message << ")";

	string str = sstm.str();
	str.c_str();
	return str;
}

// *** ItemNotFound ***

ItemNotFound::ItemNotFound() throw()
: Exception("No match was found for the given criteria")
{
}

ItemNotFound::ItemNotFound(const char* message, const char* criteria) throw()
: Exception(MakeMessage(message, criteria).c_str())
{
}

string ItemNotFound::MakeMessage(const char* message, const char* criteria) throw()
{
	string returnValue;

	stringstream sstm;
	sstm << message << " (criteria = " << criteria << " )";

	returnValue = sstm.str();
	returnValue.c_str();
	return returnValue;
}

// **** NoSuchInstance ****

NoSuchInstance::NoSuchInstance(const char* name, const char* kind) throw()
: Exception(MakeMessage(name, kind).c_str())
{
}


string NoSuchInstance::MakeMessage(const char* name, const char* kind) throw()
{
	try
	{
		stringstream sstm;

		sstm << "No instance found for name=\"" << name << "\"";

		if ((kind != NULL) && *kind)
			sstm << " kind=\"" << kind << "\"";

		string str = sstm.str();
		str.c_str();

		return str;
	}
	catch(...)
	{
		return "No instance found for name";
	}
}

// *** NotConnected ***

NotConnected::NotConnected() throw()
: Exception("No connection has been established")
{
}

// *** NotInitialized ***

NotInitialized::NotInitialized() throw()
: Exception("This object has not been initialized")
{
}

// **** OrbAlreadyInitialized ****

OrbAlreadyInitialized::OrbAlreadyInitialized() throw()
: Exception("The ORB has already been initialized")
{
}


// **** OrbNotInitialized ****

OrbNotInitialized::OrbNotInitialized() throw()
: Exception("The ORB has not been initialized")
{
}

// *** SystemException ***

#if 0
SystemException::SystemException(const CORBA::SystemException& sysEx) throw()
: Exception(MakeReportString(sysEx).c_str())
{
}

std::string SystemException::MakeReportString(const CORBA::SystemException& sysEx) throw()
{
	try
	{
		std::stringstream sstm;
		sstm << "Caught CORBA system exception: completed=";

		switch(sysEx.completed())
		{
		case CORBA::COMPLETED_NO:	sstm << "no";		break;
		case CORBA::COMPLETED_YES:	sstm << "yes";		break;
		default:					sstm << "maybe";	break;
		}
#ifdef minor
#undef minor
#endif
		sstm << " minor=" << sysEx.minor();

		string str = sstm.str();
		str.c_str();

		return str;
	}
	catch(...)
	{
		return "Caught CORBA system exception";
	}
}
#endif

// *** WouldOverwrite ***

WouldOverwrite::WouldOverwrite() throw()
: Exception("The operation would perform a disallowed overwrite")
{
}

WouldOverwrite::WouldOverwrite(const char* message) throw()
: Exception(message)
{
}


}
}
