Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Help needed to Traverse a file from the cdt project

> I need to show the focus on a text file inside a C/C++ project. I have
> created a project structure something like this, so i wanted to set focus on
> def222.txt PROGRAMATICALLY(i know this file name and i am sure this file is
> exist).
> 
> to do this i need to traverse and focus on that text file(def222.txt). Can
> any body give a code snippet to traverse a file inside C/C++ project.
> 
> Example1
>      + includes
>      - src
>         + form.h
>         + form.c
>            Makefile.am
>            abc111.txt
>            def222.txt
>            ght333.txt
>       + imaget
>         autogen.sh
>         Makefile.sh

Look for references to IResourceProxyVisitor, e.g. from cdt.ui ToggleSourceAndHeaderAction,
with IContainer being the project:

	/**
	 * Find a file in the given resource container for the given basename.
	 * 
	 * @param container
	 * @param basename
	 * @return a matching {@link IFile} or <code>null</code>, if no matching file was found
	 */
	private IFile findInContainer(IContainer container, final String basename) {
		final IFile[] result= { null };
		IResourceProxyVisitor visitor= new IResourceProxyVisitor() {
			public boolean visit(IResourceProxy proxy) throws CoreException {
				if (result[0] != null) {
					return false;
				}
				if (!proxy.isAccessible()) {
					return false;
				}
				if (proxy.getType() == IResource.FILE && proxy.getName().equals(basename)) {
					result[0]= (IFile)proxy.requestResource();
					return false;
				}
				return true;
			}};
		try {
			container.accept(visitor, 0);
		} catch (CoreException exc) {
			// ignore
		}
		return result[0];
	}


Back to the top