From fb28f6800864176be2ffca29e8e664b641f33170 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 21 Dec 2009 18:04:02 +0000 Subject: m3 is copied into trunk, we should be good to go, now. --- src/functionmatches.cpp | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/functionmatches.cpp (limited to 'src/functionmatches.cpp') diff --git a/src/functionmatches.cpp b/src/functionmatches.cpp new file mode 100644 index 0000000..4e4b7ff --- /dev/null +++ b/src/functionmatches.cpp @@ -0,0 +1,141 @@ +#include "functionmatches.h" + +#include + +FunctionMatches::FunctionMatches() +{ +} + +FunctionMatches::~FunctionMatches() +{ +} + +Bu::FString FunctionMatches::getName() const +{ + return "matches"; +} + +bool FunctionMatches::globcmp( const Bu::FString &sTxt, const Bu::FString &sMatches ) +{ + Bu::FString::const_iterator t, g; + t = sTxt.begin(); + g = sMatches.begin(); + + while( g && t ) + { + switch( *g ) + { + case '*': + // First, if the * is at the end, then we do match, it doesn't + // matter what is in sTxt + if( !(g+1) ) + return true; + // Now attempt to scan for the remainder as a matched set + { + Bu::FString::const_iterator tn = t+1, gn = g+1, gi=g+1; + bool bFoundMatch = false; + while( tn && gn ) + { + if( *gn == '*' ) + { + g = gn; + t = tn; + break; + } + if( *tn == *gn ) + { + g = gn; + t = tn; + tn++; + gn++; + bFoundMatch = true; + } + else + { + gn = gi; + tn++; + bFoundMatch = false; + } + } + if( bFoundMatch == false ) + return false; + if( !tn && !gn && bFoundMatch ) + return true; + } + break; + + case '?': + // Don't bother checking. + t++; + g++; + break; + + default: + if( *t != *g ) + return false; + t++; + g++; + break; + } + } + if( t || (g && *g != '*') ) + return false; + return true; +} + +bool FunctionMatches::matchlist( const Bu::FString &sTxt, VarList &lParams ) +{ + for( VarList::iterator i = lParams.begin(); i; i++ ) + { + if( (*i).getType() == Variable::typeList ) + { + for( VarList::iterator j = (*i).begin(); j; j++ ) + { + if( globcmp( sTxt, (*j).toString() ) ) + return true; + } + } + else + { + if( globcmp( sTxt, (*i).toString() ) ) + return true; + } + } + return false; +} + +Variable FunctionMatches::call( Variable &input, VarList lParams ) +{ + switch( input.getType() ) + { + case Variable::typeString: + { + Bu::FString sTxt = input.getString(); + return Variable( matchlist( sTxt, lParams ) ); + } + break; + + case Variable::typeList: + { + Variable vRet( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( (*i).getType() != Variable::typeString ) + continue; + Bu::FString sTxt = (*i).getString(); + if( matchlist( sTxt, lParams ) ) + vRet.append( *i ); + } + return vRet; + } + break; + + default: + throw Bu::ExceptionBase("You can only use a string or list as the " + "input to matches."); + break; + } + + return Variable(); +} + -- cgit v1.2.3