Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created September 13, 2016 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elazarl/2a1c610b9d847877ce46dccea7e5b52c to your computer and use it in GitHub Desktop.
Save elazarl/2a1c610b9d847877ce46dccea7e5b52c to your computer and use it in GitHub Desktop.
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