From ea17d3262924f95a4fa721b3de6d7fe499bdcf14 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 5 Apr 2012 22:52:54 +0000 Subject: Added unique function and a workaround for non-matching inputs to rules passing the rule input filter. --- src/functionplugger.cpp | 2 ++ src/functionunique.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/functionunique.h | 16 ++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/functionunique.cpp create mode 100644 src/functionunique.h (limited to 'src') diff --git a/src/functionplugger.cpp b/src/functionplugger.cpp index 8bd2edd..1f2bcf5 100644 --- a/src/functionplugger.cpp +++ b/src/functionplugger.cpp @@ -21,6 +21,7 @@ extern Bu::PluginInfo pluginFunctionOpen; extern Bu::PluginInfo pluginFunctionClose; extern Bu::PluginInfo pluginFunctionRead; extern Bu::PluginInfo pluginFunctionWrite; +extern Bu::PluginInfo pluginFunctionUnique; FunctionPlugger::FunctionPlugger() { @@ -42,6 +43,7 @@ FunctionPlugger::FunctionPlugger() registerBuiltinPlugin( &pluginFunctionClose ); registerBuiltinPlugin( &pluginFunctionRead ); registerBuiltinPlugin( &pluginFunctionWrite ); + registerBuiltinPlugin( &pluginFunctionUnique ); DIR *dir = opendir("/usr/lib/build"); if( !dir ) diff --git a/src/functionunique.cpp b/src/functionunique.cpp new file mode 100644 index 0000000..ca2461f --- /dev/null +++ b/src/functionunique.cpp @@ -0,0 +1,47 @@ +#include "functionunique.h" + +#include +#include +PluginInterface3( pluginFunctionUnique, unique, FunctionUnique, Function, + "Mike Buland", 0, 1 ); + +FunctionUnique::FunctionUnique() +{ +} + +FunctionUnique::~FunctionUnique() +{ +} + +Bu::String FunctionUnique::getName() const +{ + return "unique"; +} + +Variable FunctionUnique::call( Variable &input, VarList lParams ) +{ + if( lParams.getSize() > 0 ) + throw Bu::ExceptionBase("Unique does not take any parameters."); + if( input.getType() != Variable::typeList ) + throw Bu::ExceptionBase("unique does not work on non-list types."); + + Bu::Hash hHas; + + Variable vOut( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( (*i).getType() != Variable::typeString ) + continue; + + Bu::String s = (*i).getString(); + + if( hHas.has( s ) ) + continue; + + hHas.insert( s, true ); + vOut.append( *i ); + } + + return vOut; +} + diff --git a/src/functionunique.h b/src/functionunique.h new file mode 100644 index 0000000..6bc4225 --- /dev/null +++ b/src/functionunique.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_UNIQUE_H +#define FUNCTION_UNIQUE_H + +#include "function.h" + +class FunctionUnique : public Function +{ +public: + FunctionUnique(); + virtual ~FunctionUnique(); + + virtual Bu::String getName() const; + virtual Variable call( Variable &input, VarList lParams ); +}; + +#endif -- cgit v1.2.3