Change prefix of #include paths

# move_includes

# Change prefix of #include paths

Description:

This transformation allows to modify the prefix of file paths in #include directives.

The motivation for this transformation is to help move whole libraries from one file tree to another.

Please use this transformation as a boilerplate for your own customized version.

For example, the following file:

    #include "boost/shared_ptr.hpp"
    #include "boost/bind.hpp"

will be transformed into:

    #include "boom/shared_ptr.hpp"
    #include "boom/bind.hpp"

Note: The transformation is performed in place, which means that the source files are modified.

Show source code for this transformation

Hide source code

    # change prefix of #include paths

    set oldPrefix "\"boost/"
    set newPrefix "\"boom/"

    set oldPrefixLength [string length $oldPrefix]

    foreach fileName [getSourceFileNames] {
        set tokens [getTokens $fileName 1 0 -1 -1 {}]
        set newFile [open $fileName "w"]
        foreach token $tokens {
            set tokenValue [lindex $token 0]
            set tokenType [lindex $token 3]
            if {$tokenType == "pp_qheader"} {
                set index [string first $oldPrefix $tokenValue]
                if {$index != -1} {
                    set tokenValue [string replace $tokenValue $index [expr $index + $oldPrefixLength - 1] $newPrefix]
                }
            }
            puts -nonewline $newFile $tokenValue
        }
        close $newFile
    }

Transformation index