summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/HtRegexReplaceList.cc
blob: 2ba7a4d25141bfe5c24a558819c25f1673ac2e48 (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
//
// HtRegexReplaceList.cc
//
// HtRegexReplaceList: Perform RegexReplace on a list of from/to pairs.
// 		       Patterns are applied in order; pattern matching 
// 		       doesn't stop when a match occurs.
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 2000-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtRegexReplaceList.cc,v 1.5 2004/05/28 13:15:21 lha Exp $
//
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "HtRegexReplaceList.h"

#ifdef HAVE_STD
#include <iostream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#endif /* HAVE_STD */

HtRegexReplaceList::HtRegexReplaceList(StringList &list, int case_sensitive )
{
	if (list.Count() & 1)
	{
		lastErrorMessage = "HtRegexReplaceList needs an even number of strings";
		return;
	}

	int i;
	String err;

	for (i = 0; i < list.Count(); i += 2)
	{
		String from = list[i];
		String to	= list[i+1];
		HtRegexReplace *replacer = new HtRegexReplace(from.get(), to.get(), case_sensitive);
		replacers.Add(replacer);		// Stash it even if there's an error so it will get destroyed later
		const String &err = replacer->lastError();
		if (err.length() != 0)
		{
			lastErrorMessage = err;
			return;
		}
	}
}

HtRegexReplaceList::~HtRegexReplaceList()
{
	// replacers gets chucked away
}

int HtRegexReplaceList::replace(String &str, int nullpattern , int nullstr )
{
	int repCount = replacers.Count();
	int doneCount = 0;

	for (int rep = 0; rep < repCount; rep++)
	{
		HtRegexReplace *replacer = (HtRegexReplace *) replacers[rep];
		if (replacer->replace(str, nullpattern, nullstr) > 0)
			doneCount++;
	}

	return doneCount;
}

const String &HtRegexReplaceList::lastError()
{
	return lastErrorMessage;
}

// End of HtRegexReplaceList.cc