-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpp_clean.sh
executable file
·62 lines (53 loc) · 1.12 KB
/
cpp_clean.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
#!/bin/bash
TMPNAME=.temp.for.sed.$$
get_mode_num()
{
local n=0
if [ ${1:0:1} != '-' ]; then
n=$((n + 4))
fi
if [ ${1:1:1} != '-' ]; then
n=$((n + 2))
fi
if [ ${1:2:1} != '-' ]; then
n=$((n + 1))
fi
echo -n "$n"
}
get_file_mode()
{
local mode=`ls -ls $1 | awk '{ print $2 }'`
echo -n "0$(get_mode_num ${mode:1:3})$(get_mode_num ${mode:4:6})$(get_mode_num ${mode:7:9})"
}
delete_trailing_space()
{
flist=$@
for file in $flist
do
if [ -f $file ];
then
if ! grep '[[:space:]]\+$' $file > /dev/null 2>&1
then
continue
fi
oldmode=$(get_file_mode $file)
sed 's/[[:space:]]\+$//' $file > $file$TMPNAME
if [ $? = 0 ];
then
mv $file$TMPNAME $file
chmod $oldmode $file
echo "delete trailing whitespace for $file"
else
rm -f $file$TMPNAME
fi
fi
done
}
if [ $# -gt 0 ];
then
delete_trailing_space $@
exit 0
fi
find \( -name "*.cpp" -o -name "*.hpp" -o -name "*.[hc]" \) -exec $0 {} \;
line_count=`find \( -name "*.cpp" -o -name "*.hpp" -o -name "*.[hc]" \) -exec cat {} \; | awk 'BEGIN{c} {if (length>0) c+=1;} END{print c}'`
echo "total line count of source file: $line_count"