/*********************************************************************
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, const std::tstring& strInitString)
: cxTokenizerTokenRule(nIDValue,ttrf,NULL,strInitString)
{
}
cxTokenizerNumberTokenRule::~cxTokenizerNumberTokenRule()
{
}
//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////
bool cxTokenizerNumberTokenRule::fDoesApply(
const cxTokenizerContext *ptcContext,
TCHAR tcChar, bool *pfComplete,
int nStartPos, int nCurPos) const
{
const cxTokenizerNumberContextCookie
*pCookie = NULL;
ptcContext->fGetConstCookie(this,&pCookie);
if(nCurPos==nStartPos)
{
if(ptcContext->fActiveRules())
return false;
if(isdigit(tcChar))
{
if(pCookie!=NULL)
{
// This is the case when:
// 1234
// ^ here not
// ^ here the first time
// ^ ...
// So it must be ignored
return false;
}
}
else
return false;
}
if(pCookie!=NULL && pCookie->fCompleted)
return false;
TCHAR tcNext = ptcContext->tcPeekNextCharacter();
if( !(isdigit(tcNext) ||
(tcNext=='x' && (nCurPos-nStartPos)==0 && tcChar=='0') ||
(tcNext=='.' && (pCookie==NULL || !pCookie->fDecimalPointHit))) ||
ptcContext->fIsLastCharacter())
{
(*pfComplete) = true;
return true;
}
return true;
}
void cxTokenizerNumberTokenRule::vApplied(
cxTokenizerContext *ptcContext,
TCHAR tcChar,
bool fCompleted,
int nStartPos, int nCurPos) const
{
cxTokenizerNumberContextCookie
*pCookie = NULL;
ptcContext->fGetCookie(this,&pCookie);
// First character?
if(nCurPos==nStartPos)
{
// We need exclusive access to the following characters:
ptcContext->vSetFlag(tctx_exclusive,true);
ASSERT(pCookie==NULL);
ptcContext->fSetCookie(this,&pCookie);
ASSERT(pCookie!=NULL);
}
// cookie must already exist.
ASSERT(pCookie!=NULL);
if(fCompleted)
{
ptcContext->vSetFlag(tctx_exclusive,false);
pCookie->fCompleted=true;
}
if(tcChar=='.')
pCookie->fDecimalPointHit=true;
pCookie->strResult+=tcChar;
}
bool cxTokenizerNumberTokenRule::fGetResultString(const cxTokenizerContext* ptcContext, std::tstring& strResult) const
{
const cxTokenizerNumberContextCookie
*pCookie = NULL;
ptcContext->fGetConstCookie(this,&pCookie);
ASSERT(pCookie!=NULL);
if(pCookie->strResult.size()>2 &&
pCookie->strResult[1]=='x')
{
TCHAR szTemp[32]; szTemp[31]='\0';
unsigned long ulTemp=0;
_stscanf(pCookie->strResult.c_str()+2,_T("%lx"),&ulTemp);
_sntprintf(szTemp,31,"%lu",ulTemp);
strResult = szTemp;
return true;
}
strResult =pCookie->strResult;
return true;
}