-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasscreator.sh
executable file
·155 lines (133 loc) · 3.6 KB
/
classcreator.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
#!/usr/bin/env sh
# shellcheck disable=SC3043 # local is present in a wide (but not all) number of shells
# so it is almost POSIX to use it but not strictly POSIX
usage(){
cat <<USAGE
Usage: $name classname [dir] [header_dir class_dir]
Examples:
-$name classname -> creates a classname.cpp and classname.h class file in the current directory
-$name classname dir -> creates a dir/classname.cpp and dir/classname.h
class file in the specified directory
-$name classname header_dir class_dir -> creates a header_dir/classname.h and a
class_dir/classname.cpp file in the specified directories for header files and source files
USAGE
}
:<<-"GETNAME"
Function that gets the name of the script that should be used for the printing messages
GETNAME
getname(){
if echo "$0" | grep -E "^/proc/self/fd" 1>/dev/null;then name="classcreator"
else name="$(basename -s .sh "$0")"; fi
}
:<<-"SHOULD_DELETE"
Function that checks whever or not a user wants to delete an already existing file
This function exits the script if the user doesn't want to delete the file
SHOULD_DELETE
should_delete(){
read -p "File $1 already exists. Proceed to deletion ?[y/n]
$> " user_answer
if [ "$user_answer" == 'y' ] ; then return 0 ; fi
echo "Not proceeding with script execution"
kill -INT $$
}
:<<-CREATE_FILE
Function that creates a file with the given filename as an input. If no file is provided, do nothing
If the file can't be created, return an error
CREATE_FILE
create_file(){
if [ "$1" = "" ] ; then return 1; fi
if [ -f "$1" ] && should_delete "$1" ; then shred -f -n 10 -u -z "$1" ; fi
if [ ! -d "$(dirname "$1")" ] ; then mkdir -p "$(dirname "$1")" ; fi
if ! touch "$1" ; then echo "Couldn't create file : $1"; return 1 ; fi
return 0;
}
:<<-"ADD_SLASH_DIR"
Function that adds a trailing '/' if needed to a directory name
ADD_SLASH_DIR
add_slash_dir(){
local tmp
tmp="$(echo "$dir" | rev)"
if [ "$(echo "$tmp" | cut -c 1-1)" = "/" ] ; then return 0 ; fi
dir="${dir}/"
return 0
}
:<<-"GETPATHS"
Function that gets the paths to the class header file and to the class source
According to the arguments that were provided to the script
GETPATHS
getpaths(){
local dir
if [ "$2" = "" ] ; then class_header="$1".h ; class_source="$1".cpp; return 0; fi
dir="$2"
add_slash_dir
if [ "$3" = "" ] ; then class_header="${dir}${1}.h" ; class_source="${dir}${1}.cpp"; return 0; fi
class_header="${dir}${1}.h"
dir="$3"
add_slash_dir
class_source="${dir}${1}.cpp"
}
:<<-"CREATE_HEADER"
CREATE_HEADER
create_header(){
local header
local upp
if [ "$2" = "" ] ; then return 1 ; fi
class="$1"
header="$2"
upp="$(echo "$class" | tr '[:lower:]' '[:upper:]')"
if ! create_file "$header" ; then return 1 ; fi
cat >"$header" <<HEADER
#ifndef __${upp}_H__
# define __${upp}_H__
class $class{
public:
$class();
$class( const $class & );
$class& operator=( const $class & );
virtual ~$class();
};
#endif
HEADER
return 0
}
:<<-"CREATE_CLASS"
CREATE_CLASS
create_class(){
local class
local classfile
if [ "$2" = "" ] ; then return 1 ; fi
class="$1"
classfile="$2"
if ! create_file "$classfile" ; then return 1 ; fi
cat >"$classfile" <<CLASSFILE
#include "${class}.h"
$class::$class(){
}
$class::$class( const $class & other ){
(void)other;
}
$class& $class::operator=( const $class & other ){
(void)other;
return (*this);
}
$class::~$class(){
}
CLASSFILE
return 0
}
main(){
local name
local class_source
local class_header
getname
if [ "$1" = "" ]
then
usage
return 0
fi
getpaths "$@"
if ! create_header "$1" "$class_header" ; then return 1; fi
if ! create_class "$1" "$class_source" ; then return 1; fi
return 0;
}
main "$@"