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(cdr) "
- firstField = True
- else:
- if line.find( "}" ) != -1:
- insideStruct = False
- formatStr += "\""
- print formatStr
- print argsStr
- outFile.write( formatStr + "\n" )
- outFile.write( argsStr + "\n" )
- else:
- commentPos = line.find( "//" )
- if commentPos != -1:
- line = line[ 0 : commentPos ]
- isPointer = line.find( "*" ) != -1
- words = line.split()
- if words[ len( words ) - 1 ].strip() == ";":
- del words[ len( words ) - 1 ]
- fieldName = words[ len( words ) - 1 ]
- del words[ len( words ) - 1 ]
- fieldType = "".join( words )
- if fieldName.startswith( "*" ):
- fieldName = fieldName[ 1: ]
- if fieldName.endswith( ";" ):
- fieldName = fieldName[ : len( fieldName ) - 1 ]
- if not firstField:
- formatStr += ", "
- argsStr += ", "
- formatStr += fieldName + "="
- if isPointer:
- formatStr += "%p"
- else:
- if fieldType.find( "int" ) != -1:
- formatStr += "%d"
- elif fieldType.find( "float" ) != -1:
- formatStr += "%f"
- argsStr += "(cdr)->" + fieldName
- firstField = False
Advertisement
Add Comment
Please, Sign In to add comment