Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert .h files using ifdef guard to use pragma once
#!/usr/bin/python
import sys
import os
import tempfile
import re
starts_with_ifndef = re.compile(r'^\s*#ifndef.*\n#define.*\n')
ends_with_endif = re.compile(r'#endif.*$')
def topragmaonce(filename):
"topragmaonce will remove #ifndef...#endif guard, and put #pragma once instead"
tmp = tempfile.NamedTemporaryFile(delete=False)
with open(filename, 'r') as fp:
data = fp.read()
if not starts_with_ifndef.match(data):
return
data = starts_with_ifndef.sub('', data)
data = ends_with_endif.sub('', data)
tmp.write('#pragma once\n')
tmp.write(data)
tmp.close()
os.rename(tmp.name, filename)
for arg in sys.argv[1:]:
topragmaonce(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment