Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe37d7df6f273f5557a163f6e494f19a63777c04 (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences 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:
 *     Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.util;

import java.io.IOException;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.text.TextUtilities;

public class FileHelper {
	private static final String DEFAULT_LINE_DELIMITTER = "\n"; //$NON-NLS-1$

	public static IFile getFileFromNode(IASTNode node) {
		IPath implPath = new Path(node.getContainingFilename());
		return ResourceLookup.selectFileForLocation(implPath, null);
	}

	public static boolean isFirstWithinSecondLocation(IASTFileLocation loc1, IASTFileLocation loc2) {
		boolean isEquals = true;

		isEquals &= loc1.getFileName().equals(loc2.getFileName());
		isEquals &= loc1.getNodeOffset() >= loc2.getNodeOffset();
		isEquals &= loc1.getNodeOffset() + loc1.getNodeLength() <= loc2.getNodeOffset()
				+ loc2.getNodeLength();

		return isEquals;
	}

	public static String determineLineDelimiter(IFile file) {
		String fileContent = ""; //$NON-NLS-1$

		try {
			fileContent = FileContentHelper.getContent(file, 0);
		} catch (CoreException e) {
		} catch (IOException e) {
		}

		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject();
		IScopeContext[] scopeContext;
		if (project != null) {
			scopeContext = new IScopeContext[] { new ProjectScope(project) };
		} else {
			scopeContext = new IScopeContext[] { InstanceScope.INSTANCE };
		}
		String platformDefaultLineDelimiter = System.getProperty("line.separator", DEFAULT_LINE_DELIMITTER); //$NON-NLS-1$
		String defaultLineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
				Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, scopeContext);
		return TextUtilities.determineLineDelimiter(fileContent.toString(), defaultLineDelimiter);
	}
}

Back to the top