Skip to content

Commit e935769

Browse files
author
jef
committed
scripts for the astyle re-indentation & iostream=>QgsDebugMsg migration cleanup
git-svn-id: http://svn.osgeo.org/qgis/trunk@9137 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ab85a37 commit e935769

File tree

4 files changed

+325
-0
lines changed

4 files changed

+325
-0
lines changed

scripts/astyle-all.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
if ! astyle.sh >/dev/null 2>&1; then
4+
echo astyle.sh not found in path >&2
5+
exit 1
6+
fi
7+
8+
set -e
9+
10+
export elcr="$(tput el)$(tput cr)"
11+
12+
find src -type f -print | while read f; do
13+
case "$f" in
14+
*.cpp|*.h|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H)
15+
;;
16+
17+
*)
18+
continue
19+
;;
20+
esac
21+
22+
if [ -f "$f.astyle" ]; then
23+
# reformat backup
24+
cp "$f.astyle" "$f"
25+
touch -r "$f.astyle" "$f"
26+
else
27+
# make backup
28+
cp "$f" "$f.astyle"
29+
touch -r "$f" "$f.astyle"
30+
fi
31+
32+
echo -ne "Reformating $f$elcr"
33+
astyle.sh "$f"
34+
done
35+
36+
echo
37+
38+
# convert CRLF to LF
39+
find .. -type f \
40+
! -path "*/.svn/*" \
41+
! -path "*/win_build/*" \
42+
! -name "*.def" \
43+
! -name "*.rc" \
44+
! -name "*.png" \
45+
-exec file {} \; |
46+
grep CRLF |
47+
cut -d: -f1 |
48+
while read f; do
49+
echo -ne "Flipping $f$elcr"
50+
flip -ub "$f"
51+
done

scripts/astyle-rollback.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
find . \( -name "*.astyle" -o -name "*.iostream" \) -exec rm {} \;
6+
svn revert -R .
7+
svn update
8+
patch -p0 --dry-run <qgslogger-before.diff
9+
patch -p0 <qgslogger-before.diff
10+
mv qgslogger-before.diff qgslogger-before.diff.orig
11+
svn diff >qgslogger-before.diff
12+
diff -u qgslogger-before.diff.orig qgslogger-before.diff

scripts/astyle.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
if ! qgsloggermig.pl >/dev/null 2>&1; then
4+
echo qgsloggermig.pl not found in path >&2
5+
exit 1
6+
fi
7+
8+
set -e
9+
10+
export ARTISTIC_STYLE_OPTIONS="\
11+
--preserve-date \
12+
--indent-preprocessor \
13+
--brackets=break \
14+
--convert-tabs \
15+
--indent=spaces=2 \
16+
--indent-classes \
17+
--indent-labels \
18+
--indent-namespaces \
19+
--indent-switches \
20+
--one-line=keep-blocks \
21+
--one-line=keep-statements \
22+
--max-instatement-indent=40 \
23+
--min-conditional-indent=-1 \
24+
--suffix=none"
25+
26+
#--break-blocks \
27+
28+
export ARTISTIC_STYLE_OPTIONS="\
29+
$ARTISTIC_STYLE_OPTIONS \
30+
--pad=oper \
31+
--pad=paren-in \
32+
--unpad=paren"
33+
34+
for f in "$@"; do
35+
flip -ub "$f"
36+
qgsloggermig.pl "$f"
37+
astyle $ARTISTIC_STYLE_OPTIONS "$f"
38+
done

