aboutsummaryrefslogtreecommitdiff
path: root/src/functionregexp.cpp
blob: 2dc18b2a5be71a79f6e4f490f7bcf15452b737ba (plain)
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
#include "functionregexp.h"
#include "bu/plugger.h"
#include "regexp.h"

PluginInterface2(regexp, FunctionRegexp, Function, "Mike Buland", 0, 1 );

FunctionRegexp::FunctionRegexp()
{
}

FunctionRegexp::~FunctionRegexp()
{
}

void FunctionRegexp::execute( Build *bld, const StringList &lInput, StringList &lOutput )
{
	if( lParams.size() == 1 )
	{
		RegExp re( lParams.front().c_str() );

		for( StringList::const_iterator i = lInput.begin();
			 i != lInput.end(); i++ )
		{
			if( re.execute( (*i).c_str() ) )
			{
				lOutput.push_back( *i );
				if( bld )
				{
					int jmax = re.getNumSubStrings();
					for( int j = 0; j < jmax; j++ )
					{
						char buf[30];
						sprintf( buf, "re:%d", j );
						bld->set( *i, buf, re.getSubString( j ) );
					}
				}
			}
		}
	}
	else
	{
		if( !bld )
		{
			throw BuildException("You apparently can't use regexp with two params here.  Isn't that odd?");
		}

		RegExp re( lParams.front().c_str() );
		lParams.pop_front();
		std::string p2 = lParams.front();

		for( StringList::const_iterator i = lInput.begin();
			 i != lInput.end(); i++ )
		{
			if( re.execute( (*i).c_str() ) )
			{
				VarMap ext;
				int jmax = re.getNumSubStrings();
				for( int j = 0; j < jmax; j++ )
				{
					char buf[30];
					sprintf( buf, "re:%d", j );
					ext[buf] = re.getSubString( j );
				}

				std::string sNew = bld->replVars( p2, NULL, &ext );
				lOutput.push_back( sNew );

				for( int j = 0; j < jmax; j++ )
				{
					char buf[30];
					sprintf( buf, "re:%d", j );
					bld->set( sNew, buf, re.getSubString( j ) );
				}
				bld->copyContext( *i, sNew );
			}
		}
	}
}

Function *FunctionRegexp::duplicate( Build &bld, const StringList *cont, VarMap *mExtra )
{
	Function *pRet = new FunctionRegexp();
	pRet->copyData( this, bld, cont, mExtra );
	return pRet;
}