-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppgen
More file actions
executable file
·108 lines (81 loc) · 3.03 KB
/
cppgen
File metadata and controls
executable file
·108 lines (81 loc) · 3.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
import os
import sys
import getopt
import operator
import itertools
def char_repeat(char, times = 1):
return reduce(operator.concat, [char for i in range(times)])
def linesep(times = 1):
return char_repeat(os.linesep, times)
def tab(times = 1):
return char_repeat('\t', times)
def space(times = 1):
return char_repeat(' ', times)
def begin_namespaces(f, namespaces):
max_name_len = max(map(lambda x: len(x), namespaces))
for ns in namespaces:
f.write('namespace ' + ns + space(max_name_len - len(ns) + 1)
+ '{' + linesep())
def end_namespaces(f, namespaces):
for ns in reversed(namespaces):
f.write('} // ' + ns + ' namespace' + linesep())
def print_usage_and_exit():
print "Usage: ..."
sys.exit(0)
if __name__ == '__main__':
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], '',
['ns=', 'filesname='])
except getopt.GetoptError, err:
sys.exit(err)
class_name = sys.argv[1]
namespaces = []
namespace_name_max_len = 0
files_name = class_name
for option, arg in opts:
if option in ('-h', '--help'):
print_usage_and_exit()
elif option in('--ns'):
namespaces = arg.split(',')
namespace_name_max_len = max(map(lambda x: len(x), namespaces))
elif option in ('--filesname'):
files_name = arg
header_file_name = files_name.lower() + '.h'
source_file_name = files_name.lower() + '.cpp'
if os.path.exists(header_file_name):
sys.exit('File ' + header_file_name + ' already exists.')
if os.path.exists(source_file_name):
sys.exit('File ' + source_file_name + ' already exists.')
header_file = open(header_file_name, 'w')
source_file = open(source_file_name, 'w')
# Generate header file.
header_file.write('#ifndef INCLUDED_' + files_name.upper() + linesep())
header_file.write('#define INCLUDED_' + files_name.upper() + linesep(2))
begin_namespaces(header_file, namespaces)
header_file.write(linesep())
header_file.write('class ' + class_name + linesep())
header_file.write('{' + linesep())
header_file.write('public:' + linesep(2))
header_file.write(space(4) + class_name + '();' + linesep())
header_file.write(space(4) + '~' + class_name + '();' + linesep(2))
header_file.write('private:' + linesep(2))
header_file.write(space(4) + class_name
+ '(const ' + class_name + '& rhs);' + linesep());
header_file.write(space(4) + class_name + '& operator=(const ' +
class_name + '& rhs);' + linesep());
header_file.write('};' + linesep(2))
end_namespaces(header_file, namespaces)
header_file.write(linesep())
header_file.write('#endif // INCLUDED_' + class_name.upper() + linesep())
# Generate source file.
source_file.write('#include <' + files_name.lower() + '.h>' + linesep(2))
begin_namespaces(source_file, namespaces)
source_file.write(linesep())
source_file.write(class_name + '::' + class_name + '()' + linesep())
source_file.write('{' + linesep() + '}' + linesep(2))
source_file.write(class_name + '::~' + class_name + '()' + linesep())
source_file.write('{' + linesep() + '}' + linesep(2))
end_namespaces(source_file, namespaces)
header_file.close()
source_file.close()