scripts/qgsloggermig.pl

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
#!/usr/bin/perl
2+
3+
# use QgsDebugMsg instead of iostream for debugging output
4+
5+
# EXAMPLE:
6+
#
7+
# #include <iostream>
8+
#
9+
# #ifdef QGISDEBUG
10+
# std::cout << "A " << a << " B " << b << " A " << a << std::endl;
11+
# // std::cout << "commented out" << std::endl;
12+
# #endif
13+
#
14+
# becomes
15+
#
16+
# #include "qgslogger.h"
17+
#
18+
# QgsDebugMsg(QString("A %1 B %2 A %1").arg(a).arg(b));
19+
# QgsDebugMsgLevel("commented out", 3);
20+
#
21+
#
22+
# append // OK to keep it as it is.
23+
24+
use strict;
25+
use warnings;
26+
27+
for my $file (@ARGV) {
28+
my $output;
29+
my $F;
30+
my @file;
31+
32+
open $F, $file;
33+
34+
my $externc = 0;
35+
my $ifdef = 0;
36+
my $loggerseen=0;
37+
my $lastinclude=0;
38+
my $modified = 0;
39+
my $i=0;
40+
my $le;
41+
while(<$F>) {
42+
$i++;
43+
44+
($le) = /([\r\n]+)$/ unless defined $le;
45+
46+
if(/\s*#\s*include\s*<iostream>/) {
47+
next;
48+
}
49+
50+
if(/\s*#\s*include\s*qgslogger\.h/) {
51+
$loggerseen=1;
52+
}
53+
54+
$externc=1 if /extern\s+\"C\"\s*{/;
55+
$externc=0 if $externc && /^\s*}\s*$/;
56+
$ifdef++ if /^\s*#\s*if/;
57+
$ifdef-- if /^\s*#\s*endif/;
58+
59+
if($externc==0 && $ifdef==0 && /\s*#\s*include/) {
60+
$lastinclude = scalar(@file)+1;
61+
}
62+
63+
if(/std::(cout|cerr)/) {
64+
die "nested? [$file]" if defined $output;
65+
$output = "";
66+
}
67+
68+
if(defined $output) {
69+
$output .= $_;
70+
if(/;/) {
71+
($le) = ($output =~ /([\r\n]+)$/);
72+
$output =~ s/$le/\n/g;
73+
74+
my $level = 0;
75+
if($output =~ /^\s*\/\/\s*(std::(cout|cerr))/) {
76+
$level = 3;
77+
$output =~ s/^\s*\/\///;
78+
$output =~ s/\n\s*\/\//\n /g;
79+
}
80+
81+
my @arr = split /\s*<<\s*/, $output;
82+
my ($indent) = ($arr[0] =~ /^(\s*)/);
83+
$arr[0] =~ s/^\s+//;
84+
85+
if($arr[0] =~ /^\/\// || $arr[-1] =~ /\/\/ OK$/) {
86+
# commented out
87+
push @file, "$output\n";
88+
undef $output;
89+
next;
90+
}
91+
92+
unless( $arr[0] =~ /^std::(cout|cerr)$/ ) {
93+
die "std::(cerr|cout) expected [$file]: |" . $arr[0] . "|";
94+
}
95+
96+
$arr[-1] =~ s/\s*;\s*$/;/;
97+
98+
if( $arr[-1] =~ /\\n";$/) {
99+
$arr[-1] =~ s/\\n";/"/;
100+
push @arr, "std::endl;";
101+
} elsif( $arr[-1] =~ /'\\n';$/) {
102+
$arr[-1] = "std::endl;";
103+
}
104+
105+
if( $arr[-1] =~ /^std::flush;$/ &&
106+
$arr[-2] =~ /^std::endl$/ ) {
107+
pop @arr;
108+
pop @arr;
109+
$arr[-1] = "std::endl;";
110+
}
111+
112+
unless( $arr[-1] =~ /^std::endl;$/ ) {
113+
die "std::endl; expected [$file]: |" . $arr[-1] . "|";
114+
}
115+
116+
shift @arr;
117+
pop @arr;
118+
119+
my $str;
120+
my %args;
121+
my @args;
122+
my $fmt = "";
123+
foreach(@arr) {
124+
if(/^"(.*)"$/) {
125+
$fmt .= $1;
126+
} else {
127+
if(/^QString::number\s*\(\s*([^,]*)\s*\)$/) {
128+
$_ = $1;
129+
}
130+
131+
s/\.toLocal8Bit\(\).data\(\)$//;
132+
s/\.toUtf8\(\).data\(\)$//;
133+
s/\.ascii\(\)$//;
134+
135+
if(exists $args{$_}) {
136+
my $n = $args{$_};
137+
$fmt .= "%$n";
138+
} else {
139+
push @args, $_;
140+
$args{$_} = scalar(@args);
141+
$fmt .= "%" . scalar(@args);
142+
}
143+
}
144+
}
145+
146+
if(@args>0) {
147+
if(@args==1 && $fmt eq "%1") {
148+
$str = $args[0];
149+
} else {
150+
$str = "QString(\"$fmt\").arg(" . join(").arg(", @args) . ")";
151+
}
152+
} else {
153+
$str = "\"$fmt\"";
154+
}
155+
156+
if($level == 3) {
157+
# push @file, $indent . "QgsDebugMsgLevel($str, 3);$le";
158+
push @file, $indent . "// QgsDebugMsg($str);$le";
159+
} else {
160+
push @file, $indent . "QgsDebugMsg($str);$le";
161+
}
162+
163+
$modified=1;
164+
165+
undef $output;
166+
}
167+
} else {
168+
push @file, $_;
169+
}
170+
}
171+
close $F;
172+
173+
174+
if($modified) {
175+
if(!$loggerseen) {
176+
die "no includes? [$file]" unless defined $lastinclude;
177+
splice @file, $lastinclude, 0, "#include \"qgslogger.h\"$le";
178+
}
179+
180+
#print "MODIFIED: $file\n";
181+
182+
my @filtered;
183+
my @output;
184+
my $ifdef_seen=0;
185+
186+
foreach(@file) {
187+
if($ifdef_seen) {
188+
if(/^\s*#\s*if/) {
189+
die "nested #if? [$file]";
190+
} elsif(/^\s*QgsDebugMsg/) {
191+
push @output, $_;
192+
} elsif(/^\s*#\s*endif/) {
193+
push @filtered, $_ foreach @output;
194+
undef @output;
195+
$ifdef_seen=0;
196+
} else {
197+
push @filtered, "#ifdef QGISDEBUG$le";
198+
push @filtered, $_ foreach @output;
199+
push @filtered, $_;
200+
undef @output;
201+
$ifdef_seen=0;
202+
}
203+
} elsif(/^\s*#\s*ifdef\s+QGISDEBUG\s*$/) {
204+
die "output pending" if @output;
205+
$ifdef_seen=1;
206+
} else {
207+
push @filtered, $_;
208+
}
209+
}
210+
211+
die "output pending" if @output;
212+
213+
link $file, "$file.iostream" unless -f "$file.iostream";
214+
unlink $file;
215+
216+
open $F, ">$file";
217+
foreach (@filtered) {
218+
print $F $_;
219+
}
220+
close $F;
221+
}
222+
}
223+
224+
# vim: set ts=8 noet:

0 commit comments

Comments
 (0)