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.
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.
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).
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.
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
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
41
my @extensions = ("java");
46
while ($dirpos < $#ARGV) {
47
if ($ARGV[$dirpos] eq "-e") {
48
@extensions = split(/,/, $ARGV[$dirpos + 1]);
56
if ($dirpos > $#ARGV) {
72
push (@tabvalues, " ");
74
for (my $i = 1; $i < 8; $i++) {
75
push(@tabvalues, $tabvalues[$i - 1] . " ");
78
open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n";
80
while ($dirpos <= $#ARGV) {
83
find(\&parse_file, abs_path($ARGV[$dirpos]));
93
print "Checked $allfiles file(s)\n";
94
print "Modified $filecount file(s)\n";
95
print "See results in the file $currdir/filelist\n";
98
my $filename = $File::Find::name;
104
return if ($filename =~ /\/SCCS\//);
106
# Skip files with invalid extensions
108
foreach my $ext (@extensions) {
109
if ($_ =~ /\.$ext$/i) {
115
return if ($accepted == 0);
118
my $dirname = dirname($filename);
123
open(FILE, $filename) or die "Failed while open $filename: $!\n";
128
my $emptylinescount = 0;
131
while ($line = <FILE>) {
132
my $originalline = $line;
136
# Remove from the end of the line spaces and return character
137
while ($line =~ /\s$/) {
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);
148
if (length($line) == 0) {
151
while ($emptylinescount > 0) {
157
push(@content, $line);
160
if ($originalline ne ($line . "\n")) {
168
if ($emptylinescount > 0) {
174
if ($modified != 0) {
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";
184
# Print name from current dir
185
if (index($filename, $currdir) == 0) {
186
print FILELIST substr($filename, length($currdir) + 1);
188
print FILELIST $filename;
194
print "$filename: modified\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";
205
print " normalizer.pl -e c,cpp,h,hpp .\n";