Created
September 13, 2016 11:57
-
-
Save elazarl/2a1c610b9d847877ce46dccea7e5b52c to your computer and use it in GitHub Desktop.
Convert .h files using ifdef guard to use pragma once
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
#!/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