/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

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.
////////////////////////////////////////////////////////////////////////////////
#Title: xbCompressItf
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"
#include "xbCompressItf.h"

#include "_xbVer.h"

#include "xbCompressLZSS.h"

xbEimpl(xbEcompress,xbEadv);
xbEimpl(xbEcompressUser,xbEcompress);
xbEimpl(xbEcompressCorrupt,xbEcompress);

xbCompressPlugType	g_xbCompressPlugType("Compress",XDCBASE_HSVER,"xdcBase HotPlug Compression");
xbHotPlugFactoryList g_EmbeddedCompressFactList[] =
{
	{"default",xbLZSSplug::CreateObject,0,NULL},
	{"eplz",xbLZSSplug::CreateObject,3,NULL},
	{"eplzss",xbLZSSplug::CreateObject,3,NULL},
	{NULL,NULL,0,NULL}
};

xbHotPlugFactory g_EmbeddedCompressFact(NULL,&g_xbCompressPlugType,"EmbedEpLZSS","Embedded eplzss",g_EmbeddedCompressFactList,0,xbHotPlugLinkEmbedded);


/*
bool	xbCompressPlugType::CreatePluggedObject(const xbString &name,
	xbRefPtr<IxbCompressPlug> *pprObj,xbRefPtr<IxbHotPlugin> *pprPlugin)
{
	return xbHotPlugType::CreatePluggedObject(name,pprObj,pprPlugin);
	xbRefPtr<IxbObject> prObj;
	if (CreatePluggedObject(name,pprObj?&prObj:NULL,pprPlugin))
	{
		if (pprObj)
		{
			*pprObj = xbRefQIPtr<IxbCompressPlug>(prObj);
		}
		return true;
	}
	if (name.CompareNoCase("eplz") == 0 || name.CompareNoCase("eplzss") == 0)
	{
		if (pprPlugin)
			pprPlugin->Empty();
		if (!pprObj)
			return true;

		xbLZSSplug *plg = xbDnew xbLZSSplug();
		if (!plg) xbThrow CMemoryException();
		try
		{
	//		plg->setFactory(pFactory);
			plg->Create();

			(*pprObj) = plg;

			plg->Release();
		}
		catch(...)
		{
			plg->Release();
			throw;
		}
		return true;
	}
	return false;
}

bool	xbCompressPlugType::CanWorkWith(IxbHotPlugin *pObj,const xbString &name)
{
	if (xbHotPlugType::CanWorkWith(pObj,name))
		return true;
	if (!pObj && (name.CompareNoCase("eplz") == 0 || name.CompareNoCase("eplzss") == 0))
		return true;
	return false;
}*/

int		xbCompressPlugType::PrioritySort(const Object &obj1,const Object &obj2)
{
	if (obj1.m_priority > obj2.m_priority)
		return 1;
	if (obj1.m_priority < obj2.m_priority)
		return -1;
	return obj1.m_name.CompareNoCase(obj2.m_name);
}

int		xbCompressPlugType::GetSupportedEncoding(xbArray<xbString> *pArrEncode)
{
	xbLock lckStart(this);
//	checkPlugins();
	loadConfig();

	xbArray<Object>	arrObj;
//	bool bEPLZSS = false;
	{
		xbSectLock lck(&m_sect);
		Object *pObj = m_arrObj.Begin();
		Object *pEnd = m_arrObj.End();
		while(pObj < pEnd)
		{
			xbString name(pObj->m_name);
			name.OwnData();
			//if (name.CompareNoCase("eplzss") == 0)
			//	bEPLZSS = true;
			arrObj.Add(Object(name,pObj->m_priority,NULL,NULL));
			pObj++;
		}
	}
	//if (!bEPLZSS)
	//	arrObj.Add(Object("eplzss",0x10000,NULL,NULL));
	if (pArrEncode)
	{
		arrObj.Sort(PrioritySort);
		pArrEncode->Length = arrObj.Length;
		xbString *pStr = pArrEncode->Begin();
		Object *pObj = arrObj.Begin();
		Object *pEnd = arrObj.End();
		while(pObj < pEnd)
		{
			*pStr = pObj->m_name;
			pObj++;
			pStr++;
		}
	}

	return arrObj.Length;
}
