Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eaeaccad2a4688b230f2b79fe6bd6e2c99d4f922 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2012 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

/**
 * Visitor to find macros in a BTree.
 * @since 4.0.2
 */
public final class MacroContainerPatternCollector implements IBTreeVisitor {
	private final PDOMLinkage fLinkage;
	
	private final List<PDOMMacroContainer> macros = new ArrayList<PDOMMacroContainer>();
	private final Pattern fPattern;
	private final IProgressMonitor fMonitor;
	private int fMonitorCheckCounter= 0;

		
	public MacroContainerPatternCollector(PDOMLinkage linkage, Pattern pattern, IProgressMonitor monitor) {
		fLinkage= linkage;
		fPattern= pattern;
		fMonitor= monitor;
	}

	
	@Override
	final public int compare(long record) throws CoreException {
		if (fMonitor != null)
			checkCancelled();
		return 0;
	}
	
	@Override
	final public boolean visit(long record) throws CoreException {
		if (record == 0)
			return true;

		String name= PDOMNamedNode.getDBName(fLinkage.getDB(), record).getString();
		if (fPattern.matcher(name).matches()) {
			macros.add(new PDOMMacroContainer(fLinkage, record));
		}
		return true; // look for more
	}
	
	final public PDOMMacroContainer[] getMacroContainers() {
		return macros.toArray(new PDOMMacroContainer[macros.size()]);
	}
	
	private void checkCancelled() {
		if (++fMonitorCheckCounter % 0x1000 == 0 && fMonitor.isCanceled()) {
			throw new OperationCanceledException();
		}
	}
}

Back to the top