-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkheader
More file actions
executable file
·151 lines (131 loc) · 3.93 KB
/
mkheader
File metadata and controls
executable file
·151 lines (131 loc) · 3.93 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
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
# Use the current/default shell (got this idea from a script in 'copyparty' - https://github.com/9001/copyparty)
set -e
ESC="\x1b["
BOLD="1;"
NORM="22m"
RED="31m"
GREEN="32m"
YELLOW="33m"
RESET="\x1b[0m"
NAME="mkheader"
VERSION="1.2"
ERROR="$ESC$BOLD$RED[ERROR][$NAME]$ESC$RED"
WARN="$ESC$BOLD$YELLOW[WARNING][$NAME]$ESC$YELLOW"
MSG_GUARD="The header guard argument cannot be used with multiple header files!$RESET\n"
MSG_FILENAMES="Duplicate file names detected!$RESET\n"
HEADER_FILES=()
GUARD=""
FORCE=false
NOCONFIRM=false
CLOBBER=false
printHelp()
{
cat <<- EOF
Usage: $NAME [-hv] [-n|--noconfirm] [-g|--guard <header_guard>] [-f|--force] <filename(s)>
-h, --help print help document
-v, --version print script version
--force process duplicate filenames and warn the user instead of exiting
-n, --noconfirm with -g/--guard, skip confirmation
-g, --guard GUARD specify the name for the header guard (cannot be used with multiple files)
-c, --clobber overwrite existing files
Notes:
When generating multiple files, '$NAME' will error out if it encounters duplicate filenames. This can
be overridden with the '--force' flag, although it's not recommended.
Examples:
$NAME header_file.hpp another_header_file.h a_weird_header_file another_weird_header_file.headerfile
$NAME -ng PROJECT_FONTS_H fonts.hpp
$NAME v$VERSION
EOF
}
try_SetGuard()
{
if [[ ${#HEADER_FILES[@]} -ne 1 ]]; then
printf "$ERROR$MSG_GUARD"
return 1
fi
GUARD=$(printf "$GUARD" | tr -c '[:alnum:]' '_')
if ! $NOCONFIRM; then
printf "::Use \"$GUARD\" as the header guard? [Y/n] "
read -r ANSWER
[[ $ANSWER =~ ^[Nn] ]] && printf "\n::A default header guard will be used instead.\n"
fi
printf "::Header guard set to '$GUARD'\n"
return 0
}
check_DuplicateFilenames()
{
UNIQUE_NUMBER=$(printf '%s\n' "${HEADER_FILES[@]##*/}" | awk '!($0 in seen){seen[$0];c++} END {print c}')
if [[ $UNIQUE_NUMBER -ne ${#HEADER_FILES[@]} ]]; then
if $FORCE; then
printf "$WARN $MSG_FILENAMES"
else
printf "$ERROR $MSG_FILENAMES"
return 1
fi
fi
return 0
}
OPTIONS=$(getopt -l "help,version,noconfirm,force,guard:,clobber" -o "hvng:c" -a -- "$@")
eval set -- "$OPTIONS"
while true
do
case "$1" in
-h|--help)
printHelp
exit 0
;;
-v|--version)
printf "$NAME v$VERSION\n"
exit 0
;;
-n|--noconfirm)
NOCONFIRM=true
;;
--force)
FORCE=true
;;
-g|--guard)
shift
GUARD="$1"
;;
-c|--clobber)
CLOBBER=true
;;
--)
shift
break
;;
esac
shift
done
HEADER_FILES=($@)
[[ -n "$GUARD" ]] && try_SetGuard
if [[ ${#HEADER_FILES[@]} -lt 1 ]]; then
printHelp
exit 1
fi
if ! check_DuplicateFilenames; then
exit 1
fi
for FILE in ${HEADER_FILES[@]}; do
CLOBBERING_TIME=false
if [[ -f $FILE ]]; then
if [[ $CLOBBER == false ]]; then
printf "$ESC$BOLD$YELLOW::File $ESC$NORM$FILE$ESC$BOLD$YELLOW already exists; skipping\n$RESET"
continue
fi
CLOBBERING_TIME=true
fi
if [[ -z $GUARD ]]; then
EXTENSION="${FILE##*.}"
[[ -z $EXTENSION ]] && EXTENSION="H"
FILESTEM="${FILE##*/}"
FILEBASE="${FILESTEM%%.$EXTENSION}"
CAPITAL_EXTENSION=$(echo "$EXTENSION" | tr '[:lower:]' '[:upper:]')
[[ $CAPITAL_EXTENSION == "HPP" ]] && CAPITAL_EXTENSION="H"
CAPITAL_FILEBASE=$(echo "$FILEBASE" | tr '[:lower:]' '[:upper:]')
GUARD="$CAPITAL_FILEBASE"_"$CAPITAL_EXTENSION"
fi
printf "#ifndef $GUARD\n#define $GUARD\n\n#endif // $GUARD\n" > "$FILE"
[[ $CLOBBERING_TIME == true ]] && printf "::Overwrote Header File '$FILE'\n" || printf "::Created Header File '$FILE'\n"
done