-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboostFormatToSTD.py
58 lines (51 loc) · 2.08 KB
/
boostFormatToSTD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re, os
def boostToSTD(contents):
output = re.sub(r'.*fmter ((% .*?)+);', r'\1', contents, 0, re.DOTALL)
output = re.sub(r' *\n[ \t%]*', r'%', output)
args = output.split('%')
output = re.sub(r'.*?boost::w?format fmter\(L?"((\\?.)*)"\);.*', r'\1', contents, 0, re.DOTALL)
spacing = re.sub(r'.*?boost::w?format fmter\(L?"((\\?.)*)"\);([\n \t]*)[^\n\t ]*?.*', r'\3', contents, 0, re.DOTALL)
output2 = 'std::basic_ostringstream<TCHAR> sStream;' + spacing + 'sStream << _T("'
i = 0
argNb = 1
while i < len(output):
if output[i] == '%' and i < len(output)-2 and output[i+2] == '%':
output2 += '") << ' + args[argNb] + ' << _T("'
i = i + 3
argNb = argNb + 1
else:
output2 += output[i]
i = i + 1
output2 += '");'
ending = re.sub(r'.*?boost::w?format fmter\(L?"((\\?.)*)"\);.*?(\n[ \t]*[^\n]*?fmter.str\(\).*?;)', r'\3', contents, 0, re.DOTALL)
ending = re.sub(r'fmter.str\(\)', r'sStream.str()', ending)
return output2 + ending
def applyRegexToFile(filename, sOutFile):
file = open(filename, 'r')
fileContents = file.read()
output = ''
lastMatched = 0
for match in list(re.finditer(r'boost::w?format fmter\(L?"(\\?.)*?"\);.*?fmter (% .*?)+;.*?fmter.str\(\).*?;', fileContents, re.DOTALL)):
subStr = boostToSTD(fileContents[match.start():match.end()])
output += fileContents[lastMatched:match.start()]
output += subStr
lastMatched = match.end()
output += fileContents[lastMatched:]
file.close()
file = open(sOutFile, 'w')
file.write(output)
file.close()
sOutputFolder = 'test2'
sInputFolder = 'test'
for dirname, dirnames, filenames in os.walk(sInputFolder):
# Browse all files in current subfolder
for filename in filenames:
# Retrieve the name of the current file
sParsedFile = os.path.join(dirname, filename)
# Compute the name of the file to create
sPathToFile = sOutputFolder + sParsedFile[len(sInputFolder):len(sParsedFile)-len(filename)]
if not os.path.exists(sPathToFile):
os.mkdir(sPathToFile)
sCreatedFile = sOutputFolder + sParsedFile[len(sInputFolder):len(sParsedFile)]
# Parse current file
applyRegexToFile(sParsedFile, sCreatedFile)