diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index f0b49f62dd6..b012b83ef1d 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -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 diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties index a781efd3a06..4725c5de4e7 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties @@ -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 diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java index ebf4233d626..4dafb90c47b 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java @@ -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; } -} \ No newline at end of file + + 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 IStatus.OK if + * the given name is valid as a C/C++ file name, + * a status with code IStatus.WARNING + * 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 IStatus.OK if + * the given name is valid as a C/C++ header file name, + * a status with code IStatus.WARNING + * 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 IStatus.OK if + * the given name is valid as a C/C++ source file name, + * a status with code IStatus.WARNING + * 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; + } +}