diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-06-03 05:51:21 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-06-03 05:51:21 +0000 |
commit | 4f1e5849d4a48eb674d81164ba4baba4ad51a89f (patch) | |
tree | ee590deb9b6d99a73c24ea36877039d623d9cfd3 /src | |
parent | e8a57991458cfaa536fcc0d8b9a72739a8859da5 (diff) | |
download | build-4f1e5849d4a48eb674d81164ba4baba4ad51a89f.tar.gz build-4f1e5849d4a48eb674d81164ba4baba4ad51a89f.tar.bz2 build-4f1e5849d4a48eb674d81164ba4baba4ad51a89f.tar.xz build-4f1e5849d4a48eb674d81164ba4baba4ad51a89f.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/functionplugger.cpp | 2 | ||||
-rw-r--r-- | src/functionrange.cpp | 71 | ||||
-rw-r--r-- | src/functionrange.h | 17 |
3 files changed, 90 insertions, 0 deletions
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; | |||
16 | extern Bu::PluginInfo pluginFunctionToString; | 16 | extern Bu::PluginInfo pluginFunctionToString; |
17 | extern Bu::PluginInfo pluginFunctionUnlink; | 17 | extern Bu::PluginInfo pluginFunctionUnlink; |
18 | extern Bu::PluginInfo pluginFunctionRegEx; | 18 | extern Bu::PluginInfo pluginFunctionRegEx; |
19 | extern Bu::PluginInfo pluginFunctionRange; | ||
19 | 20 | ||
20 | FunctionPlugger::FunctionPlugger() | 21 | FunctionPlugger::FunctionPlugger() |
21 | { | 22 | { |
@@ -32,6 +33,7 @@ FunctionPlugger::FunctionPlugger() | |||
32 | registerBuiltinPlugin( &pluginFunctionToString ); | 33 | registerBuiltinPlugin( &pluginFunctionToString ); |
33 | registerBuiltinPlugin( &pluginFunctionUnlink ); | 34 | registerBuiltinPlugin( &pluginFunctionUnlink ); |
34 | registerBuiltinPlugin( &pluginFunctionRegEx ); | 35 | registerBuiltinPlugin( &pluginFunctionRegEx ); |
36 | registerBuiltinPlugin( &pluginFunctionRange ); | ||
35 | 37 | ||
36 | DIR *dir = opendir("/usr/lib/build"); | 38 | DIR *dir = opendir("/usr/lib/build"); |
37 | if( !dir ) | 39 | 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 @@ | |||
1 | #include "functionrange.h" | ||
2 | |||
3 | #include <bu/plugger.h> | ||
4 | PluginInterface3( pluginFunctionRange, range, FunctionRange, Function, | ||
5 | "Mike Buland", 0, 1 ); | ||
6 | |||
7 | FunctionRange::FunctionRange() | ||
8 | { | ||
9 | } | ||
10 | |||
11 | FunctionRange::~FunctionRange() | ||
12 | { | ||
13 | } | ||
14 | |||
15 | Bu::String FunctionRange::getName() const | ||
16 | { | ||
17 | return "range"; | ||
18 | } | ||
19 | |||
20 | Variable FunctionRange::call( Variable &input, VarList lParams ) | ||
21 | { | ||
22 | Variable vRet( Variable::typeList ); | ||
23 | int iLow = 1; | ||
24 | int iHigh = 1; | ||
25 | int iStep = 1; | ||
26 | if( lParams.getSize() == 1 ) | ||
27 | { | ||
28 | iHigh = lParams.first().toInt(); | ||
29 | } | ||
30 | else if( lParams.getSize() == 2 ) | ||
31 | { | ||
32 | iLow = lParams.first().toInt(); | ||
33 | iHigh = lParams.last().toInt(); | ||
34 | } | ||
35 | else if( lParams.getSize() == 3 ) | ||
36 | { | ||
37 | VarList::iterator i = lParams.begin(); | ||
38 | iLow = (*i).toInt(); | ||
39 | i++; | ||
40 | iHigh = (*i).toInt(); | ||
41 | i++; | ||
42 | iStep = (*i).toInt(); | ||
43 | } | ||
44 | if( iStep == 0 ) | ||
45 | throw Bu::ExceptionBase("Step cannot be zero."); | ||
46 | |||
47 | if( iHigh < iLow ) | ||
48 | { | ||
49 | if( iStep > 0 ) | ||
50 | throw Bu::ExceptionBase( | ||
51 | "If start is less than end then step must be negative."); | ||
52 | for( int j = iLow; j >= iHigh; j += iStep ) | ||
53 | { | ||
54 | vRet.append( Variable( j ) ); | ||
55 | } | ||
56 | } | ||
57 | else | ||
58 | { | ||
59 | if( iStep < 0 ) | ||
60 | throw Bu::ExceptionBase( | ||
61 | "If start is more than end then step must be positive."); | ||
62 | for( int j = iLow; j <= iHigh; j += iStep ) | ||
63 | { | ||
64 | vRet.append( Variable( j ) ); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | |||
69 | return vRet; | ||
70 | } | ||
71 | |||
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 @@ | |||
1 | #ifndef FUNCTION_RANGE_H | ||
2 | #define FUNCTION_RANGE_H | ||
3 | |||
4 | #include "function.h" | ||
5 | |||
6 | class FunctionRange : public Function | ||
7 | { | ||
8 | public: | ||
9 | FunctionRange(); | ||
10 | virtual ~FunctionRange(); | ||
11 | |||
12 | virtual Bu::String getName() const; | ||
13 | virtual Variable call( Variable &input, VarList lParams ); | ||
14 | |||
15 | }; | ||
16 | |||
17 | #endif | ||