/*=========================================================================
*
* CPAFileO.cpp		- CPA_FileObject : implementation
*
*
* Version			1.0
* Creation date		20.06.97
* Revision date
*
* Shaitan
*=======================================================================*/
#include "stdafx.h"

#ifdef ACTIVE_EDITOR

#include "acp_base.h"

#include "itf/CPAFileO.hpp"
#include "itf/CPAEditB.hpp"
#include "itf/CPAInter.hpp"

//#################################################################################
// INITS
//#################################################################################

/*===========================================================================
	Constructor
=========================================================================*/
CPA_FileObject::CPA_FileObject (CPA_EditorBase *pEditor, const CString csFileName)
	: CPA_BaseObject (pEditor, C_szFileTypeName)
{
	tdeMissingCriteria	eResult;
	// list
	m_oDllKeyList.RemoveAll();

	// rename
	eResult = fn_eRename(csFileName);
	ASSERT(eResult == E_mc_None);
}

/*===========================================================================
	Destructor
=========================================================================*/
CPA_FileObject::~CPA_FileObject (void)
{

}


//#################################################################################
// ACCES
//#################################################################################


/*===========================================================================
	prefix string that completes the minimal name to the full name
=========================================================================*/
char *CPA_FileObject::GetCompletionPath (CPA_EditorBase *pEditor, const CString csKey)
{
	tdstDllKeyList	*p_stList;
	tdstKeyName		*p_stKey;
	CString			csName;

	// init name
	csName.Empty();
	
	// find Dll List
	p_stList = GetDllKeyList(pEditor);
	if (!p_stList)
		return "";

	// find Key
	p_stKey = GetKeyName(p_stList, csKey);
	if (!p_stKey)
		return "";

	return (char *)LPCTSTR(p_stKey->m_csCompletionName);
}

/*===========================================================================
	Minimal name
=========================================================================*/
CString CPA_FileObject::GetReferencedName (CPA_EditorBase *pEditor, const CString csKey)
{
	tdstDllKeyList	*p_stList;
	tdstKeyName		*p_stKey;
	CString			csName;

	// init name
	csName.Empty();
	
	// find Dll List
	p_stList = GetDllKeyList(pEditor);
	if (!p_stList)
		return csName;

	// find Key
	p_stKey = GetKeyName(p_stList, csKey);
	if (!p_stKey)
		return csName;

	// return corresponding name
	return (p_stKey->m_csName);
}

/*===========================================================================
	Complete name
=========================================================================*/
CString CPA_FileObject::GetCompleteName (void)
{
	CString csName, csPrefix;
	
	// get current prefix
	csPrefix = GetName().Left(4);
	
	// if necessary, remove prefix
	if (csPrefix == GetEditor()->GetInterface()->GetUserPrefix())
		csName = GetName().Mid(4);
	else
		csName = GetName();

	return csName;
}

/*===========================================================================
	Register reference
=========================================================================*/
//replace all '/' symbols by '\' symbols in the path string
/*static*/ void CPA_FileObject::sm_vUniformizeSlashes(CString &r_csPath)
{
	char *pszSlash = r_csPath.GetBuffer(0);
	while ( pszSlash = strchr(pszSlash, '/') )
		*pszSlash = '\\';
	r_csPath.ReleaseBuffer();
}

BOOL CPA_FileObject::fn_bSetReferencedName(CPA_EditorBase *pEditor, const CString csKey, const CString csName)
{
	tdstDllKeyList	*p_stList;
	tdstKeyName		*p_stKey;

	CString csReferencedName(csName);
	sm_vUniformizeSlashes(csReferencedName); //because arg is const

	//if the referenced name is not a substring of the full name, do not add the key...
	if ( GetCompleteName().Find(csName) == -1 )
		return FALSE;
	
	// check if DllKeyList already exists
	p_stList = GetDllKeyList(pEditor);
	if (!p_stList)
	{
		// create new list
		p_stList = new (tdstDllKeyList);
		// init parameters
		p_stList->m_pEditor = pEditor;
		p_stList->m_oKeyList.RemoveAll();
		// add it to the list
		m_oDllKeyList.AddTail(p_stList);
	}

	// check if Key already exists
	p_stKey = GetKeyName(p_stList, csKey);
	if (p_stKey)
		return (p_stKey->m_csName == csName);
	
	// create new Key
	p_stKey = new (tdstKeyName);
	// init parameters
	p_stKey->m_csKey = csKey;
	p_stKey->m_csName = csName;
	// completion path
	long lCompleteLength = GetCompleteName().GetLength();
	long lReferencedLength = p_stKey->m_csName.GetLength();
	p_stKey->m_csCompletionName = (lCompleteLength > lReferencedLength)
	? GetCompleteName().Left(lCompleteLength - lReferencedLength - 1)
	: "";

	// add it to the list
	p_stList->m_oKeyList.AddTail(p_stKey);
	return TRUE;
}

/*===========================================================================
	Find DLL Key List
=========================================================================*/
tdstDllKeyList * CPA_FileObject::GetDllKeyList (CPA_EditorBase *pEditor)
{
    tdstDllKeyList	*p_stList;
	POSITION		pos;
	
	pos = m_oDllKeyList.GetHeadPosition();
    while (pos)
    {
        p_stList = m_oDllKeyList.GetNext(pos);
        if (p_stList->m_pEditor == pEditor)
            return p_stList;
    }
    return NULL;
}

/*===========================================================================
	Find Key in DLL List
=========================================================================*/
tdstKeyName * CPA_FileObject::GetKeyName (tdstDllKeyList *p_stList, const CString csKey)
{
    tdstKeyName	*p_stKey;
	POSITION	pos;
	
	pos = p_stList->m_oKeyList.GetHeadPosition();
    while (pos)
    {
        p_stKey = p_stList->m_oKeyList.GetNext(pos);
        if (p_stKey->m_csKey == csKey)
            return p_stKey;
    }
    return NULL;
}

#endif // ACTIVE_EDITOR
