/*
	ZBinaryFileWriter.cpp
	Author: Chris Ertel <crertel@762studios.com>, Patrick Baggett <ptbaggett@762studios.com>
	Created: 3/11/2013

	Purpose: 

	Writes a binary stream from a SST_File handle. NOTE: On 32-bit platforms, only files up to 4GB are supported.

	License:

	TODO

*/

#include <ZUtil/ZBinaryFileWriter.hpp>

/*
	TODO: This implementation makes use of SST_File, which is a thin wrapper over system calls. Because of this,
	small writes can reduce performance. A 4KB buffer (or better, one that is a multiple of inode size) would
	help reduce the overhead.
*/

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

ZBinaryFileWriter::ZBinaryFileWriter(SST_File handle, SST_ByteOrder bo) :
ZBinaryWriter(bo), fp(handle)
{
	uint64_t offset64;

	SST_OS_GetFilePointer(fp, &offset64);

	Offset = (size_t)offset64;

}

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

void ZBinaryFileWriter::WriteU8(uint8_t v)
{
	SST_OS_WriteFile(fp, &v, sizeof(uint8_t));

	++Offset;
}

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

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

	SST_OS_WriteFile(fp, &v, sizeof(uint16_t));

	Offset += sizeof(uint16_t);
}

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

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

	SST_OS_WriteFile(fp, &v, sizeof(uint32_t));

	Offset += sizeof(uint32_t);

}

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

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

	SST_OS_WriteFile(fp, &v, sizeof(uint64_t));

	Offset += sizeof(uint64_t);
}

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

void ZBinaryFileWriter::WriteU8Array(const uint8_t* v, size_t count)
{
	SST_OS_WriteFile(fp, v, count);

	Offset += sizeof(uint8_t) * count;
}

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

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

	Offset += sizeof(uint16_t) * count;

}

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

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

	Offset += sizeof(uint32_t) * count;

}

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

void ZBinaryFileWriter::WriteU64Array(const uint64_t* v, size_t count)
{
	if(GetStreamByteOrder() == SST_BYTEORDER_HOST)
		SST_OS_WriteFile(fp, v, count * sizeof(uint64_t));
	else
	{
		for(size_t i=0; i<count; i++)
		{
			uint64_t value = SST_OS_ByteSwap64(v[i]);
			SST_OS_WriteFile(fp, &value, sizeof(uint64_t));
		}
	}

	Offset += sizeof(uint64_t) * count;

}


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

void ZBinaryFileWriter::SeekTo(size_t offset)
{
	uint64_t offset64 = (uint64_t)offset;	//Because size_t can be 32 bits, casting to int64_t can sign extend (bad), cast to uint64_t first so it is zero-extended.
	SST_OS_SeekFile(fp, (int64_t)offset64, SST_SEEK_START);

	Offset = offset;
}

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