Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Fast regular expressions

, 29 Oct 2000
Compiles a regular expression into a fast automaton.
// RexFileProcessor.cpp: implementation of the CRexFileProcessor class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RexSearch.h"
#include "RexFileProcessor.h"
#include "RexAlgorithm.h"
#include "RexInterface.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRexFileProcessor::CRexFileProcessor()
{
}

CRexFileProcessor::~CRexFileProcessor()
{

}
REXI_DefErr   CRexFileProcessor::AddRegDefinition(const char* pcszName,const char* pcszRegExp)
{
    return m_rexSearch.AddRegDef(pcszName,pcszRegExp);
}
REXI_DefErr   CRexFileProcessor::SetRegExpression(const char* pcszExpr)
{
    return m_rexSearch.SetRegexp(pcszExpr);
}



void                CRexFileProcessor::ProcessFile(CSearchResult& rResult,
        const char* pcszPathName,const char* pcszFileName,
        bool bLinewise,bool bAtMostOneMatchPerLine)
{
    CFile oFile;
    if( oFile.Open(pcszPathName,CFile::modeRead) ){
        CString strFile;
        LPSTR pszBuf= strFile.GetBufferSetLength(oFile.GetLength()+3);
        pszBuf[0]= '\n';
        oFile.Read(pszBuf+1,oFile.GetLength());
        pszBuf[oFile.GetLength()]='\n';
        pszBuf[oFile.GetLength()+1]='\0';
        bool bEos;
        REXA_BegEnd matchRange;
        LPCSTR pszFindBuf= pszBuf;
        int nLenMatch;
        while( m_rexSearch.Find(pszFindBuf,nLenMatch,bEos) ){
            const char* psz= pszFindBuf-nLenMatch;
            const char* pLineBeg;
            for(pLineBeg= psz;*pLineBeg!='\n';pLineBeg--);
            const char* pLineEnd;
            nLenMatch= max(nLenMatch,1);
            for(pLineEnd= psz+nLenMatch;*pLineEnd && *pLineEnd!='\n' && *pLineEnd!='\r';pLineEnd++);
            CString strLine= CString(pLineBeg+1,pLineEnd-pLineBeg-1);
            strLine.Replace("\t","    ");
            SSearchInfo sSearchInfo(pcszPathName,pcszFileName,
                                pszFindBuf-pszBuf-nLenMatch-1,nLenMatch,strLine);
            pszFindBuf= pLineEnd;
            rResult.vecSearchInfo.push_back(sSearchInfo);
            if(bEos){
                break;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Martin Holzherr

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 30 Oct 2000
Article Copyright 2000 by Martin Holzherr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid