Guest User

struct logging

a guest
Jun 4th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 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(cdr) "
  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.             commentPos = line.find( "//" )
  50.             if commentPos != -1:
  51.                 line = line[ 0 : commentPos ]
  52.             isPointer = line.find( "*" ) != -1
  53.             words = line.split()
  54.             if words[ len( words ) - 1 ].strip() == ";":
  55.                 del words[ len( words ) - 1 ]
  56.             fieldName = words[ len( words ) - 1 ]
  57.             del words[ len( words ) - 1 ]
  58.             fieldType = "".join( words )
  59.             if fieldName.startswith( "*" ):
  60.                 fieldName = fieldName[ 1: ]
  61.             if fieldName.endswith( ";" ):
  62.                 fieldName = fieldName[ : len( fieldName ) - 1 ]
  63.             if not firstField:
  64.                 formatStr += ", "
  65.                 argsStr += ", "
  66.             formatStr += fieldName + "="
  67.             if isPointer:
  68.                 formatStr += "%p"
  69.             else:
  70.                 if fieldType.find( "int" ) != -1:
  71.                     formatStr += "%d"
  72.                 elif fieldType.find( "float" ) != -1:
  73.                     formatStr += "%f"
  74.             argsStr += "(cdr)->" + fieldName
  75.             firstField = False
Advertisement
Add Comment
Please, Sign In to add comment