1 # Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions 5 # are met: 6 # 7 # 1. Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # 2. Redistributions in binary form must reproduce the above copyright 10 # notice, this list of conditions and the following disclaimer in the 11 # documentation and/or other materials provided with the distribution. 12 # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 13 # its contributors may be used to endorse or promote products derived 14 # from this software without specific prior written permission. 15 # 16 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 # Used for helping remove extra blank lines from files when processing. 28 # see split-class for an example usage (or other scripts in bugzilla) 29 30 BEGIN { 31 use Exporter (); 32 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); 33 $VERSION = 1.00; 34 @ISA = qw(Exporter); 35 @EXPORT = qw(&resetSpacingHeuristics &isOnlyWhiteSpace &applySpacingHeuristicsAndPrint &setPreviousAllowedLine &setPreviousAllowedLine &printPendingEmptyLines &ignoringLine); 36 %EXPORT_TAGS = (); 37 @EXPORT_OK = (); 38 } 39 40 our @EXPORT_OK; 41 42 my $justFoundEmptyLine = 0; 43 my $previousLineWasDisallowed = 0; 44 my $previousAllowedLine = ""; 45 my $pendingEmptyLines = ""; 46 47 sub resetSpacingHeuristics 48 { 49 $justFoundEmptyLine = 0; 50 $previousLineWasDisallowed = 0; 51 $previousAllowedLine = ""; 52 $pendingEmptyLines = ""; 53 } 54 55 sub isOnlyWhiteSpace 56 { 57 my $line = shift; 58 my $isOnlyWhiteSpace = ($line =~ m/^\s+$/); 59 $pendingEmptyLines .= $line if ($isOnlyWhiteSpace); 60 return $isOnlyWhiteSpace; 61 } 62 63 sub applySpacingHeuristicsAndPrint 64 { 65 my ($out, $line) = @_; 66 67 printPendingEmptyLines($out, $line); 68 $previousLineWasDisallowed = 0; 69 print $out $line; 70 } 71 72 sub setPreviousAllowedLine 73 { 74 my $line = shift; 75 $previousAllowedLine = $line; 76 } 77 78 sub printPendingEmptyLines 79 { 80 my $out = shift; 81 my $line = shift; 82 if ($previousLineWasDisallowed) { 83 if (!($pendingEmptyLines eq "") && !($previousAllowedLine =~ m/{\s*$/) && !($line =~ m/^\s*}/)) { 84 $pendingEmptyLines = "\n"; 85 } else { 86 $pendingEmptyLines = ""; 87 } 88 } 89 print $out $pendingEmptyLines; 90 $pendingEmptyLines = ""; 91 } 92 93 sub ignoringLine 94 { 95 # my $line = shift; # ignoring input argument 96 $previousLineWasDisallowed = 1; 97 } 98 99 1; 100