Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:44

0001 #!/usr/bin/env python3
0002 import argparse
0003 import os
0004 from glob import glob
0005 import re
0006 
0007 code_format = """
0008 #pragma once
0009 {code}
0010 """.strip()
0011 
0012 
0013 def fix_pragma(file):
0014     with open(file, "r+") as f:
0015         text = f.read().strip()
0016 
0017         def repl(m):
0018             code = m.group(2).strip()
0019             return code_format.format(code=code)
0020 
0021         newtext, num = re.subn(
0022             r"#ifndef (.*)\n#define \1.*\n((:?.|\n)+)#endif.*", repl, text, 1
0023         )
0024         if num == 1:
0025             f.seek(0)
0026             f.truncate()
0027             f.write(newtext)
0028 
0029 
0030 def main():
0031     p = argparse.ArgumentParser()
0032     p.add_argument("input")
0033     args = p.parse_args()
0034 
0035     headers = []
0036 
0037     if os.path.isfile(args.input):
0038         headers = [args.input]
0039     elif os.path.isdir(args.input):
0040         patterns = ["**/*.hpp", "**/*.h"]
0041         headers = sum(
0042             [glob(os.path.join(args.input, p), recursive=True) for p in patterns], []
0043         )
0044     else:
0045         headers = glob(args.input, recursive=True)
0046 
0047     # for h in headers: print(h)
0048 
0049     for h in headers:
0050         fix_pragma(h)
0051 
0052 
0053 if "__main__" == __name__:
0054     main()