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