/*********************************************************************
Copyright (C) 2001 by
Alexander Berthold, alexander-berthold@web.de.
Hoegestr. 54
79108 Freiburg i. Breisgau
Germany
-- This file is part of cxTokenizer --
"cxTokenizer" is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or any later version.
"cxTokenizer" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with "cxTokenizer"; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
---------------------------------------------------------------
If you find any bugs or if you make other corrections/
enhancements, i'd appreciate if you'd let me know about
that. My email is
alexander-berthold@web.de
If you share this code, do not remove this text.
---------------------------------------------------------------
*********************************************************************/
// cxTokenizerMapData.cpp: implementation of the cxTokenizerMapData class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "cxTokenizerMapData.h"
#include "cxTokenizerTokenRule.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "cxTokenizerMapDataDiags.cpp"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cxTokenizerMapData::cxTokenizerMapData()
{
m_pRule =NULL;
m_fComputed =false;
}
cxTokenizerMapData::~cxTokenizerMapData()
{
iterator it;
for(it=begin();it!=end();it++)
delete it->second;
}
//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////
/*********************************************************************
FUNCTION: cxTokenizerMapData::plmdInsert
PURPOSE: Inserts for the character 'tcChar' the rule
'pRule' in the map of child nodes.
RETURNS: 'cxTokenizerMapData'; the cxTokenizerMapData
building the new 'leaf'
*********************************************************************/
cxTokenizerMapData *cxTokenizerMapData::ptmdInsert(
TCHAR tcChar,
cxTokenizerTokenRule* pRule)
{
iterator it = find(tcChar);
bool fInsert = ( (it==end()) || (tcChar=='\0') );
cxTokenizerMapData *ptmdData = NULL;
if(fInsert)
{
ptmdData =new cxTokenizerMapData;
it =insert(value_type(tcChar,ptmdData));
}
else
ptmdData =it->second;
if(pRule!=NULL && ptmdData->m_pRule!=NULL)
throw cxTokenizerException(ERR_INVALID_RULE);
else
{
if(pRule!=NULL)
ptmdData->m_pRule=pRule;
}
return ptmdData;
}
/*********************************************************************
FUNCTION: cxTokenizerMapData::plmdDoesApply
PURPOSE: Tests if there is a child node for the
character 'tcChar'.
RETURNS: 'cxTokenizerMapData' != NULL if found;
NULL if no path has been found.
*********************************************************************/
cxTokenizerMapData *cxTokenizerMapData::ptmdDoesApply(
const cxTokenizerContext* ptcContextInfo,
TCHAR tcChar,
bool *pfComplete,
int nStartPos, int nCurPos)
{
// Computed rule?
if(m_fComputed)
return m_pRule->fDoesApply(ptcContextInfo,tcChar,pfComplete,nStartPos,nCurPos)?this:NULL;
iterator it = find(tcChar);
cxTokenizerMapData *ptmdTemp = NULL;
TCHAR tcTemp;
// No character matched 'tcChar' -> is there a computed rule
// matching 'tcChar'?
if(it == end())
{
it =find(_T('\0'));
while(it!=end() && it->first==_T('\0'))
{
ASSERT(it->second->fIsLeaf());
if(it->second->pttrGetRule()->fDoesApply(ptcContextInfo,tcChar,pfComplete,nStartPos,nCurPos))
return it->second;
it++;
}
return NULL;
}
tcTemp =it->first;
ptmdTemp =it->second;
if(ptmdTemp->fIsLeaf())
{
if(ptmdTemp->pttrGetRule()->fDoesApply(ptcContextInfo,tcChar,pfComplete,nStartPos,nCurPos))
return ptmdTemp;
return NULL;
}
else
return ptmdTemp;
}
/*********************************************************************
FUNCTION: cxTokenizerMapData::vApplied
PURPOSE: Is being called if a path has been taken.
Called with the same parameters as plmdDoesApply.
RETURNS: - void -
*********************************************************************/
void cxTokenizerMapData::vApplied(
cxTokenizerContext* ptcContextInfo,
TCHAR tcChar, bool fComplete,
int nStartPos, int nEndPos)
{
if(m_pRule)
m_pRule->vApplied(ptcContextInfo,tcChar,fComplete,nStartPos,nEndPos);
}