Last active
May 6, 2020 09:25
-
-
Save lemon32767/30d9f375759a2ae5995ae8bbe8ad9607 to your computer and use it in GitHub Desktop.
format C multiline macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
""" | |
usage: mmacrofmt.py [tabsize] < infile > outfile | |
formats something like: | |
#define X(a) \ | |
whatever \ | |
missing a backslash | |
//some comment | |
hmm\ | |
into | |
#define X(a) \ | |
whatever \ | |
missing a backslash \ | |
/*some comment*/ \ | |
hmm \ | |
""" | |
import sys, re | |
tabsize = 2 | |
if len(sys.argv) > 1: | |
tabsize = int(sys.argv[1]) | |
lines = list(sys.stdin.readlines()) | |
col = 0 | |
for i in range(0,len(lines)): | |
l = lines[i].replace('\t'," "*tabsize) | |
if l[len(l)-2] != '\\': | |
l = re.sub("\\/\\/(.*)$", "/*\\1*/", l) | |
l = re.sub("\\\\$","",l).rstrip() | |
lines[i] = l | |
col = len(l)+1 if len(l) > col-1 else col | |
for l in lines: | |
print(l + " "*(col - len(l)) + "\\") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment