1import re 2 3preprocessorRe = re.compile( r'\s*#.*' ) 4 5fdefineRe = re.compile( r'\s*#\s*define\s*(\S*)\s*\(' ) # #defines that take arguments 6defineRe = re.compile( r'\s*#\s*define\s*(\S*)(\s+)(.*)' ) # all #defines 7undefRe = re.compile( r'\s*#\s*undef\s*(\S*)' ) # all #undefs 8 9ifdefCommonRe = re.compile( r'\s*#\s*if' ) # all #ifdefs 10ifdefRe = re.compile( r'\s*#\s*ifdef\s*(\S*)' ) 11ifndefRe = re.compile( r'\s*#\s*ifndef\s*(\S*)' ) 12endifRe = re.compile( r'\s*#\s*endif\s*//\s*(.*)' ) 13elseRe = re.compile( r'\s*#\s*else' ) 14ifRe = re.compile( r'\s*#\s*if\s+(.*)' ) 15 16nsRe = re.compile( r'(.*?\s*\s*namespace\s+)(\w+)(\s*{?)(.*)' ) 17nsCloseRe = re.compile( r'(.*\s*})(\s*\/\/\s*namespace\s+)(\w+)(\s*)(.*)' ) 18 19 20class LineMapper: 21 def __init__( self, idMap, outerNamespace ): 22 self.idMap = idMap 23 self.outerNamespace = outerNamespace 24 25 # TBD: 26 # #if, #ifdef, comments after #else 27 def mapLine( self, lineNo, line ): 28 for idFrom, idTo in self.idMap.items(): 29 r = re.compile("(.*)" + idFrom + "(.*)") 30 31 m = r.match( line ) 32 if m: 33 line = m.group(1) + idTo + m.group(2) + "\n" 34 35 m = nsCloseRe.match( line ) 36 if m: 37 originalNs = m.group(3) 38 # print("[{0}] originalNs: '{1}' - closing".format(lineNo, originalNs)) 39 # print( " " + line ) 40 # print( " 1:[{0}]\n 2:[{1}]\n 3:[{2}]\n 4:[{3}]\n 5:[{4}]".format( m.group(1), m.group(2), m.group(3), m.group(4), m.group(5) ) ) 41 if originalNs in self.outerNamespace: 42 outerNs, innerNs = self.outerNamespace[originalNs] 43 return "{0}}}{1}{2}::{3}{4}{5}\n".format( m.group(1), m.group(2), outerNs, innerNs, m.group(4), m.group(5)) 44 m = nsRe.match( line ) 45 if m: 46 originalNs = m.group(2) 47 # print("[{0}] originalNs: '{1}'".format(lineNo, originalNs)) 48 # print( " " + line ) 49 # print( " 1:[{0}]\n 2:[{1}]\n 3:[{2}]\n 4:[{3}]".format( m.group(1), m.group(2), m.group(3), m.group(4) ) ) 50 if originalNs in self.outerNamespace: 51 outerNs, innerNs = self.outerNamespace[originalNs] 52 return "{0}{1} {{ namespace {2}{3}{4}\n".format( m.group(1), outerNs, innerNs, m.group(3), m.group(4) ) 53 54 return line 55 56 def mapFile(self, filenameIn, filenameOut ): 57 print( "Embedding:\n {0}\nas:\n {1}".format( filenameIn, filenameOut ) ) 58 with open( filenameIn, 'r' ) as f, open( filenameOut, 'w' ) as outf: 59 lineNo = 1 60 for line in f: 61 outf.write( self.mapLine( lineNo, line ) ) 62 lineNo = lineNo + 1 63 print( "Written {0} lines".format( lineNo ) )