Skip to main content
summaryrefslogtreecommitdiffstats
blob: bae9031b40307ea3c99951282a47d263ec0aace9 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2009, 2013 Wind River Systems, Inc. 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:
 *     Markus Schorn - initial API and implementation
 *     Sergey Prigogin (Google)
 *     Chris Recoskie (IBM Corporation)
 *******************************************************************************/ 
package org.eclipse.cdt.core.parser;

import java.io.File;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.scanner.CharArray;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;

/**
 * Abstract class for representing the content of a file.
 * It serves as the input to the preprocessor.
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @since 5.2
 */
public abstract class FileContent {
	/** @since 5.4 */
	public static final long NULL_TIMESTAMP = -1;
	/** @since 5.4 */
	public static final long NULL_FILE_SIZE = -1;

	/** 
	 * Returns the location of this file content as it will appear in {@link IASTFileLocation#getFileName()}
	 */
	public abstract String getFileLocation();

	/**
	 * Returns the modification time of the file containing the content, or NULL_TIMESTAMP if
	 * the content does not originate from a file. A zero value may be returned if there was
	 * an I/O error.
	 * @since 5.4
	 */
	public abstract long getTimestamp();

	/**
	 * Returns time when the file was read. Corresponds to the start of reading.    
	 * @return time before reading started in milliseconds since epoch
	 * @since 5.4
	 */
	public abstract long getReadTime();

	/**
	 * Returns the size of the file, or NULL_FILE_SIZE if the content does not originate from
	 * a file. A zero value may be returned if there was an I/O error.
	 * @since 5.4
	 */
	public abstract long getFileSize();

	/**
	 * Returns {@code true} if there were I/O errors while retrieving contents of this file.
	 * @since 5.4
	 */
	public abstract boolean hasError();

	/**
	 * Returns a 64-bit hash value of the file contents.
	 */
	public abstract long getContentsHash();

	/**
	 * Creates a file content object for a fixed buffer.
	 * @param filePath the path of the file as it will appear in {@link IASTFileLocation#getFileName()}
	 * @param contents the actual content.
	 */
	public static FileContent create(String filePath, char[] contents) {
		return new InternalFileContent(filePath, new CharArray(contents));
	}

	/**
	 * Creates a file content object for a translation-unit, which may be a working copy.
	 */
	public static FileContent create(ITranslationUnit tu) {
		IPath location= tu.getLocation();
		if (location == null)
			return create(tu.getElementName(), tu.getContents());
		
		if (tu.isWorkingCopy()) {
			return create(location.toOSString(), tu.getContents());
		}
		
		IResource res= tu.getResource();
		if (res instanceof IFile) {
			return create((IFile) res);
		}
		return createForExternalFileLocation(location.toOSString());
	}
	
	/**
	 * Creates a file content object for an index file location.
	 */
	public static FileContent create(IIndexFileLocation ifl) {
		return InternalParserUtil.createFileContent(ifl);
	}

	/**
	 * Creates a file content for a workspace file
	 */
	public static FileContent create(IFile file) {
		return InternalParserUtil.createWorkspaceFileContent(file);
	}

	public static FileContent createForExternalFileLocation(String fileLocation) {
		return createForExternalFileLocation(fileLocation, InternalParserUtil.SYSTEM_DEFAULT_ENCODING);
	}

	/**
	 * Creates a file content object for a file location that is not part of the workspace
	 * @since 5.3
	 */
	public static FileContent createForExternalFileLocation(String fileLocation, String encoding) {
		return InternalParserUtil.createExternalFileContent(fileLocation, encoding);
	}

	/**
	 * Provided to achieve backwards compatibility.
	 */
	@Deprecated
	public static FileContent adapt(CodeReader reader) {
		if (reader == null)
			return null;
		
		long fileReadTime = System.currentTimeMillis();
		CharArray chars = new CharArray(reader.buffer);
		String filePath = reader.getPath();
		File file = new File(filePath);
		return new InternalFileContent(filePath, chars, file.lastModified(), file.length(), fileReadTime);
	}
}

Back to the top