Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.5M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
#include "MTSearchFile.h"
#include <windows.h>

void MTSearchFile::search(const std::vector<MTSTRING> &directories, const std::vector<MTSTRING> &searchPatterns, std::vector<MTSTRING> &results)
{      
    results.clear();
    
    for( unsigned int t=0; t < directories.size(); t++ )
    {
        // the path must end with a /
		MTSTRING dir = directories[t];

        if( dir[dir.size()-1] != '/' &&
            dir[dir.size()-1] != '\\' )
        {
            dir += _T("/");
        }

		search(dir.c_str(), searchPatterns, results);        
    }    
}

void MTSearchFile::search(const MTCHAR *directory, const std::vector<MTSTRING> &searchPatterns, std::vector<MTSTRING> &results)
{
    // file search
	for( unsigned int t=0; t<searchPatterns.size(); t++ )
	{	
		search( directory, searchPatterns[t].c_str(), results );		
	}    

	// now, we look for subfolders    
	WIN32_FIND_DATA findData;
    HANDLE hFind;
	MTSTRING curLookIn = directory;
	curLookIn += _T("*.*");
    
	hFind = FindFirstFile( curLookIn.c_str() , &findData);
    
    if( hFind != INVALID_HANDLE_VALUE )
    {
        do
        {        
			// if this is a sub-folder then search it
            if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                findData.cFileName[0] != '.' )                
            {
                MTSTRING curLookDir = directory;
				curLookDir += findData.cFileName;
				curLookDir += _T("/");

				search( curLookDir.c_str(), searchPatterns, results );                
            }

        }while( FindNextFile(hFind, &findData) );        
    }     
    
    FindClose(hFind);
}

void MTSearchFile::search(const MTCHAR *directory, const MTCHAR *searchPattern, std::vector<MTSTRING> &results)
{	
    WIN32_FIND_DATA findData;
	MTSTRING curLookIn = directory;
	curLookIn += searchPattern;
	
	HANDLE hFind = FindFirstFile(curLookIn.c_str(), &findData);
    
    if( hFind != INVALID_HANDLE_VALUE )
    {
        do
        { 	
            // skip directories and special files named "."
			if( !(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
				findData.cFileName[0] != '.' )                
            {
				MTSTRING found = directory;
				found += findData.cFileName;
                results.push_back( found );
            }
            
        }while( FindNextFile(hFind, &findData) );        
    }

    FindClose(hFind);
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions