File names should be well-formed

# F002

# File names should be well-formed

Description:

The source file names should be well-formed in the sense of their allowed maximum length and directory depth. Directory and file names should start with alphabetic character or underscore. In addition, directory names should not contain dots and file names can have only one dot.

Recognized parameters:

Name | Default | Description
--- | --- | ---
`max-directory-depth` | `8` | Maximum depth of the directory structure.
`max-dirname-length` | `31` | Maximum length of the directory path component.
`max-filename-length` | `31` | Maximum length of the leaf file name.
`max-path-length` | `100` | Maximum length of the full path.

Compliance: Boost

Show source code for this rule

Hide source code

    # File names should be well-formed

    set maxDirectoryDepth [getParameter "max-directory-depth" 8]
    set maxDirnameLength [getParameter "max-dirname-length" 31]
    set maxFilenameLength [getParameter "max-filename-length" 31]
    set maxPathLength [getParameter "max-path-length" 100]

    foreach fileName [getSourceFileNames] {
        if {[string length $fileName] > $maxPathLength} {
            report $fileName 1 "path name too long"
        }

        set dirDepth 0
        foreach dir [file split [file dirname $fileName]] {
            if {$dir == "/" || $dir == "." || $dir == ".."} {
                continue
            }

            incr dirDepth

            if {[string length $dir] > $maxDirnameLength} {
                report $fileName 1 "directory name component too long"
                break
            }

            set first [string index $dir 0]
            if {[string is alpha $first] == 0 && $first != "_"} {
                report $fileName 1 "directory name should start with alphabetic character or underscore"
                break
            }

            if {[string first "." $dir] != -1} {
                report $fileName 1 "directory name should not contain the dot"
                break
            }
        }

        if {$dirDepth >= $maxDirectoryDepth} {
            report $fileName 1 "directory structure too deep"
        }

        set leafName [file tail $fileName]
        if {[string length $leafName] > $maxFilenameLength} {
            report $fileName 1 "file name too long"
        }

        set first [string index $leafName 0]
        if {[string is alpha $first] == 0 && $first != "_"} {
            report $fileName 1 "file name should start with alphabetic character or underscore"
        }

        if {[llength [split $leafName .]] > 2} {
            report $fileName 1 "file name should not contain more than one dot"
        }
    }

Rule index