/*==========================================================================*/
/* Copyright (c) 1999 - Sykes Enterprises Inc. - All rights reserved        */
/*--------------------------------------------------------------------------*/
/* Sykes grants you ("Licensee") a non-exclusive, royalty free, license to  */
/* use, modify and redistribute this software in source and binary code     */
/* form, provided that i) this copyright notice and license appear on all   */
/* copies of the software; and ii) Licensee does not utilize the software   */
/* in a manner which is disparaging to Sykes.                               */
/*                                                                          */
/* This software is provided "AS IS", without a warranty of any kind. ALL   */
/* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING */
/* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR        */
/* PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SYKES AND ITS          */
/* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A  */
/* RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS           */
/* DERIVATIVES. IN NO EVENT WILL SYKES OR ITS LICENSORS BE LIABLE FOR ANY   */
/* LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,          */
/* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND        */
/* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR      */
/* INABILITY TO USE SOFTWARE, EVEN IF SYKES HAS BEEN ADVISED OF THE         */
/* POSSIBILITY OF SUCH DAMAGES.                                             */
/*==========================================================================*/

#include "okLiteTMRecord.h"


/*--------------------------------------------------------------------------*/

bool okLiteTMRecord::Read
(
	FILE*                p_pFile
)
{
	unsigned short       usSize;
	char                 szBuf[okLTM_LBUF+1];


	// Source
	fread(&usSize, 1, sizeof(usSize), p_pFile);
	fread(szBuf, usSize, 1, p_pFile);
	// Security test in case the index is corrupted or the
	// lPos value is incorrect
	if (( usSize < 0 ) || ( usSize > okLTM_LBUF ))
	{
		// Invalid size, probably because of bad lPos value
		return(false);
	}
	szBuf[usSize] = 0;
	m_sSource = szBuf;

	// Traget
	fread(&usSize, 1, sizeof(usSize), p_pFile);
	fread(szBuf, usSize, 1, p_pFile);
	szBuf[usSize] = 0;
	m_sTarget = szBuf;

	// Type
	fread(&usSize, 1, sizeof(usSize), p_pFile);
	fread(szBuf, usSize, 1, p_pFile);
	szBuf[usSize] = 0;
	m_sType = szBuf;
		
	// Id
	fread(&usSize, 1, sizeof(usSize), p_pFile);
	fread(szBuf, usSize, 1, p_pFile);
	szBuf[usSize] = 0;
	m_sId = szBuf;

	// X
	fread(&m_nX, 1, sizeof(m_nX), p_pFile);
	// Y
	fread(&m_nY, 1, sizeof(m_nY), p_pFile);
	// CX
	fread(&m_nCX, 1, sizeof(m_nCX), p_pFile);
	// CY
	fread(&m_nCY, 1, sizeof(m_nCY), p_pFile);

	return(true);
}


/*--------------------------------------------------------------------------*/

bool okLiteTMRecord::Write
(
	FILE*                p_pFile,
	long&                p_rlIndexPos
)
{
	unsigned short       usSize;


	// Source
	usSize = m_sSource.length();
	fwrite(&usSize, 1, sizeof(usSize), p_pFile);
	p_rlIndexPos += sizeof(usSize);
	fwrite(m_sSource.c_str(), usSize, 1, p_pFile);
	p_rlIndexPos += usSize;

	// Traget
	usSize = m_sTarget.length();
	fwrite(&usSize, 1, sizeof(usSize), p_pFile);
	p_rlIndexPos += sizeof(usSize);
	fwrite(m_sTarget.c_str(), usSize, 1, p_pFile);
	p_rlIndexPos += usSize;

	// Type
	usSize = m_sType.length();
	fwrite(&usSize, 1, sizeof(usSize), p_pFile);
	p_rlIndexPos += sizeof(usSize);
	fwrite(m_sType.c_str(), usSize, 1, p_pFile);
	p_rlIndexPos += usSize;
		
	// Id
	usSize = m_sId.length();
	fwrite(&usSize, 1, sizeof(usSize), p_pFile);
	p_rlIndexPos += sizeof(usSize);
	fwrite(m_sId.c_str(), usSize, 1, p_pFile);
	p_rlIndexPos += usSize;

	// X
	fwrite(&m_nX, 1, sizeof(m_nX), p_pFile);
	p_rlIndexPos += sizeof(m_nX);
	// Y
	fwrite(&m_nY, 1, sizeof(m_nY), p_pFile);
	p_rlIndexPos += sizeof(m_nY);
	// CX
	fwrite(&m_nCX, 1, sizeof(m_nCX), p_pFile);
	p_rlIndexPos += sizeof(m_nCX);
	// CY
	fwrite(&m_nCY, 1, sizeof(m_nCY), p_pFile);
	p_rlIndexPos += sizeof(m_nCY);

	return(true);
}

