Skip to content

Instantly share code, notes, and snippets.

@lemon32767
Last active May 6, 2020 09:25
Show Gist options
  • Save lemon32767/30d9f375759a2ae5995ae8bbe8ad9607 to your computer and use it in GitHub Desktop.
Save lemon32767/30d9f375759a2ae5995ae8bbe8ad9607 to your computer and use it in GitHub Desktop.
format C multiline macros
#!/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