Click here to Skip to main content
15,881,044 members
Articles / Programming Languages / C++

Fast regular expressions

Rate me:
Please Sign up or sign in to vote.
4.85/5 (19 votes)
29 Oct 2000 360.6K   5.2K   104  
Compiles a regular expression into a fast automaton.
// FileFindRecursive.h: Schnittstelle f�r die Klasse CFileFindRecursive.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FILEFINDRECURSIVE_H__C340CE26_9439_11D4_86C5_1E1BBD000000__INCLUDED_)
#define AFX_FILEFINDRECURSIVE_H__C340CE26_9439_11D4_86C5_1E1BBD000000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>
using namespace std;

class CFileFindRecursive{
public:
      CFileFindRecursive(
          CString strStartDirectory,
          CString strWildCards,
          bool (*pProcessFile)(const char* pcszPathName,const char* pcszFileName,void* pData),
          void* pData)
          :m_pFuncProcessFile(pProcessFile),m_pData(pData),m_bCanceled(false)
      {
          int nPosPrev,nPos;
		  if( strWildCards.GetLength()>1 && strWildCards.Right(1)!=_T(";") ){
			  strWildCards+= _T(";");
		  }
          for(  nPos=0,nPosPrev=0; 
                (nPos= strWildCards.Find(';',nPos))!=-1;
                nPosPrev=nPos+1,nPos=nPosPrev){
              m_strWildCard= strWildCards.Mid(nPosPrev,nPos - nPosPrev);
              FindInThisDirectory(strStartDirectory);
          }
          
      }
      
private:
    void FindInThisDirectory(CString strDir)
    {
        CFileFind findFile;
        if( findFile.FindFile(strDir+"\\"+m_strWildCard) ){
            if( m_bCanceled )
                        return;
            while(findFile.FindNextFile()){
                if( !findFile.IsDirectory() ){
                    m_bCanceled= !m_pFuncProcessFile(findFile.GetFilePath(),
                        findFile.GetFileName(),m_pData);
                    if( m_bCanceled )
                        return;
                }
            }
        }
        Recurse(strDir);
    }
    void Recurse(CString str)
    {   
        CFileFind finder;
        CString strWildcard(str);
        strWildcard += _T("\\*.*");   // start working for files
        BOOL bWorking = finder.FindFile(strWildcard);   
        while (bWorking){
            if( m_bCanceled )
                        return;
            bWorking = finder.FindNextFile();
            if (finder.IsDots())         continue;
            if (finder.IsDirectory()){
                FindInThisDirectory(finder.GetFilePath());
            }
        }
    }

    CString             m_strWildCard;
    bool                m_bCanceled;
    bool (*m_pFuncProcessFile)(const char* pcszPathName,const char* pcszFileName,void* pData);
    void* m_pData;
};

#endif // !defined(AFX_FILEFINDRECURSIVE_H__C340CE26_9439_11D4_86C5_1E1BBD000000__INCLUDED_)

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


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions