/*********************************************************************
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.
---------------------------------------------------------------
*********************************************************************/
// cxTokenizerNumberTokenRule.cpp: implementation of the cxTokenizerNumberTokenRule class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "cxTokenizerTokenRule.h"
#include "cxTokenizerContextCookie.h"
#include "cxTokenizerContext.h"
#include "cxTokenizerNumberTokenRule.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cxTokenizerNumberTokenRule::cxTokenizerNumberTokenRule(int nIDValue, xttr_flags ttrf)
: cxTokenizerTokenRule(nIDValue,ttrf,NULL)
{
}
cxTokenizerNumberTokenRule::~cxTokenizerNumberTokenRule()
{
}
//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////
bool cxTokenizerNumberTokenRule::fDoesApply(
const cxTokenizerContext *ptcContext,
TCHAR tcChar, bool *pfComplete,
int nStartPos, int nCurPos) const
{
const cxTokenizerNumberContextCookie
*pCookie;
pCookie =reinterpret_cast<const cxTokenizerNumberContextCookie*>
(ptcContext->ptccGetConstCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));
if(nCurPos==nStartPos)
{
if(pCookie!=NULL)
{
// This is the case when:
// 1234
// ^ here not
// ^ here the first time
// ^ ...
// So it must be ignored
return false;
}
if(isdigit(tcChar))
{
if(pfComplete)
(*pfComplete)=true;
return true;
}
else
return false;
}
ASSERT(pCookie!=NULL);
if(pCookie->fCompleted)
return false;
if(!isdigit(tcChar))
{
if(tcChar=='.')
{
if(pCookie->fDecimalPointHit)
return false;
else
return true;
}
return false;
}
return true;
}
void cxTokenizerNumberTokenRule::vApplied(
cxTokenizerContext *ptcContext,
TCHAR tcChar,
bool /* fCompleted */,
int nStartPos, int nCurPos) const
{
cxTokenizerNumberContextCookie
*pCookie;
pCookie =reinterpret_cast<cxTokenizerNumberContextCookie*>
(ptcContext->ptccGetCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));
// First character?
if(nCurPos==nStartPos)
{
ASSERT(pCookie==NULL);
pCookie =new cxTokenizerNumberContextCookie();
ptcContext->fSetCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this),reinterpret_cast<cxTokenizerContextCookie*>(pCookie));
pCookie->strResult+=tcChar;
return;
}
// No, cookie must already exist.
ASSERT(pCookie!=NULL);
if(pCookie->fCompleted)
return;
if(!isdigit(tcChar))
{
if(tcChar=='.')
{
ASSERT(pCookie->fDecimalPointHit==false);
pCookie->fDecimalPointHit=true;
}
}
pCookie->strResult+=tcChar;
}
bool cxTokenizerNumberTokenRule::fGetResultString(const cxTokenizerContext* ptcContext, std::tstring& strResult) const
{
const cxTokenizerNumberContextCookie
*pCookie;
pCookie =reinterpret_cast<const cxTokenizerNumberContextCookie*>
(ptcContext->ptccGetConstCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));
ASSERT(pCookie!=NULL);
strResult =pCookie->strResult;
return true;
}