Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, os, re
- from Tkinter import *
- from tkSimpleDialog import askstring
- if len( sys.argv ) < 2:
- sys.exit( "Usage: " + os.path.basename( sys.argv[ 0 ] ) + " <h-file> [impl-class-name]|[:ask]" )
- inFileName = sys.argv[ 1 ]
- doAsk = False
- classImplName = ""
- if len( sys.argv ) > 2:
- classImplName = sys.argv[ 2 ]
- if classImplName == ":ask":
- classImplName = ""
- doAsk = True
- abstractFuncRe = re.compile( r"(\s*\=\s*0\s*)" )
- funcNameRe = re.compile( r"(\w+\s*\()" )
- inFile = open( inFileName, "rt" )
- classStarted = False
- className = ""
- outFileLines_h = []
- outFileLines_cpp = []
- for line in inFile:
- line = line.strip( "\n\r" )
- tmp = line
- commentPos = tmp.find( "//" )
- if commentPos != -1:
- tmp = tmp[ 0 : commentPos ]
- tmp = tmp.replace( " ", "" )
- tmp = tmp.replace( "\t", "" )
- if tmp.startswith( "#" ) or tmp.startswith( "//" ):
- continue
- if tmp.startswith( "class" ) and not tmp.endswith( ";" ):
- classStarted = True
- words = line.split()
- className = words[ 1 ]
- if classImplName == "":
- if doAsk:
- r = Tk()
- r.withdraw()
- classImplName = askstring( "make_impl", "Enter impl-class-name:", initialvalue = className + "Impl" )
- r.destroy()
- if classImplName == None:
- exit()
- else:
- classImplName = className + "Impl"
- outFileLines_h.append( "class " + classImplName + " : public " + className + "\n" )
- continue
- if not classStarted:
- continue
- isFunc = False
- funcName = ""
- m = abstractFuncRe.search( line )
- if m != None:
- isFunc = True
- a = m.group( 1 )
- line = line.replace( a, "" )
- if line.find( "~" + className ) != -1 or tmp.startswith( className ):
- continue
- outFileLines_h.append( line + "\n" )
- if isFunc:
- m = funcNameRe.search( line )
- if m != None:
- funcName = m.group( 1 )
- funcName = funcName.strip( " \t(" )
- print funcName
- cppLine = line.replace( funcName, classImplName + "::" + funcName, 1 )
- cppLine = cppLine.replace( "virtual ", "" )
- cppLine = cppLine.replace( "virtual\t", "" )
- cppLine = cppLine.lstrip()
- cppLine = cppLine.rstrip( ";" )
- outFileLines_cpp.append( cppLine + "\n" )
- outFileLines_cpp.append( "{\n" )
- outFileLines_cpp.append( "}\n" )
- outFileLines_cpp.append( "\n" )
- outFileName_h = os.path.dirname( inFileName ) + "\\" + classImplName + ".h"
- outFileName_cpp = os.path.dirname( inFileName ) + "\\" + classImplName + ".cpp"
- outFile_h = open( outFileName_h, "wt" )
- outFile_cpp = open( outFileName_cpp, "wt" )
- defGuard = "_" + os.path.basename( outFileName_h ).replace( ".", "_" ).upper() + "_INCLUDED_"
- outFile_h.write( "#if !defined( " + defGuard + " )\n" )
- outFile_h.write( "#define " + defGuard + "\n" )
- outFile_h.write( "\n" )
- outFile_h.write( "#include \"" + os.path.basename( inFileName ) + "\"\n" )
- outFile_h.write( "\n" )
- outFile_cpp.write( "#include \"" + os.path.basename( outFileName_h ) + "\"\n" )
- outFile_cpp.write( "\n" )
- outFile_h.writelines( outFileLines_h )
- outFile_cpp.writelines( outFileLines_cpp )
- outFile_h.write( "\n" )
- outFile_h.write( "#endif // !defined( " + defGuard + " )\n" )
- outFile_h.close()
- outFile_cpp.close()
Advertisement
Add Comment
Please, Sign In to add comment