Mon, 28 May 2018 10:42:25 +0800
Merge
1 #!/usr/bin/perl
3 #
4 # Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 #
7 # This code is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License version 2 only, as
9 # published by the Free Software Foundation.
10 #
11 # This code is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # version 2 for more details (a copy is included in the LICENSE file that
15 # accompanied this code).
16 #
17 # You should have received a copy of the GNU General Public License version
18 # 2 along with this work; if not, write to the Free Software Foundation,
19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 # or visit www.oracle.com if you need additional information or have any
23 # questions.
24 #
26 #
27 # Parses java files:
28 # 1. Removes from the end of lines spaces and TABs
29 # 2. Replaces TABs by spaces
30 # 3. Replaces all NewLine separators by Unix NewLine separators
31 # 4. Makes one and only one empty line at the end of each file
33 if ($#ARGV < 0) {
34 &usage;
36 die;
37 }
39 use Cwd 'abs_path';
41 my @extensions = ("java");
43 # Read options
44 my $dirpos = 0;
46 while ($dirpos < $#ARGV) {
47 if ($ARGV[$dirpos] eq "-e") {
48 @extensions = split(/,/, $ARGV[$dirpos + 1]);
49 } else {
50 last;
51 }
53 $dirpos += 2;
54 }
56 if ($dirpos > $#ARGV) {
57 &usage;
59 die;
60 }
62 use Cwd;
63 my $currdir = getcwd;
65 my $allfiles = 0;
67 my $filecount = 0;
69 my @tabvalues;
71 # Init tabvalues
72 push (@tabvalues, " ");
74 for (my $i = 1; $i < 8; $i++) {
75 push(@tabvalues, $tabvalues[$i - 1] . " ");
76 }
78 open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n";
80 while ($dirpos <= $#ARGV) {
81 use File::Find;
83 find(\&parse_file, abs_path($ARGV[$dirpos]));
85 $dirpos += 1;
86 }
88 close(FILELIST);
90 use Cwd 'chdir';
91 chdir $currdir;
93 print "Checked $allfiles file(s)\n";
94 print "Modified $filecount file(s)\n";
95 print "See results in the file $currdir/filelist\n";
97 sub parse_file {
98 my $filename = $File::Find::name;
100 # Skip directories
101 return if -d;
103 # Skip SCCS files
104 return if ($filename =~ /\/SCCS\//);
106 # Skip files with invalid extensions
107 my $accepted = 0;
108 foreach my $ext (@extensions) {
109 if ($_ =~ /\.$ext$/i) {
110 $accepted = 1;
112 last;
113 }
114 }
115 return if ($accepted == 0);
117 use File::Basename;
118 my $dirname = dirname($filename);
120 use Cwd 'chdir';
121 chdir $dirname;
123 open(FILE, $filename) or die "Failed while open $filename: $!\n";
125 # Read file
126 my @content;
127 my $line;
128 my $emptylinescount = 0;
129 my $modified = 0;
131 while ($line = <FILE>) {
132 my $originalline = $line;
134 # Process line
136 # Remove from the end of the line spaces and return character
137 while ($line =~ /\s$/) {
138 chop($line);
139 }
141 # Replace TABs
142 for (my $i = 0; $i < length($line); $i++) {
143 if (substr($line, $i, 1) =~ /\t/) {
144 $line = substr($line, 0, $i) . $tabvalues[7 - ($i % 8)] . substr($line, $i + 1);
145 }
146 }
148 if (length($line) == 0) {
149 $emptylinescount++;
150 } else {
151 while ($emptylinescount > 0) {
152 push(@content, "");
154 $emptylinescount--;
155 }
157 push(@content, $line);
158 }
160 if ($originalline ne ($line . "\n")) {
161 $modified = 1;
162 }
164 }
166 $allfiles++;
168 if ($emptylinescount > 0) {
169 $modified = 1;
170 }
172 close(FILE);
174 if ($modified != 0) {
175 # Write file
176 open(FILE, ">$filename") or die "Failed while open $filename: $!\n";
178 for (my $i = 0; $i <= $#content; $i++) {
179 print FILE "$content[$i]\n";
180 }
182 close(FILE);
184 # Print name from current dir
185 if (index($filename, $currdir) == 0) {
186 print FILELIST substr($filename, length($currdir) + 1);
187 } else {
188 print FILELIST $filename;
189 }
190 print FILELIST "\n";
192 $filecount++;
194 print "$filename: modified\n";
195 }
196 }
198 sub usage {
199 print "Usage:\n";
200 print " normalizer.pl [-options] <dir> [dir2 dir3 ...]\n";
201 print " Available options:\n";
202 print " -e comma separated files extensions. By default accepts only java files\n";
203 print "\n";
204 print "Examples:\n";
205 print " normalizer.pl -e c,cpp,h,hpp .\n";
206 }