| 18 | return file.read() |
| 19 | |
| 20 | def check_file(filename): |
| 21 | if filename in EXCEPTIONS: |
| 22 | return False |
| 23 | guard_name = ("_".join(filename.split(PATH)[1].split("/"))[:-2]).upper() + "_H" |
| 24 | header_guard_line1 = f"#ifndef {guard_name}" |
| 25 | header_guard_line2 = f"#define {guard_name}" |
| 26 | footer1 = f"#endif // {guard_name}" |
| 27 | footer2 = "#endif" |
| 28 | |
| 29 | lines = read_file(filename).splitlines() |
| 30 | |
| 31 | # check header |
| 32 | for i, line in enumerate(lines): |
| 33 | if line == "// This file can be included several times.": |
| 34 | # file does not need header/footer |
| 35 | return False |
| 36 | if line.startswith("//") or line.startswith("/*") or line.startswith("*/") or line.startswith("\t") or line == "": |
| 37 | continue |
| 38 | if line.startswith("#ifndef"): |
| 39 | if line != header_guard_line1: |
| 40 | print(f"Wrong header guard in {filename}, is: {line}, should be: {header_guard_line1}") |
| 41 | return True |
| 42 | next_line = lines[i + 1] if i + 1 < len(lines) else None |
| 43 | if next_line != header_guard_line2: |
| 44 | print(f"Wrong header guard in {filename}, is: {next_line}, should be: {header_guard_line2}") |
| 45 | return True |
| 46 | break |
| 47 | else: |
| 48 | print(f"Missing header guard in {filename}, should be: {header_guard_line1}") |
| 49 | return True |
| 50 | else: # executed if the loop wasn't broken out of |
| 51 | print(f"Missing header guard in {filename}, file is empty?") |
| 52 | return True |
| 53 | |
| 54 | # check footer |
| 55 | if lines[-1] != footer1 and lines[-1] != footer2: |
| 56 | print(f"Wrong footer in {filename}, is: {lines[-1]}, should be: {footer1}") |
| 57 | return True |
| 58 | |
| 59 | return False |
| 60 | |
| 61 | |
| 62 | def check_dir(directory): |