Guest User

struct_2_log

a guest
Jun 5th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import sys
  2. import os.path
  3. import re
  4.  
  5. if len( sys.argv ) < 2:
  6.     sys.exit( "usage: " + os.path.split( sys.argv[ 0 ] )[ 1 ] + " <h-file>" )
  7.    
  8. rgStructName = re.compile( r"struct\s+([a-zA-Z0-9_]+)" )
  9. insideComment = False
  10. insideStruct = False
  11. structName = ""
  12. formatStr = ""
  13. argsStr = ""
  14. firstField = True
  15.  
  16. hFile = open( sys.argv[ 1 ], "rt" )
  17. outFileName = os.path.splitext( sys.argv[ 1 ] )[ 0 ] + "_logging.h"
  18. outFile = open( outFileName, "wt" )
  19. for line in hFile:
  20.     line = line.strip()
  21.     if line.startswith( "//" ):
  22.         continue
  23.     if not insideComment:
  24.         if line.startswith( "/*" ):
  25.             insideComment = True
  26.     if line.endswith( "*/" ):
  27.         insideComment = False
  28.         continue
  29.     if insideComment:
  30.         continue
  31.        
  32.     if not insideStruct:
  33.         m = rgStructName.search( line )
  34.         if m != None:
  35.             insideStruct = True
  36.             structName = m.group( 1 )
  37.             formatStr = "#define " + structName.upper() + "_FORMAT \""
  38.             argsStr = "#define " + structName.upper() + "_ARGS(p) "
  39.             firstField = True
  40.     else:
  41.         if line.find( "}" ) != -1:
  42.             insideStruct = False
  43.             formatStr += "\""
  44.             print formatStr
  45.             print argsStr
  46.             outFile.write( formatStr + "\n" )
  47.             outFile.write( argsStr + "\n" )
  48.         else:
  49.             dtPos = line.find( ";" )
  50.             if dtPos != -1:
  51.                 line = line[ : dtPos ]
  52.             isPointer = False
  53.             starPos = line.find( "*" )
  54.             if starPos != -1:
  55.                 isPointer = True
  56.                 line = "".join( line.split( "*" ) )
  57.             isArray = False
  58.             pos_arr0 = line.find( "[" )
  59.             pos_arr1 = line.find( "]" )
  60.             if pos_arr0 != -1 and pos_arr1 != -1:
  61.                 isArray = True
  62.                 line = line[ : pos_arr0 ];
  63.             words = line.split()
  64.             isStruct = "struct" in words
  65.             if ( isStruct or "void" in words ) and not isPointer:
  66.                 continue
  67.             if isStruct:
  68.                 del words[ 0 ]
  69.             if words[ 0 ] == "const":
  70.                 del words[ 0 ]
  71.             isUnsigned = words[ 0 ] == "unsigned"
  72.             if isUnsigned:
  73.                 del words[ 0 ]
  74.             fmt = ""
  75.             if isArray:
  76.                 if words[ 0 ] == "char":
  77.                     fmt = "%s"
  78.                 else:
  79.                     continue
  80.             elif isPointer:
  81.                 fmt = "%s" if words[ 0 ] == "char" else "%p"
  82.             elif words[ 0 ] == "int" or words[ 0 ] == "short":
  83.                 fmt = "%ui" if isUnsigned else "%i"
  84.             elif words[ 0 ] == "long":
  85.                 if words[ 1 ] == "long":
  86.                     fmt = "%llui" if isUnsigned else "%lli"
  87.                     del words[ 0 ]
  88.                 else:
  89.                     fmt = "%lui" if isUnsigned else "%li"
  90.             elif words[ 0 ] == "float":
  91.                 fmt = "%f"
  92.             elif words[ 0 ] == "double":
  93.                 fmt = "%lf"
  94.             fieldName = words[ 1 ]
  95.             if not firstField:
  96.                 formatStr += ", "
  97.                 argsStr += ", "
  98.             formatStr += fieldName + "=" + fmt
  99.             argsStr += "(p)->" + fieldName
  100.             firstField = False
Advertisement
Add Comment
Please, Sign In to add comment