From 4f1e5849d4a48eb674d81164ba4baba4ad51a89f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 3 Jun 2011 05:51:21 +0000 Subject: Added a range function. It works a lot like the range function in python, if called with one parameter, it will produce a list of the numbers 1 through the number provided inclusive, if two numbers, it will produce a list of all numbers between the first and second parameter, inclusive, and if three parameters are provided it will use the last one as a step. --- src/functionplugger.cpp | 2 ++ src/functionrange.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ src/functionrange.h | 17 ++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/functionrange.cpp create mode 100644 src/functionrange.h (limited to 'src') diff --git a/src/functionplugger.cpp b/src/functionplugger.cpp index 035e5b9..c7b270c 100644 --- a/src/functionplugger.cpp +++ b/src/functionplugger.cpp @@ -16,6 +16,7 @@ extern Bu::PluginInfo pluginFunctionTargets; extern Bu::PluginInfo pluginFunctionToString; extern Bu::PluginInfo pluginFunctionUnlink; extern Bu::PluginInfo pluginFunctionRegEx; +extern Bu::PluginInfo pluginFunctionRange; FunctionPlugger::FunctionPlugger() { @@ -32,6 +33,7 @@ FunctionPlugger::FunctionPlugger() registerBuiltinPlugin( &pluginFunctionToString ); registerBuiltinPlugin( &pluginFunctionUnlink ); registerBuiltinPlugin( &pluginFunctionRegEx ); + registerBuiltinPlugin( &pluginFunctionRange ); DIR *dir = opendir("/usr/lib/build"); if( !dir ) diff --git a/src/functionrange.cpp b/src/functionrange.cpp new file mode 100644 index 0000000..f45cf0e --- /dev/null +++ b/src/functionrange.cpp @@ -0,0 +1,71 @@ +#include "functionrange.h" + +#include +PluginInterface3( pluginFunctionRange, range, FunctionRange, Function, + "Mike Buland", 0, 1 ); + +FunctionRange::FunctionRange() +{ +} + +FunctionRange::~FunctionRange() +{ +} + +Bu::String FunctionRange::getName() const +{ + return "range"; +} + +Variable FunctionRange::call( Variable &input, VarList lParams ) +{ + Variable vRet( Variable::typeList ); + int iLow = 1; + int iHigh = 1; + int iStep = 1; + if( lParams.getSize() == 1 ) + { + iHigh = lParams.first().toInt(); + } + else if( lParams.getSize() == 2 ) + { + iLow = lParams.first().toInt(); + iHigh = lParams.last().toInt(); + } + else if( lParams.getSize() == 3 ) + { + VarList::iterator i = lParams.begin(); + iLow = (*i).toInt(); + i++; + iHigh = (*i).toInt(); + i++; + iStep = (*i).toInt(); + } + if( iStep == 0 ) + throw Bu::ExceptionBase("Step cannot be zero."); + + if( iHigh < iLow ) + { + if( iStep > 0 ) + throw Bu::ExceptionBase( + "If start is less than end then step must be negative."); + for( int j = iLow; j >= iHigh; j += iStep ) + { + vRet.append( Variable( j ) ); + } + } + else + { + if( iStep < 0 ) + throw Bu::ExceptionBase( + "If start is more than end then step must be positive."); + for( int j = iLow; j <= iHigh; j += iStep ) + { + vRet.append( Variable( j ) ); + } + } + + + return vRet; +} + diff --git a/src/functionrange.h b/src/functionrange.h new file mode 100644 index 0000000..1f6d8c5 --- /dev/null +++ b/src/functionrange.h @@ -0,0 +1,17 @@ +#ifndef FUNCTION_RANGE_H +#define FUNCTION_RANGE_H + +#include "function.h" + +class FunctionRange : public Function +{ +public: + FunctionRange(); + virtual ~FunctionRange(); + + virtual Bu::String getName() const; + virtual Variable call( Variable &input, VarList lParams ); + +}; + +#endif -- cgit v1.2.3