borisbn

make_impl

Jun 17th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys, os, re
  2. from Tkinter import *
  3. from tkSimpleDialog import askstring
  4.  
  5. if len( sys.argv ) < 2:
  6.     sys.exit( "Usage: " + os.path.basename( sys.argv[ 0 ] ) + " <h-file> [impl-class-name]|[:ask]" )
  7.  
  8. inFileName = sys.argv[ 1 ]
  9.  
  10. doAsk = False
  11. classImplName = ""
  12. if len( sys.argv ) > 2:
  13.     classImplName = sys.argv[ 2 ]
  14.     if classImplName == ":ask":
  15.         classImplName = ""
  16.         doAsk = True
  17.  
  18. abstractFuncRe = re.compile( r"(\s*\=\s*0\s*)" )
  19. funcNameRe = re.compile( r"(\w+\s*\()" )
  20.  
  21. inFile = open( inFileName, "rt" )
  22. classStarted = False
  23. className = ""
  24. outFileLines_h = []
  25. outFileLines_cpp = []
  26. for line in inFile:
  27.     line = line.strip( "\n\r" )
  28.     tmp = line
  29.     commentPos = tmp.find( "//" )
  30.     if commentPos != -1:
  31.         tmp = tmp[ 0 : commentPos ]
  32.     tmp = tmp.replace( " ", "" )
  33.     tmp = tmp.replace( "\t", "" )
  34.     if tmp.startswith( "#" ) or tmp.startswith( "//" ):
  35.         continue
  36.     if tmp.startswith( "class" ) and not tmp.endswith( ";" ):
  37.         classStarted = True
  38.         words = line.split()
  39.         className = words[ 1 ]
  40.         if classImplName == "":
  41.             if doAsk:
  42.                 r = Tk()
  43.                 r.withdraw()
  44.                 classImplName = askstring( "make_impl", "Enter impl-class-name:", initialvalue = className + "Impl" )
  45.                 r.destroy()
  46.                 if classImplName == None:
  47.                     exit()
  48.             else:
  49.                 classImplName = className + "Impl"
  50.         outFileLines_h.append( "class " + classImplName + " : public " + className + "\n" )
  51.         continue
  52.     if not classStarted:
  53.         continue
  54.     isFunc = False
  55.     funcName = ""
  56.     m = abstractFuncRe.search( line )
  57.     if m != None:
  58.         isFunc = True
  59.         a = m.group( 1 )
  60.         line = line.replace( a, "" )
  61.     if line.find( "~" + className ) != -1 or tmp.startswith( className ):
  62.         continue
  63.     outFileLines_h.append( line + "\n" )
  64.     if isFunc:
  65.         m = funcNameRe.search( line )
  66.         if m != None:
  67.             funcName = m.group( 1 )
  68.             funcName = funcName.strip( " \t(" )
  69.             print funcName
  70.         cppLine = line.replace( funcName, classImplName + "::" + funcName, 1 )
  71.         cppLine = cppLine.replace( "virtual ", "" )
  72.         cppLine = cppLine.replace( "virtual\t", "" )
  73.         cppLine = cppLine.lstrip()
  74.         cppLine = cppLine.rstrip( ";" )
  75.         outFileLines_cpp.append( cppLine + "\n" )
  76.         outFileLines_cpp.append( "{\n" )
  77.         outFileLines_cpp.append( "}\n" )
  78.         outFileLines_cpp.append( "\n" )
  79.  
  80.  
  81. outFileName_h = os.path.dirname( inFileName ) + "\\" + classImplName + ".h"
  82. outFileName_cpp = os.path.dirname( inFileName ) + "\\" + classImplName + ".cpp"
  83. outFile_h = open( outFileName_h, "wt" )
  84. outFile_cpp = open( outFileName_cpp, "wt" )
  85. defGuard = "_" + os.path.basename( outFileName_h ).replace( ".", "_" ).upper() + "_INCLUDED_"
  86. outFile_h.write( "#if !defined( " + defGuard + " )\n" )
  87. outFile_h.write( "#define " + defGuard + "\n" )
  88. outFile_h.write( "\n" )
  89. outFile_h.write( "#include \"" + os.path.basename( inFileName ) + "\"\n" )
  90. outFile_h.write( "\n" )
  91. outFile_cpp.write( "#include \"" + os.path.basename( outFileName_h ) + "\"\n" )
  92. outFile_cpp.write( "\n" )
  93.  
  94. outFile_h.writelines( outFileLines_h )
  95. outFile_cpp.writelines( outFileLines_cpp )
  96.  
  97. outFile_h.write( "\n" )
  98. outFile_h.write( "#endif // !defined( " + defGuard + " )\n" )
  99. outFile_h.close()
  100. outFile_cpp.close()
Advertisement
Add Comment
Please, Sign In to add comment