/*
	ZBinaryBufferWriter.cpp
	Author: Patrick Baggett <ptbaggett@762studios.com>
	Created: 6/05/2013

	Purpose: 

	Writes a binary stream to memory

	License:

	TODO

*/

#include <ZUtil/ZBinaryBufferWriter.hpp>

/*************************************************************************/

void ZBinaryBufferWriter::WriteU8(uint8_t v)
{
	Data[Offset] = v;
	++Offset;
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU16(uint16_t v)
{
	if(GetStreamByteOrder() != SST_BYTEORDER_HOST)
		v = SST_OS_ByteSwap16(v);

	memcpy(Data+Offset, &v, sizeof(uint16_t));

	Offset += sizeof(uint16_t);
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU32(uint32_t v)
{
	if(GetStreamByteOrder() != SST_BYTEORDER_HOST)
		v = SST_OS_ByteSwap32(v);

	memcpy(Data+Offset, &v, sizeof(uint32_t));

	Offset += sizeof(uint32_t);

}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU64(uint64_t v)
{
	if(GetStreamByteOrder() != SST_BYTEORDER_HOST)
		v = SST_OS_ByteSwap64(v);

	memcpy(Data+Offset, &v, sizeof(uint64_t));

	Offset += sizeof(uint64_t);
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU8Array(const uint8_t* v, size_t count)
{
	memcpy(Data+Offset, v, count);

	Offset += sizeof(uint8_t) * count;
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU16Array(const uint16_t* v, size_t count)
{
	if(GetStreamByteOrder() == SST_BYTEORDER_HOST)
		memcpy(Data+Offset, v, count*sizeof(uint16_t));
	else
	{
		for(size_t i=0; i<count; i++)
		{
			uint16_t value = SST_OS_ByteSwap16(v[i]);
			memcpy(Data+Offset+i*sizeof(uint16_t), &value, sizeof(uint16_t));
		}
	}
	
	Offset += sizeof(uint16_t) * count;
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU32Array(const uint32_t* v, size_t count)
{
	if(GetStreamByteOrder() == SST_BYTEORDER_HOST)
		memcpy(Data+Offset, v, count*sizeof(uint32_t));
	else
	{
		for(size_t i=0; i<count; i++)
		{
			uint32_t value = SST_OS_ByteSwap32(v[i]);
			memcpy(Data+Offset+i*sizeof(uint32_t), &value, sizeof(uint32_t));
		}
	}

	Offset += sizeof(uint32_t) * count;
}

/*************************************************************************/

void ZBinaryBufferWriter::WriteU64Array(const uint64_t* v, size_t count)
{
	if(GetStreamByteOrder() == SST_BYTEORDER_HOST)
		memcpy(Data+Offset, v, count*sizeof(uint16_t));
	else
	{
		for(size_t i=0; i<count; i++)
		{
			uint64_t value = SST_OS_ByteSwap64(v[i]);
			memcpy(Data+Offset+i*sizeof(uint64_t), &value, sizeof(uint64_t));
		}
	}

	Offset += sizeof(uint64_t) * count;
}


/*************************************************************************/

void ZBinaryBufferWriter::WritePointer(void* pointer)
{
	uintptr_t ptrValue = (uintptr_t)pointer;

	if(GetStreamByteOrder() == SST_BYTEORDER_HOST)
		memcpy(Data+Offset, &ptrValue, sizeof(uintptr_t));
	else
	{
		switch(sizeof(uintptr_t))
		{
			case sizeof(uint16_t): WriteU16((uint16_t)ptrValue); break;
			case sizeof(uint32_t): WriteU32((uint32_t)ptrValue); break;
			case sizeof(uint64_t): WriteU64((uint64_t)ptrValue); break;
		}
	}

	Offset += sizeof(uintptr_t);
}

/*************************************************************************/
