Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3fa1395681f3a22faf8a6381599e416b6e1461e (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2007 Intel Corporation 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:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.dataprovider;

import java.io.File;

import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.utils.cdtvariables.SupplierBasedCdtVariableSubstitutor;
import org.eclipse.core.runtime.IPath;

class PathInfo {
	private String fUnresolvedStr;
	private IPath fResolvedPath;
	private boolean fIsWorkspacePath;
	private String fAbsoluteInfoStr;
	private Boolean fIsAbsolute;
	private SupplierBasedCdtVariableSubstitutor fSubstitutor;
	
	private static final boolean IS_WINDOWS = File.separatorChar == '\\';


	public PathInfo(String str, boolean isWspPath, SupplierBasedCdtVariableSubstitutor subst){
		fUnresolvedStr = str;
		fIsWorkspacePath = isWspPath;
		fSubstitutor = subst;
	}

	public String getUnresolvedPath(){
		return fUnresolvedStr;
	}
	
	public boolean isWorkspacePath(){
		return fIsWorkspacePath;
	}
	
	public boolean isAbsolute(){
		if(fIsAbsolute == null)
			fIsAbsolute = Boolean.valueOf(checkIsAbsolute());
		return fIsAbsolute.booleanValue();
	}
	
	private boolean checkIsAbsolute(){
//		if(fIsWorkspacePath)
//			return true;
		
		if(fResolvedPath != null)
			return fResolvedPath.isAbsolute();

		if(fAbsoluteInfoStr != null){
			return isAbsolute(fAbsoluteInfoStr, fSubstitutor, new String[1]);
		}

		String str[] = new String[1];
		boolean isAbs = isAbsolute(fUnresolvedStr, fSubstitutor, str);
		fAbsoluteInfoStr = str[0];
		return isAbs;
	}
	
	private static boolean isAbsolute(String str, SupplierBasedCdtVariableSubstitutor subst, String[] out){
		int length = str.length();
		out[0] = str;
		if(length == 0)
			return false;
		
		char c0 = str.charAt(0);
		if(c0 == '/' || c0 == '\\')
			return true;
		
		if(length == 1)
			return false;
		
		char c1 = str.charAt(1);
		if(IS_WINDOWS && c1 == ':')
			return true;
		
		if(length < 4)
			return false;
		
		if(c0 == '$' && c1 == '{'){
			int indx = str.indexOf('}');
			if(indx != -1){
				String macroName = str.substring(2, indx);
				if(macroName.length() != 0){
					String resolvedMacro;
					try {
						resolvedMacro = subst.resolveToString(macroName);
					} catch (CdtVariableException e) {
						ManagedBuilderCorePlugin.log(e);
						resolvedMacro = null;
						e.printStackTrace();
					}
					String substr = str.substring(indx + 1);
					String rStr = resolvedMacro == null || resolvedMacro.length() == 0 ?
							substr : new StringBuffer().append(resolvedMacro).append(subst).toString();
					return isAbsolute(rStr, subst, out);
				}
			}
		}
		
		return false;
	}
	
	public SupplierBasedCdtVariableSubstitutor getSubstitutor(){
		return fSubstitutor;
	}
}

Back to the top