aboutsummaryrefslogtreecommitdiff
path: root/src/functionmatches.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/functionmatches.cpp141
1 files changed, 141 insertions, 0 deletions
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 @@
1#include "functionmatches.h"
2
3#include <unistd.h>
4
5FunctionMatches::FunctionMatches()
6{
7}
8
9FunctionMatches::~FunctionMatches()
10{
11}
12
13Bu::FString FunctionMatches::getName() const
14{
15 return "matches";
16}
17
18bool FunctionMatches::globcmp( const Bu::FString &sTxt, const Bu::FString &sMatches )
19{
20 Bu::FString::const_iterator t, g;
21 t = sTxt.begin();
22 g = sMatches.begin();
23
24 while( g && t )
25 {
26 switch( *g )
27 {
28 case '*':
29 // First, if the * is at the end, then we do match, it doesn't
30 // matter what is in sTxt
31 if( !(g+1) )
32 return true;
33 // Now attempt to scan for the remainder as a matched set
34 {
35 Bu::FString::const_iterator tn = t+1, gn = g+1, gi=g+1;
36 bool bFoundMatch = false;
37 while( tn && gn )
38 {
39 if( *gn == '*' )
40 {
41 g = gn;
42 t = tn;
43 break;
44 }
45 if( *tn == *gn )
46 {
47 g = gn;
48 t = tn;
49 tn++;
50 gn++;
51 bFoundMatch = true;
52 }
53 else
54 {
55 gn = gi;
56 tn++;
57 bFoundMatch = false;
58 }
59 }
60 if( bFoundMatch == false )
61 return false;
62 if( !tn && !gn && bFoundMatch )
63 return true;
64 }
65 break;
66
67 case '?':
68 // Don't bother checking.
69 t++;
70 g++;
71 break;
72
73 default:
74 if( *t != *g )
75 return false;
76 t++;
77 g++;
78 break;
79 }
80 }
81 if( t || (g && *g != '*') )
82 return false;
83 return true;
84}
85
86bool FunctionMatches::matchlist( const Bu::FString &sTxt, VarList &lParams )
87{
88 for( VarList::iterator i = lParams.begin(); i; i++ )
89 {
90 if( (*i).getType() == Variable::typeList )
91 {
92 for( VarList::iterator j = (*i).begin(); j; j++ )
93 {
94 if( globcmp( sTxt, (*j).toString() ) )
95 return true;
96 }
97 }
98 else
99 {
100 if( globcmp( sTxt, (*i).toString() ) )
101 return true;
102 }
103 }
104 return false;
105}
106
107Variable FunctionMatches::call( Variable &input, VarList lParams )
108{
109 switch( input.getType() )
110 {
111 case Variable::typeString:
112 {
113 Bu::FString sTxt = input.getString();
114 return Variable( matchlist( sTxt, lParams ) );
115 }
116 break;
117
118 case Variable::typeList:
119 {
120 Variable vRet( Variable::typeList );
121 for( VarList::iterator i = input.begin(); i; i++ )
122 {
123 if( (*i).getType() != Variable::typeString )
124 continue;
125 Bu::FString sTxt = (*i).getString();
126 if( matchlist( sTxt, lParams ) )
127 vRet.append( *i );
128 }
129 return vRet;
130 }
131 break;
132
133 default:
134 throw Bu::ExceptionBase("You can only use a string or list as the "
135 "input to matches.");
136 break;
137 }
138
139 return Variable();
140}
141