-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.py
More file actions
57 lines (43 loc) · 1.98 KB
/
Copy pathembed.py
File metadata and controls
57 lines (43 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import re
import sys
import os
def embed_files(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
content = f.read()
# Регулярное выражение для поиска //c:embed filename и следующей строки static const char *name;
pattern = r'(//c:embed \s*([^\s]+))\s*\n\s*(.*?char\s*\*\s*\w+\s*;)'
matches = list(re.finditer(pattern, content))
print(f"Found {len(matches)} matches.")
for i, match in enumerate(matches):
print(f"Match {i+1}:")
print(f" Full match: {repr(match.group(0))}")
print(f" Tag: {repr(match.group(1))}")
print(f" Filename: {repr(match.group(2))}")
print(f" Declaration: {repr(match.group(3))}")
def replace_match(match):
tag = match.group(1) # //c:embed filename
filename = match.group(2) # filename
declaration = match.group(3) # static const char *name;
if not os.path.exists(filename):
print(f"Error: file '{filename}' not found for tag '{tag}'")
sys.exit(1)
with open(filename, 'r', encoding='utf-8') as f:
file_content = f.read()
# Заменяем ; на = R"glsl(...)glsl";
embedded_line = declaration.replace(";", f' = R"glsl(\n{file_content})glsl";')
new_tag = tag.replace('//c:embed ', '//c:embedded ')
print(f" Replacing with: {repr(new_tag + chr(10) + embedded_line[:80])}...")
return f'{new_tag}\n{embedded_line}'
# Заменяем все совпадения
content = re.sub(pattern, replace_match, content)
with open(output_file, 'w', encoding='utf-8') as f:
f.write(content)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python embed.py input.c output.c")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
embed_files(input_file, output_file)
print(f"Embedded files from {input_file} to {output_file}")