#include "MPT_Exception.h"

namespace MobilePhotoTool{

	MPT_Exception::MPT_Exception()
	{
		this->description	= NULL;
		this->file			= NULL;
		this->line			= NULL;
		this->error			= NULL;
		this->exception		= false;

		this->description	= new char[this->maxStringlength];
		if(this->description != NULL){
			this->file			= new char[this->maxStringlength];
			if(this->file != NULL){
				this->line = new char[this->maxStringlength];
				if(this->line != NULL){
					this->error = new char[this->maxStringlength * 3 + 5];
				}
			}
		
		}
	}


	MPT_Exception::~MPT_Exception(void)
	{
		if(this->description != NULL){
			delete []this->description;
		}
		if(this->file != NULL){
			delete []this->file;
		}
		if(this->line != NULL){
			delete []this->line;
		}

		if(this->error != NULL){
			delete []this->error;
		}
	}

	char* MPT_Exception::toString()
	{
		return error;  
	}

	void MPT_Exception::setError(const char *description, const char *file, int line){
		if(	(int)strlen(description) < this->maxStringlength &&
			(int)strlen(file)	< this->maxStringlength){
				
				strcpy(this->description, description);
				strcpy(this->file, file);
				sprintf(this->line,"%d", line);
				
				strcpy(this->error, " ");
				strcat(this->error, this->description);
				strcat(this->error, " ");
				strcat(this->error, this->file);
				strcat(this->error, " ");
				strcat(this->error, this->line);

		}else{
			strcpy(this->error,"The exception message is to long");
			strcat(this->error, this->line);
		}

		this->exception = true;
	}
}
