-
Notifications
You must be signed in to change notification settings - Fork 0
/
createClass.sh
executable file
·208 lines (174 loc) · 3.79 KB
/
createClass.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
uppercaseName=$(printf '%s' "$1" | tr "a-z" "A-Z" )
# hpp file
echo "#ifndef ${uppercaseName}_H" > $1.hpp
echo "#define ${uppercaseName}_H" >> $1.hpp
echo "#include <iostream>" >> $1.hpp
echo "#include <string>" >> $1.hpp
echo " " >> $1.hpp
echo "// Class definition " >> $1.hpp
echo "class $1\n{" >> $1.hpp
printf "private: \n" >> $1.hpp
c=1
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
echo "_$i;" >> $1.hpp
else
printf "\t$i " >> $1.hpp
fi
c=$((c+1))
done
echo " " >> $1.hpp
printf "public: \n" >> $1.hpp
echo "// Constructor " >> $1.hpp
printf "\t$1(); \n" >> $1.hpp
printf "\t$1( const $1 &rhs); \n" >> $1.hpp
if [ $# -gt 2 ]
then
printf "\t$1(" >> $1.hpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
printf "${prev} new$j" >> $1.hpp
if [ $i != ${!#} ]
then
printf ", " >> $1.hpp
else
printf ");\n" >> $1.hpp
fi
else
prev=$i
fi
c=$((c+1))
done
fi
printf "\t~$1(); \n" >> $1.hpp
printf "\t$1& operator=( const $1 &rhs); \n" >> $1.hpp
echo "// Getters " >> $1.hpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
echo "$j(); " >> $1.hpp
else
printf "\t$i get" >> $1.hpp
fi
c=$((c+1))
done
echo "// Setters " >> $1.hpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
printf "\tvoid set$j(${prev} _$i); \n" >> $1.hpp
else
prev=$i
fi
c=$((c+1))
done
echo "// Output " >> $1.hpp
printf "\tvoid output(); \n" >> $1.hpp
echo "}; " >> $1.hpp
echo " " >> $1.hpp
echo "#endif" >> $1.hpp
# cpp file
echo "#include \"$1.hpp\"" > $1.cpp
echo "// Constructor initializes attributes to 0 by default " >> $1.cpp
echo "$1::$1()" >> $1.cpp
printf "\t: " >> $1.cpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
printf "_$i(0)" >> $1.cpp
if [ $i != ${!#} ]
then
printf ", " >> $1.cpp
fi
fi
c=$((c+1))
done
echo "\n{\n\n}" >> $1.cpp
echo " " >> $1.cpp
echo "$1::$1( const $1& rhs)" >> $1.cpp
echo "{" >> $1.cpp
echo "\t*this = rhs;" >> $1.cpp
echo "}" >> $1.cpp
echo " " >> $1.cpp
if [ $# -gt 2 ]
then
printf "$1::$1(" >> $1.cpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
printf "${prev} new$j" >> $1.cpp
if [ $i != ${!#} ]
then
printf ", " >> $1.cpp
else
printf ") \n\t: " >> $1.cpp
fi
else
prev=$i
fi
c=$((c+1))
done
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
printf "_$i(new$j)" >> $1.cpp
if [ $i != ${!#} ]
then
printf ", " >> $1.cpp
else
printf "\n{\n\n}\n\n" >> $1.cpp
fi
else
prev=$i
fi
c=$((c+1))
done
fi
echo "$1::~$1()\n{" >> $1.cpp
echo "" >> $1.cpp
echo "}\n" >> $1.cpp
echo "$1&\t$1::operator=( const $1& rhs )\n{" >> $1.cpp
echo "\treturn *this;" >> $1.cpp
echo "}\n" >> $1.cpp
echo "// Getters " >> $1.cpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
echo "$prev $1::get$j() { return _$i; }" >> $1.cpp
else
prev=$i
fi
c=$((c+1))
done
echo " " >> $1.cpp
echo "// Setters " >> $1.cpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
j="$(tr "a-z" "A-Z" <<< ${i:0:1})${i:1}"
echo "void $1::set$j($prev new$j) { _$i = new$j; }" >> $1.cpp
else
prev=$i
fi
c=$((c+1))
done
echo " " >> $1.cpp
echo "// Output" >> $1.cpp
echo "void $1::output()\n{" >> $1.cpp
for i in "${@:2}"; do
if [ $((c%2)) -eq 0 ]
then
echo " std::cout << \"$i : \" << _$i << std::endl; " >> $1.cpp
fi
c=$((c+1))
done
echo "}" >> $1.cpp
echo " " >> $1.cpp