Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os.path
- import re
- if len( sys.argv ) < 2:
- sys.exit( "usage: " + os.path.split( sys.argv[ 0 ] )[ 1 ] + " <h-file>" )
- rgStructName = re.compile( r"struct\s+([a-zA-Z0-9_]+)" )
- insideComment = False
- insideStruct = False
- structName = ""
- formatStr = ""
- argsStr = ""
- firstField = True
- hFile = open( sys.argv[ 1 ], "rt" )
- outFileName = os.path.splitext( sys.argv[ 1 ] )[ 0 ] + "_logging.h"
- outFile = open( outFileName, "wt" )
- for line in hFile:
- line = line.strip()
- if line.startswith( "//" ):
- continue
- if not insideComment:
- if line.startswith( "/*" ):
- insideComment = True
- if line.endswith( "*/" ):
- insideComment = False
- continue
- if insideComment:
- continue
- if not insideStruct:
- m = rgStructName.search( line )
- if m != None:
- insideStruct = True
- structName = m.group( 1 )
- formatStr = "#define " + structName.upper() + "_FORMAT \""
- argsStr = "#define " + structName.upper() + "_ARGS(p) "
- firstField = True
- else:
- if line.find( "}" ) != -1:
- insideStruct = False
- formatStr += "\""
- print formatStr
- print argsStr
- outFile.write( formatStr + "\n" )
- outFile.write( argsStr + "\n" )
- else:
- dtPos = line.find( ";" )
- if dtPos != -1:
- line = line[ : dtPos ]
- isPointer = False
- starPos = line.find( "*" )
- if starPos != -1:
- isPointer = True
- line = "".join( line.split( "*" ) )
- isArray = False
- pos_arr0 = line.find( "[" )
- pos_arr1 = line.find( "]" )
- if pos_arr0 != -1 and pos_arr1 != -1:
- isArray = True
- line = line[ : pos_arr0 ];
- words = line.split()
- isStruct = "struct" in words
- if ( isStruct or "void" in words ) and not isPointer:
- continue
- if isStruct:
- del words[ 0 ]
- if words[ 0 ] == "const":
- del words[ 0 ]
- isUnsigned = words[ 0 ] == "unsigned"
- if isUnsigned:
- del words[ 0 ]
- fmt = ""
- if isArray:
- if words[ 0 ] == "char":
- fmt = "%s"
- else:
- continue
- elif isPointer:
- fmt = "%s" if words[ 0 ] == "char" else "%p"
- elif words[ 0 ] == "int" or words[ 0 ] == "short":
- fmt = "%ui" if isUnsigned else "%i"
- elif words[ 0 ] == "long":
- if words[ 1 ] == "long":
- fmt = "%llui" if isUnsigned else "%lli"
- del words[ 0 ]
- else:
- fmt = "%lui" if isUnsigned else "%li"
- elif words[ 0 ] == "float":
- fmt = "%f"
- elif words[ 0 ] == "double":
- fmt = "%lf"
- fieldName = words[ 1 ]
- if not firstField:
- formatStr += ", "
- argsStr += ", "
- formatStr += fieldName + "=" + fmt
- argsStr += "(p)->" + fieldName
- firstField = False
Advertisement
Add Comment
Please, Sign In to add comment