//-----------------------------------------------------------------------------------
// <copyright file="LqrSMACPrintfBuffer.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>
//   Implementation of PrintfBuffer class, which creates a string buffer initialized
//   with a printf-style argument list
// </summary>
//
// <owner>Mike Cookson</owner>
//-----------------------------------------------------------------------------------

#include "LqrSMACPrintfBuffer.h"
#include <stdlib.h>
#include <stdio.h>

using namespace std;

namespace LiquidRobotics {
namespace SMACFramework {


PrintfBuffer::PrintfBuffer(const char* format, ...)
: ptr(str.c_str())
{
	va_list argptr;
	char* data = NULL;

	va_start(argptr, format);
	const int numChars = vasprintf(&data, format, argptr);
	va_end(argptr);

	if (numChars && data && *data)
		try
		{
			str.assign(data, numChars);
			ptr = str.c_str();
		}
		catch(...)
		{
			if (data) free(data);
			throw;
		}

	if (data) free(data);
}

PrintfBuffer::PrintfBuffer(const char* format, va_list argptr)
: ptr(str.c_str())
{
	char* data = NULL;

	const int numChars = vasprintf(&data, format, argptr);

	if (numChars && data && *data)
		try
		{
			str.assign(data, numChars);
			ptr = str.c_str();
		}
		catch(...)
		{
			if (data) free(data);
			throw;
		}

	if (data) free(data);
}

PrintfBuffer::PrintfBuffer(const PrintfBuffer& other)
: str(other.str)
, ptr(str.c_str())
{
}

PrintfBuffer::~PrintfBuffer()
{
}

PrintfBuffer& PrintfBuffer::operator=(const PrintfBuffer& other)
{
	if (this != &other)
	{
		str = other.str;
		ptr = str.c_str();
	}

	return *this;
}


const char* PrintfBuffer::AsPtr() const
{
	return ptr;
}

const PrintfBuffer::string& PrintfBuffer::AsStr() const
{
	return str;
}

std::ostream& operator<<(std::ostream& out, const PrintfBuffer& message)
{
	out << message.AsStr();
	return out;
}

}
}
