Click here to Skip to main content
12,296,188 members (64,460 online)
Click here to Skip to main content

Stats

299.3K views
4.7K downloads
103 bookmarked
Posted

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

You may also be interested in...

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