-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathh2pair.sh
executable file
·88 lines (75 loc) · 2.15 KB
/
h2pair.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
#!/bin/bash
# http://tipstricks.itmatrix.eu/?p=305
tac () {
awk '1 { last = NR; line[last] = $0; } END { for (i = last; i > 0; i--) { print line[i]; } }'
}
function replace_inline
{
result="$1"
# replace inlines with IGL_INLINEs, first those that begin lines
result=`echo "$result" | sed -e 's/^inline/IGL_INLINE/g'`
# then also those that begin words
result=`echo "$result" | sed -e 's/ inline/ IGL_INLINE/g'`
echo "$result"
}
# Convert well organized .h file to a .h/.cpp pair
for i in $*
do
# Only operate on .h files
filename=$(basename $i)
extension=${filename##*.}
filename=${filename%.*}
FILENAME=`echo $filename | tr '[a-z]' '[A-Z]'`
if [ ! "$extension" = "h" ];
then
echo "Skipping $i because it is not a .h file"
continue;
fi
if ! grep -q "^\/\/ Implementation *$" "$i"
then
echo "Skipping $i because it does not match ^\/\/ Implementation *$ "
continue;
fi
if [[ `grep -c "^\#endif" "$i"` > 1 ]];
then
echo "Warning $i contains multple matches to ^#endif"
fi
before=`sed -n '/^\/\/ Implementation$/q;p' "$i"`;
if ! echo "$before" | grep -q "^\#ifndef IGL_${FILENAME}_H"
then
echo "Skipping $i because it does not match ^#ifndef IGL_${FILENAME}_H "
continue;
fi
if ! echo "$before" | grep -q "^\#define IGL_${FILENAME}_H"
then
echo "Skipping $i because it does not match ^#define IGL_${FILENAME}_H "
continue;
fi
before=`replace_inline "$before"`
# prepend #include "igl_inline.h"
before=`echo "$before" | sed -e 's/^\(#define IGL_'${FILENAME}'_H\)/\1\'$'\n''#include \"igl_inline.h\"'/`
after=`sed '1,/^\/\/ Implementation$/d' $i`;
after=`replace_inline "$after"`
# reverse file
after=`echo "$after" | tac`
# everything after first (last) endif
after=`echo "
$after" | sed '1,/endif/d'`;
# reverse file
after=`echo "$after" | tac`
# append empty template code
if grep -q "template" "$i"
then
after=`echo "$after
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
#endif"`
fi
echo "$before
#ifndef IGL_STATIC_LIBRARY
# include \"$filename.cpp\"
#endif
#endif"> "$filename".h
echo "#include \"$filename.h\"
$after"> "$filename".cpp
done