project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/api/cpp/epoc/engine/preprocessor/BasicIncludeFileLocator.java
changeset 0 fb279309251b
child 610 bfb3ab3f70f2
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * This implementation searches the directory of the current source file
       
    16 * and a list of provided paths using these rules:
       
    17 * <p>
       
    18 * <li>Every file is checked in the directory of the current #include, if known.
       
    19 * <li>A user include "..." is searched on user paths then system paths.
       
    20 * <li>A system include &lt;...&gt; is searched on system paths only.
       
    21 *
       
    22 */
       
    23 package com.nokia.carbide.internal.api.cpp.epoc.engine.preprocessor;
       
    24 
       
    25 import java.io.File;
       
    26 import java.io.IOException;
       
    27 
       
    28 import com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator;
       
    29 public class BasicIncludeFileLocator implements IIncludeFileLocator {
       
    30 
       
    31 	private static final File[] NO_DIRS = new File[0];
       
    32 	
       
    33 	private File[] userPaths;
       
    34 	private File[] systemPaths;
       
    35 
       
    36 	public BasicIncludeFileLocator(File[] userPaths, File[] systemPaths) {
       
    37 		setPaths(userPaths, systemPaths);
       
    38 	}
       
    39 
       
    40 	public void setPaths(File[] userPaths, File[] systemPaths) {
       
    41 		this.userPaths = userPaths != null ? userPaths : NO_DIRS;
       
    42 		this.systemPaths = systemPaths != null ? systemPaths : NO_DIRS;
       
    43 	}
       
    44 	
       
    45 	public File findIncludeFile(String file, boolean isUser, File currentDir) {
       
    46 		// see if the file exists as an absolute file
       
    47 		File theFile = new File(file);
       
    48 		if (theFile.exists() && theFile.isFile() && theFile.isAbsolute())
       
    49 			return canonical(theFile);
       
    50 
       
    51 		// if a user include and the current directory is known try to get the file relative to this dir
       
    52 		if (isUser && currentDir != null) {
       
    53 			theFile = new File(currentDir, file);
       
    54 			if (theFile.exists() && theFile.isFile())
       
    55 				return canonical(theFile);
       
    56 		}
       
    57 
       
    58 		if (isUser) {
       
    59 			// search user directories first
       
    60 			for (File dir : userPaths) {
       
    61 				theFile = new File(dir, file);
       
    62 				if (theFile.exists() && theFile.isFile())
       
    63 					return canonical(theFile);
       
    64 			}
       
    65 		}
       
    66 		
       
    67 		// always search system directories
       
    68 		for (File dir : systemPaths) {
       
    69 			theFile = new File(dir, file);
       
    70 			if (theFile.exists() && theFile.isFile()) {
       
    71 				return canonical(theFile);
       
    72 			}
       
    73 		}
       
    74 		
       
    75 		// last case - a system include that is located in the same directory as the file including it
       
    76 		if (!isUser && currentDir != null) {
       
    77 			theFile = new File(currentDir, file);
       
    78 			if (theFile.exists() && theFile.isFile())
       
    79 				return canonical(theFile);
       
    80 		}
       
    81 
       
    82 		return null;
       
    83 	}
       
    84 
       
    85 	private File canonical(File theFile) {
       
    86 		try {
       
    87 			return theFile.getCanonicalFile();
       
    88 		} catch (IOException e) {
       
    89 			return theFile;
       
    90 		}
       
    91 	}
       
    92 
       
    93 	/* (non-Javadoc)
       
    94 	 * @see com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator#getSystemPaths()
       
    95 	 */
       
    96 	public File[] getSystemPaths() {
       
    97 		return systemPaths;
       
    98 	}
       
    99 	
       
   100 	/* (non-Javadoc)
       
   101 	 * @see com.nokia.carbide.cpp.epoc.engine.preprocessor.IIncludeFileLocator#getUserPaths()
       
   102 	 */
       
   103 	public File[] getUserPaths() {
       
   104 		return userPaths;
       
   105 	}
       
   106 }