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