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

#include "xbLibTools.h"

#include "xbFileTools.h"

#include "_xbVer.h"

#ifndef XDCBASE_EMBED

volatile DWORD xbXdcLib::s_cChg = 0;
xbXdcLib *xbXdcLib::s_pFirst = NULL;
xbSectShareSimple	xbXdcLib::s_share;

xbString xbXdcLib::GetFilePath() const 
{
	return ::xbGetFilePath(GetInstance());
}

xbXdcLib::xbXdcLib(const char *pName,const char *pVer)
	:m_pPrev(NULL),m_pNext(NULL),m_hModule(NULL),m_pName(pName),m_pVer(pVer)
{
}

xbXdcLib::~xbXdcLib()
{
	eProtectBegin();
	if (m_pPrev || m_pNext || m_hModule)
	{
		dllStop(m_hModule);
		xbThrowEsysLowFmt("xbXdcLib(%s)::dllStop: not called before globals destruction !!",m_pName);
	}
	eProtectEndTrace();
}

void xbXdcLib::dllMainCall(HMODULE hModule,DWORD dwReason)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
		dllStart(hModule);
		return;
	}
	if (dwReason == DLL_PROCESS_DETACH)
	{
		dllStop(hModule);
		return;
	}
}

void xbXdcLib::dllStart(HMODULE hModule)
{
	if (!hModule)
		xbThrowEsysLowFmt("xbXdcLib(%s)::dllStart: hModule == NULL !!",m_pName);
	if (m_pPrev || m_pNext || m_hModule)
		xbThrowEsysLowFmt("xbXdcLib(%s)::dllStart: already initialized !!",m_pName);

	{
		xbSectLockSimple lck(&s_share);
		m_pPrev = NULL;
		m_pNext = s_pFirst;
		s_pFirst = this;
		if (m_pNext) 
			m_pNext->m_pPrev = this;
		m_hModule = hModule;
	}

	s_cChg++;

	xbRdbgOutput(xbString(m_pName)+" V"+xbString(m_pVer)+" Initialized!\n");
}

void xbXdcLib::dllStop(HMODULE hModule)
{
	if (!m_pPrev && !m_pNext && !m_hModule)
		xbThrowEsysLowFmt("xbXdcLib(%s)::dllStop: not initialized !!",m_pName);

	xbRdbgAssert(m_hModule == hModule);
	{
		xbSectLockSimple lck(&s_share);
		if (m_pPrev) 
			m_pPrev->m_pNext = m_pNext;
		else 
		{
			xbAssert(s_pFirst == this);
			s_pFirst = m_pNext;
		}
		if (m_pNext) 
			m_pNext->m_pPrev = m_pPrev;
		m_pPrev = NULL;
		m_pNext = NULL;
		m_hModule = NULL;
	}

	s_cChg++;
	xbRdbgOutput(xbString(m_pName)+" V"+xbString(m_pVer)+" Terminated!\n");
}

#endif

xbXdcLibEnum::xbXdcLibEnum()
{
	m_ix = -1;
#ifndef XDCBASE_EMBED
	m_pDll = NULL;
#endif
}

xbXdcLibEnum::~xbXdcLibEnum()
{
#ifndef XDCBASE_EMBED
	xbAssert(m_pDll == NULL);
#endif
}

HMODULE			xbXdcLibEnum::NextHlib(bool bUnlockLib)
{
#ifndef XDCBASE_EMBED
	next();
	HMODULE hMod = m_pDll?m_pDll->m_hModule:NULL;
	if (bUnlockLib) 
		UnlockLib();
	return hMod;
#else
	return NULL;
#endif
}

#ifndef XDCBASE_EMBED
xbXdcLib *xbXdcLibEnum::NextLib()
{
	next();
	return m_pDll;
}

xbXdcLib		*xbXdcLibEnum::Find(const xbString &name)
{
	for(;;)
	{
		next();
		xbXdcLib *pDll = m_pDll;
		if (!pDll) 
			break;
		if (pDll->GetName().CompareNoCase(name) == 0) 
			return pDll;
		if (pDll->GetCompleteName().CompareNoCase(name) == 0) 
			return pDll;
	}
	return NULL;
}
#endif

HMODULE			xbXdcLibEnum::FindHlib(const xbString &name,bool bUnlockLib)
{
#ifndef XDCBASE_EMBED
	xbXdcLib *pLib = Find(name);
	if (!pLib) 
		return NULL;
	HMODULE hMod = pLib->m_hModule;
	if (bUnlockLib) 
		UnlockLib();
	return hMod;
#else
	return NULL;
#endif
}

#ifndef XDCBASE_EMBED
xbXdcLib *xbXdcLibEnum::GetLib()
{
	LockLib();
	return m_pDll;
}
#endif

void			xbXdcLibEnum::Reset()
{
	UnlockLib();
	m_ix = -1;
#ifndef XDCBASE_EMBED
	xbRdbgAssert(m_pDll == NULL);
#endif
}

void			xbXdcLibEnum::UnlockLib()
{
#ifndef XDCBASE_EMBED
	m_lck.Unlock();
	m_pDll = NULL;
#endif
}

void			xbXdcLibEnum::LockLib()
{
#ifndef XDCBASE_EMBED
	if (m_lck.IsLocked())
		return;

	m_lck.Lock(&xbXdcLib::s_share);
	int ix = 0;
	for (xbXdcLib* pDLL = xbXdcLib::s_pFirst; pDLL != NULL;
		pDLL = pDLL->m_pNext, ix++)
	{
		if (ix == m_ix)
		{
			m_pDll = pDLL;
			return;
		}
	}
	m_pDll = NULL;
	m_lck.Unlock();
#endif
}

void			xbXdcLibEnum::next()
{
	m_ix++;
#ifndef XDCBASE_EMBED
	if (m_pDll) 
	{
		xbRdbgAssert(m_lck.IsLocked());
		m_pDll = m_pDll->m_pNext;
		if (!m_pDll) 
			m_lck.Unlock();
		return;
	}
	LockLib();
#endif
}

