File: cvs-dup-eol-fixer

package info (click to toggle)
libhibernate3-java 3.6.10.Final-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 35,272 kB
  • sloc: java: 351,526; xml: 24,424; sh: 85; perl: 65; makefile: 24; sql: 6
file content (119 lines) | stat: -rw-r--r-- 4,221 bytes parent folder | download | duplicates (8)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!perl -w
###############################################################################
#
# Name: cvs-dup-eol-fixer
# Author: Steve Ebersole
#
# Description:
# Script to fix the bad end-of-line issues that sometimes occur after checking
# out Hibernate source from the sourceforge CVS where everything is essentially
# double-spaced.  What I found however, at least on my environment, was that
# this was actually caused by two carriage-return characters (i.e., \r\r) being
# substituted for all line endings.  This script works under that assumption
# and fixes only that issue.
#
###############################################################################


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This subroutine is essentially a recursive directory searcher.  It does also
# filter out anything from the CVS local admin dirs.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub parsedir($) {
    my @results = ();
    my $dir = shift@_;
    opendir(DIRHANDLE, $dir) or die("Unable to open dir [$dir] for parsing");
    my @dir_contents = readdir(DIRHANDLE);
    closedir(DIRHANDLE) or warn("Unable to close dir [$dir]");

    foreach $element (@dir_contents) {
        if ( $element eq "." || $element eq ".." ) {
            # Nothing to do here...
        }
        elsif ($element =~ /CVS/) {
            # nothing to do here...
        }
        # assume no extension means a directory
        elsif ($element =~ /\./) {
            if ($element =~ /\.java/) {
                push( @results, "$dir/$element" );
            }
        }
        else {
            push( @results, parsedir("$dir/$element") );
        }
    }

    return @results;
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This subroutine basically checks to see if the file needs to be fixed, based
# mainly on the number of adjacent (i.e., repeating) cariage-return characters.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub checkfile($) {
    my $file_name = shift @_;
    my $loop_count = 0;
    my $adj_cr_count = 0;
    my $line_count = 0;

    open( INFILEHANDLE, "<$file_name" ) or die( "Unable to open file [$file_name] for check" );
    while (<INFILEHANDLE>) {
        $loop_count++;

        @matches = m/\r\r/g;
        $adj_cr_count = $adj_cr_count + $#matches + 1;

        $line_count = $line_count + tr/\r\n/\r\n/;
    }
    close( INFILEHANDLE );

    my $half_line_count = $line_count / 2;
    return $loop_count == 1 && int($half_line_count) <= $adj_cr_count && $adj_cr_count <= int($half_line_count + 1);
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This is the subroutine where the file actually gets fixed.  It is also
# responsible for making sure files get backed up before doing the fix.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub fixfile($) {
    my $file_name = shift @_;
    my $file_text = "";

    open( INFILEHANDLE, "<$file_name" ) or die( "Unable to open file [$file_name] for fix input" );
    while (<INFILEHANDLE>) {
        s/\r\r/\n/g;
        $file_text .= $_;
    }
    close( INFILEHANDLE );

    my $new_file_name = $file_name . ".old";
    rename( $file_name, $new_file_name );

    open( OUTFILEHANDLE, ">$file_name" ) or die( "Unable to open file [$file_name] for fix output" );
    print( OUTFILEHANDLE $file_text );
    close( OUTFILEHANDLE );
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Start main process
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
open( REPORTFILEHANDLE, ">cvs-dup-eol-fixer.report" ) or die( "Unable to open report file" );

my $basedir = shift @ARGV;
print( REPORTFILEHANDLE "Using basedir : $basedir\n");

my @file_list = parsedir($basedir);

foreach $file_name (@file_list) {
    print( REPORTFILEHANDLE "Checking file [$file_name]\n" );
    if ( checkfile($file_name) ) {
        print( REPORTFILEHANDLE "    Need to fix file : $file_name\n" );
        fixfile($file_name);
    }
}

close(REPORTFILEHANDLE) or warn("Unable to close report file");

__END__