-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathautogenerate_c_or_cpp_code.py
executable file
·67 lines (49 loc) · 1.51 KB
/
autogenerate_c_or_cpp_code.py
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
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python3
"""
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
Gabriel Staples
Jan. 2021
Demonstrate how to do basic auto-generation of C or C++ code or headers using Python!
To run this script:
./autogenerate_c_or_cpp_code.py
...then examine the contents of "autogenerated/myheader.h", which this script just auto-generated!
References:
1. https://stackoverflow.com/questions/10985603/multi-line-string-with-arguments-how-to-declare/64437283#64437283
1. https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-a-string-and-also-use-format
1. [my code] https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/python/yaml_import/import_yaml_test.py
To produce this:
```
struct myStruct {
int i1;
float f1;
};
```
do this, for example...
"""
import textwrap
structs_dict = {
"struct_name": "myStruct",
"type1": "int",
"name1": "i1",
"type2": "float",
"name2": "f1",
}
f = open("autogenerated/myheader.h", "w")
f.write(textwrap.dedent('''\
// This file is autogenerated by Python script "autogenerate_c_or_cpp_code.py"
#pragma once
struct {} {{
{} {};
{} {};
}};
''').format(
structs_dict["struct_name"],
structs_dict["type1"],
structs_dict["name1"],
structs_dict["type2"],
structs_dict["name2"],
))
f.close()
"""
After running this script, see the output in "autogenerated/myheader.h"!
"""