project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/api/cpp/epoc/engine/preprocessor/BasicIncludeFileLocator.java
author Ed Swartz <ed.swartz@nokia.com>
Wed, 02 Dec 2009 15:02:05 -0600
changeset 610 bfb3ab3f70f2
parent 0 fb279309251b
child 684 8e7900690341
permissions -rw-r--r--
Initial round of Unix portability fixes (using HostOS class), and fixes to EPOC engine unit tests.

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
* This implementation searches the directory of the current source file
* and a list of provided paths using these rules:
* <p>
* <li>Every file is checked in the directory of the current #include, if known.
* <li>A user include "..." is searched on user paths then system paths.
* <li>A system include &lt;...&gt; is searched on system paths only.
*
*/
package com.nokia.carbide.internal.api.cpp.epoc.engine.preprocessor;

import java.io.File;
import java.io.IOException;

import com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator;
import com.nokia.cpp.internal.api.utils.core.HostOS;
public class BasicIncludeFileLocator implements IIncludeFileLocator {

	private static final File[] NO_DIRS = new File[0];
	
	private File[] userPaths;
	private File[] systemPaths;

	public BasicIncludeFileLocator(File[] userPaths, File[] systemPaths) {
		setPaths(userPaths, systemPaths);
	}

	public void setPaths(File[] userPaths, File[] systemPaths) {
		this.userPaths = userPaths != null ? userPaths : NO_DIRS;
		this.systemPaths = systemPaths != null ? systemPaths : NO_DIRS;
	}
	
	public File findIncludeFile(String file, boolean isUser, File currentDir) {
		if (HostOS.IS_UNIX) {
			file = HostOS.convertPathToUnix(file);
		}
		
		// see if the file exists as an absolute file
		File theFile = new File(file);
		if (theFile.exists() && theFile.isFile() && theFile.isAbsolute())
			return canonical(theFile);

		// if a user include and the current directory is known try to get the file relative to this dir
		if (isUser && currentDir != null) {
			theFile = new File(currentDir, file);
			if (theFile.exists() && theFile.isFile())
				return canonical(theFile);
		}

		if (isUser) {
			// search user directories first
			for (File dir : userPaths) {
				theFile = new File(dir, file);
				if (theFile.exists() && theFile.isFile())
					return canonical(theFile);
			}
		}
		
		// always search system directories
		for (File dir : systemPaths) {
			theFile = new File(dir, file);
			if (theFile.exists() && theFile.isFile()) {
				return canonical(theFile);
			}
		}
		
		// last case - a system include that is located in the same directory as the file including it
		if (!isUser && currentDir != null) {
			theFile = new File(currentDir, file);
			if (theFile.exists() && theFile.isFile())
				return canonical(theFile);
		}

		return null;
	}

	private File canonical(File theFile) {
		try {
			return theFile.getCanonicalFile();
		} catch (IOException e) {
			return theFile;
		}
	}

	/* (non-Javadoc)
	 * @see com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator#getSystemPaths()
	 */
	public File[] getSystemPaths() {
		return systemPaths;
	}
	
	/* (non-Javadoc)
	 * @see com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator#getUserPaths()
	 */
	public File[] getUserPaths() {
		return userPaths;
	}
}