/*==========================================================================*/
/* 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.                                             */
/*==========================================================================*/

#ifndef __OKLITETM_H
#define __OKLITETM_H

#include "okLiteTMResult.h"
#include "okLiteTMRecord.h"


#define okLTM_SIGNATURE "okLTM100"
#define okLTM_LSIG      8
#define okLTM_LLOC      6
#define okLTM_LCS       50
#define okLTM_LHEADER   okLTM_LSIG+(2*okLTM_LLOC)+okLTM_LCS+(3*sizeof(long))

// Buffer at the start of each key for the file position (long)
#define okLTM_STARTKEY  (sizeof(long))

// Return values of the Add() method
#define okLTM_ADD_ERROR      1
#define okLTM_ADD_ADDED      2
#define okLTM_ADD_EMPTY      3
#define okLTM_ADD_DUPLICATE  4

// End of database reached
#define okLTM_EOF            (-1)


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

class okLiteTM
{
public:

	/// Constructor
	okLiteTM ();

	/// Destructor
	~okLiteTM ();

	/// Converts normal text to XML format
	const char* ConvertToXML (string& p_rsText,
		const bool p_bAttribute,
		const bool p_bUnicodeCompatible = false);

	/// Returns true if a TM is opened
	bool IsOpen ()
		{ return(m_pFile != NULL); };

	/// Closes the TM
	void Close ();

	/// Sets the information data
	void SetInfo (const char* p_pszSourceLocale,
		const char* p_pszTargetLocale,
		const char* p_pszCodeset);

	/// Loads or creates a file
	bool Open (const char* p_pszPath,
		const bool p_bReadOnly = true,
		const char* p_pszSource = NULL,
		const char* p_pszTarget = NULL,
		const char* p_pszCodeset = NULL);

	/// Updates the header and index of the file
	bool UpdateFile ();

	/// Saves the TM in OpenTag format
	bool SaveAsOpenTag (const char* p_pszPath,
		const char* p_pszTool,
		const bool p_bOutputKeys = true);

	/// Saves the TM in TMX format
	bool SaveAsTMX (const char* p_pszPath,
		const char* p_pszTool,
		const char* p_pszToolVersion,
		const bool p_bOutputKeys = true);

	/// Add entries from tab-delimited text file
	bool AddEntriesFromFile (const char* p_pszPath,
		int* p_pnTotal,
		int* p_pnAdded,
		int* p_pnEmpty,
		int* p_pnDuplicate,
		int* p_pnError);

	/// Creates a key (you must free p_ppData after use)
	bool MakeKey (const char* p_pszText,
		char** p_ppData,
		int* p_pnLength);

	/// Adds a new record to the TM
	int Add (const char* p_pszSource,
		const char* p_pszTarget,
		const char* p_pszType = "",
		const char* p_pszId = "",
		const int p_nX = -1,
		const int p_nY = -1,
		const int p_nCX = -1,
		const int p_nCY = -1);

	/// Queries the TM, returns the number of matches
	int Query (const char* p_pszQuery,
		const int p_nThreshold,
		const bool p_bConcordance,
		okLiteTMResultList* p_pResults);

	/// Returns the number of records in the TM
	long GetCount ()
		{ return(m_lCount); };

	/// Returns the source locale
	const char* GetSrcLocale ()
		{ return(m_sSrcLocale.c_str()); };

	/// Returns the target locale
	const char* GetTrgLocale ()
		{ return(m_sTrgLocale.c_str()); };

	/// Returns the code set
	const char* GetCodeset ()
		{ return(m_sCodeset.c_str()); };

	/// Moves to the first record, returns its position or okLTM_EOF
	long MoveToFirstRecord ();

	/// Moves to the next record, returns its position or okLTM_EOF
	long MoveToNextRecord ();

	/// Reads the record data, use MoveToXXX to get the position
	bool Get (okLiteTMRecord& p_rRecord,
		const long p_lPos);

	/// Returns the current record number
	long GetCurrentIndex ()
		{ return(m_lCurIndex); };

	/// Returns the path of the TM
	const char* GetPath ()
		{ return(m_sPath.c_str()); };


private:

	/// Frees the data structure
	void Free ();


private:

	string               m_sSrcLocale;
	string               m_sTrgLocale;
	string               m_sCodeset;
	string               m_sPath;
	FILE*                m_pFile;
	bool                 m_bReadOnly;
	char*                m_pIndex;
	char*                m_pCurPos;
	long                 m_lIndexSize;
	long                 m_lIndexPos;
	long                 m_lCount;
	long                 m_lCurIndex;
};


#endif // __OKLITETM_H

