Skip to content
Snippets Groups Projects
runoff1 2.26 KiB
#!/usr/bin/perl

$n = 0;
$v = 0;
if($ARGV[0] eq "-v") {
	$v = 1;
	shift @ARGV;
}
if($ARGV[0] eq "-n") {
	$n = $ARGV[1];
	shift @ARGV;
	shift @ARGV;
}
$n = int(($n+49)/50)*50 - 1;

$file = $ARGV[0];
@lines = <>;
$linenum = 0;
foreach (@lines) {
	$linenum++;
	chomp;
	s/\s+$//;
	if(length() >= 75){
		print STDERR "$file:$linenum: line too long\n";
	}
}
@outlines = ();
$nextout = 0;

for($i=0; $i<@lines; ){
	# Skip leading blank lines.
	$i++ while $i<@lines && $lines[$i] =~ /^$/;
	last if $i>=@lines;

	# If the rest of the file fits, use the whole thing.
	if(@lines <= $i+50 && !grep { /PAGEBREAK/ } @lines){
		$breakbefore = @lines;
	}else{
		# Find a good next page break;
		# Hope for end of function.
		# but settle for a blank line (but not first blank line
		# in function, which comes after variable declarations).
		$breakbefore = $i;
		$lastblank = $i;
		$sawbrace = 0;
		$breaksize = 15;  # 15 lines to get to function
		for($j=$i; $j<$i+50 && $j < @lines; $j++){
			if($lines[$j] =~ /PAGEBREAK!/){
				$lines[$j] = "";
				$breakbefore = $j;
				$breaksize = 100;
				last;
			}
			if($lines[$j] =~ /PAGEBREAK:\s*([0-9]+)/){
				$breaksize = $1;
				$breakbefore = $j;
				$lines[$j] = "";
			}
			if($lines[$j] =~ /^};?$/){
				$breakbefore = $j+1;
				$breaksize = 15;
			}
			if($lines[$j] =~ /^{$/){
				$sawbrace = 1;
			}
			if($lines[$j] =~ /^$/){
				if($sawbrace){
					$sawbrace = 0;
				}else{
					$lastblank = $j;