1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-21 07:55:24 +02:00

2004-09-22 Chris Wiebe

added file naming conventions
	* index/org/eclipse/cdt/internal/core/messages.properties
	* src/org/eclipse/cdt/core/CConventions.java
This commit is contained in:
Chris Wiebe 2004-09-22 18:09:30 +00:00
parent b6b0fda824
commit 565312d1a6
3 changed files with 119 additions and 1 deletions

View file

@ -1,3 +1,9 @@
2004-09-22 Chris Wiebe
added file naming conventions
* index/org/eclipse/cdt/internal/core/messages.properties
* src/org/eclipse/cdt/core/CConventions.java
2004-09-21 Alain Magloire
* model/org/eclipse/cdt/core/model/IBinary.java
* model/org/eclipse/cdt/internal/core/model/Archive.java

View file

@ -41,3 +41,9 @@ convention.namespace.dollarName= Namespace has $
convention.namespace.leadingUnderscore= Namespace starts with underscore
convention.namespace.lowercaseName= Namespace starts with lower case
convention.namespace.invalidName= Namespace is invalid
convention.filename.nullName=File name is blank
convention.filename.invalid=File name contains illegal characters
convention.filename.nameWithBlanks=File name contains spaces
convention.headerFilename.filetype=File extension does not correspond to known header file types
convention.sourceFilename.filetype=File extension does not correspond to known source file types

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.core;
import java.util.StringTokenizer;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IScanner;
@ -23,6 +24,7 @@ import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.model.CModelStatus;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@ -33,6 +35,8 @@ public class CConventions {
private final static String scopeResolutionOperator= "::"; //$NON-NLS-1$
private final static char fgDot= '.';
private final static String ILLEGAL_FILE_CHARS = "/\\:<>?*|\"";
private static boolean isLegalIdentifier(String name) {
if (name == null) {
return false;
@ -316,4 +320,106 @@ public class CConventions {
return false;
}
}
private static boolean isLegalFilename(String name) {
if (name == null || name.length() == 0) {
return false;
}
int len = name.length();
// if (Character.isWhitespace(name.charAt(0)) || Character.isWhitespace(name.charAt(len - 1))) {
// return false;
// }
for (int i = 0; i < len; i++) {
char c = name.charAt(i);
if (ILLEGAL_FILE_CHARS.indexOf(c) != -1) {
return false;
}
}
return true;
}
/**
* Validate the given file name.
* The name must be the short file name (including the extension).
* It should not contain any prefix or path delimiters.
*
* @param name the file name
* @return a status object with code <code>IStatus.OK</code> if
* the given name is valid as a C/C++ file name,
* a status with code <code>IStatus.WARNING</code>
* indicating why the given name is discouraged,
* otherwise a status object indicating what is wrong with
* the name
*/
public static IStatus validateFileName(String name) {
//TODO could use a prefs option for file naming conventions
if (name == null || name.length() == 0) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.filename.nullName"), null); //$NON-NLS-1$
}
if (!isLegalFilename(name)) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.filename.invalid"), null); //$NON-NLS-1$
}
String trimmed = name.trim();
if ((!name.equals(trimmed)) || (name.indexOf(" ") != -1)) { //$NON-NLS-1$
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.filename.nameWithBlanks"), null); //$NON-NLS-1$
}
return CModelStatus.VERIFIED_OK;
}
/**
* Validate the given header file name.
* The name must be the short file name (including the extension).
* It should not contain any prefix or path delimiters.
*
* @param name the header file name
* @return a status object with code <code>IStatus.OK</code> if
* the given name is valid as a C/C++ header file name,
* a status with code <code>IStatus.WARNING</code>
* indicating why the given name is discouraged,
* otherwise a status object indicating what is wrong with
* the name
*/
public static IStatus validateHeaderFileName(IProject project, String name) {
//TODO could use a prefs option for header file naming conventions
IStatus val = validateFileName(name);
if (val.getSeverity() == IStatus.ERROR) {
return val;
}
if (!CoreModel.isValidHeaderUnitName(project, name)) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.headerFilename.filetype"), null); //$NON-NLS-1$
}
return val;
}
/**
* Validate the given source file name.
* The name must be the short file name (including the extension).
* It should not contain any prefix or path delimiters.
*
* @param name the source file name
* @return a status object with code <code>IStatus.OK</code> if
* the given name is valid as a C/C++ source file name,
* a status with code <code>IStatus.WARNING</code>
* indicating why the given name is discouraged,
* otherwise a status object indicating what is wrong with
* the name
*/
public static IStatus validateSourceFileName(IProject project, String name) {
//TODO could use a prefs option for source file naming conventions
IStatus val = validateFileName(name);
if (val.getSeverity() == IStatus.ERROR) {
return val;
}
if (!CoreModel.isValidSourceUnitName(project, name)) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.sourceFilename.filetype"), null); //$NON-NLS-1$
}
return val;
}
}