Skip to content

Instantly share code, notes, and snippets.

@ppearson
Last active June 28, 2017 04:44
Show Gist options
  • Save ppearson/9db2220db83d6e967c944d2b7f1920e3 to your computer and use it in GitHub Desktop.
Save ppearson/9db2220db83d6e967c944d2b7f1920e3 to your computer and use it in GitHub Desktop.
Namespace inserter
# Python util to add namespace item to a C++ codebase
# Note: does not cope with platform specific #define blocks
import os
basePath = ""
def getListOfFiles(path, extensionMatch):
listOfFiles = []
for root, dirnames, filenames in os.walk(path):
for filename in filenames:
extension = os.path.splitext(filename)[1]
if extension == extensionMatch:
fullPath = os.path.join(root, filename)
listOfFiles.append(fullPath)
return listOfFiles
def processHeaderFile(headerFile, newNamespace):
f = open(headerFile, "r")
contents = f.readlines()
f.close()
if len(contents) == 0:
return False
# now go through lines
# making a note of indices of things
lastHashIncludeLine = -1
lastHashEndIfLine = -1
firstHashDefineLine = -1
index = 0
for line in contents:
if line[0] != "#":
index += 1
continue
if line.find("#include ") != -1:
lastHashIncludeLine = index
elif line.find("#endif") != -1:
lastHashEndIfLine = index
elif line.find("#define") != -1 and firstHashDefineLine == -1:
firstHashDefineLine = index
index += 1
if (lastHashIncludeLine == -1 and firstHashDefineLine == -1) or lastHashEndIfLine == -1:
return False
# start from the bottom, so we don't need to offset the indices by modifying the content...
contents.insert(lastHashEndIfLine, "\n")
endNamespaceLine = "} // namespace %s\n" % (newNamespace)
contents.insert(lastHashEndIfLine, endNamespaceLine)
if lastHashIncludeLine != -1:
startNamespaceLine = "namespace %s\n" % (newNamespace)
contents.insert(lastHashIncludeLine + 1, "\n")
contents.insert(lastHashIncludeLine + 2, startNamespaceLine)
contents.insert(lastHashIncludeLine + 3, "{\n")
elif firstHashDefineLine != -1:
# we didn't have a #include, but we had a #define, which hopefully was the include guard...
startNamespaceLine = "namespace %s\n" % (newNamespace)
contents.insert(firstHashDefineLine + 1, "\n")
contents.insert(firstHashDefineLine + 2, startNamespaceLine)
contents.insert(firstHashDefineLine + 3, "{\n")
f = open(headerFile, "w+")
contents = "".join(contents)
f.write(contents)
f.close()
return True
def processImplFile(implFile, newNamespace):
f = open(implFile, "r")
contents = f.readlines()
f.close()
# now go through lines
# making a note of indices of things
lastHashIncludeLine = -1
lastGlobalNamespaceLine = -1
index = 0
for line in contents:
if line[:9] == "namespace":
lastGlobalNamespaceLine = index
if line[0] != "#":
index += 1
continue
if line.find("#include ") != -1:
lastHashIncludeLine = index
index += 1
if lastHashIncludeLine == -1:
return False
startNamespaceLine = "namespace %s\n" % (newNamespace)
endNamespaceLine = "} // namespace %s\n" % (newNamespace)
if lastGlobalNamespaceLine == -1:
# we can just append
contents.append("\n")
contents.append(endNamespaceLine)
else:
# otherwise, we need to insert the new namespace before the start of the global one
contents.insert(lastGlobalNamespaceLine, "\n")
contents.insert(lastGlobalNamespaceLine, endNamespaceLine)
contents.insert(lastHashIncludeLine + 1, "\n")
contents.insert(lastHashIncludeLine + 2, startNamespaceLine)
contents.insert(lastHashIncludeLine + 3, "{\n")
f = open(implFile, "w+")
contents = "".join(contents)
f.write(contents)
f.close()
def processFiles(listOfHeaderFiles, listOfImplFiles, newNamespace):
for headerFilePath in listOfHeaderFiles:
processHeaderFile(headerFilePath, newNamespace)
for implFilePath in listOfImplFiles:
processImplFile(implFilePath, newNamespace)
def processPath(path):
listOfHeaders = getListOfFiles(path, ".h")
listOfImpls = getListOfFiles(path, ".cpp")
processFiles(listOfHeaders, listOfImpls, "Imagine")
processPath("/Users/peter/Documents/Coding/Imagine/src/")
#print getListOfFiles("/Users/peter/Documents/Coding/Imagine/src", ".cpp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment