parallel: Indention.

This commit is contained in:
Ole Tange 2013-08-21 17:11:25 +02:00
parent 81a8a116b7
commit 2c587e4055

View file

@ -343,88 +343,87 @@ sub spreadstdin {
my $one_time_through; my $one_time_through;
my $blocksize = $opt::blocksize; my $blocksize = $opt::blocksize;
my $in = *STDIN; my $in = *STDIN;
piperead: while(1) { while(1) {
my $anything_written = 0; my $anything_written = 0;
if(not read($in,substr($buf,length $buf,0),$blocksize)) { if(not read($in,substr($buf,length $buf,0),$blocksize)) {
# End-of-file # End-of-file
$chunk_number != 1 and last; $chunk_number != 1 and last;
# Force the while-loop once if everything was read by header reading # Force the while-loop once if everything was read by header reading
$one_time_through++ and last; $one_time_through++ and last;
} }
if($opt::r) {
if($opt::r) { # Remove empty lines
# Remove empty lines $buf=~s/^\s*\n//gm;
$buf=~s/^\s*\n//gm; if(length $buf == 0) {
if(length $buf == 0) { next;
next; }
} }
} if($Global::max_lines and not $Global::max_number_of_args) {
if($Global::max_lines and not $Global::max_number_of_args) { # Read n-line records
# Read n-line records my $n_lines = $buf=~tr/\n/\n/;
my $n_lines = $buf=~tr/\n/\n/; my $last_newline_pos = rindex($buf,"\n");
my $last_newline_pos = rindex($buf,"\n"); while($n_lines % $Global::max_lines) {
while($n_lines % $Global::max_lines) { $n_lines--;
$n_lines--; $last_newline_pos = rindex($buf,"\n",$last_newline_pos-1);
$last_newline_pos = rindex($buf,"\n",$last_newline_pos-1); }
} # Chop at $last_newline_pos as that is where n-line record ends
# Chop at $last_newline_pos as that is where n-line record ends $anything_written +=
$anything_written += write_record_to_pipe($chunk_number++,\$header,\$buf,
write_record_to_pipe($chunk_number++,\$header,\$buf, $recstart,$recend,$last_newline_pos+1);
$recstart,$recend,$last_newline_pos+1); substr($buf,0,$last_newline_pos+1) = "";
substr($buf,0,$last_newline_pos+1) = ""; } elsif($opt::regexp) {
} elsif($opt::regexp) { if($Global::max_number_of_args) {
if($Global::max_number_of_args) { # -N => (start..*?end){n}
# -N => (start..*?end){n} # -L -N => (start..*?end){n*l}
# -L -N => (start..*?end){n*l} my $read_n_lines = $Global::max_number_of_args * ($Global::max_lines || 1);
my $read_n_lines = $Global::max_number_of_args * ($Global::max_lines || 1); while($buf =~ s/((?:$recstart.*?$recend){$read_n_lines})($recstart.*)$/$2/os) {
while($buf =~ s/((?:$recstart.*?$recend){$read_n_lines})($recstart.*)$/$2/os) { # Copy to modifiable variable
# Copy to modifiable variable my $b = $1;
my $b = $1; $anything_written +=
$anything_written += write_record_to_pipe($chunk_number++,\$header,\$b,
write_record_to_pipe($chunk_number++,\$header,\$b, $recstart,$recend,length $1);
$recstart,$recend,length $1); }
} } else {
} else { # Find the last recend-recstart in $buf
# Find the last recend-recstart in $buf if($buf =~ s/(.*$recend)($recstart.*?)$/$2/os) {
if($buf =~ s/(.*$recend)($recstart.*?)$/$2/os) { # Copy to modifiable variable
# Copy to modifiable variable my $b = $1;
my $b = $1; $anything_written +=
$anything_written += write_record_to_pipe($chunk_number++,\$header,\$b,
write_record_to_pipe($chunk_number++,\$header,\$b, $recstart,$recend,length $1);
$recstart,$recend,length $1); }
} }
} } else {
} else { if($Global::max_number_of_args) {
if($Global::max_number_of_args) { # -N => (start..*?end){n}
# -N => (start..*?end){n} my $i = 0;
my $i = 0; my $read_n_lines = $Global::max_number_of_args * ($Global::max_lines || 1);
my $read_n_lines = $Global::max_number_of_args * ($Global::max_lines || 1); while(($i = nindex(\$buf,$recendrecstart,$read_n_lines)) != -1) {
while(($i = nindex(\$buf,$recendrecstart,$read_n_lines)) != -1) { $i += length $recend; # find the actual splitting location
$i += length $recend; # find the actual splitting location $anything_written +=
$anything_written += write_record_to_pipe($chunk_number++,\$header,\$buf,
write_record_to_pipe($chunk_number++,\$header,\$buf, $recstart,$recend,$i);
$recstart,$recend,$i); substr($buf,0,$i) = "";
substr($buf,0,$i) = ""; }
} } else {
} else { # Find the last recend-recstart in $buf
# Find the last recend-recstart in $buf my $i = rindex($buf,$recendrecstart);
my $i = rindex($buf,$recendrecstart); if($i != -1) {
if($i != -1) { $i += length $recend; # find the actual splitting location
$i += length $recend; # find the actual splitting location $anything_written +=
$anything_written += write_record_to_pipe($chunk_number++,\$header,\$buf,
write_record_to_pipe($chunk_number++,\$header,\$buf, $recstart,$recend,$i);
$recstart,$recend,$i); substr($buf,0,$i) = "";
substr($buf,0,$i) = ""; }
} }
} }
} if(not $anything_written and not eof($in)) {
if(not $anything_written and not eof($in)) { # Nothing was written - maybe the block size < record size?
# Nothing was written - maybe the block size < record size? # Increase blocksize exponentially
# Increase blocksize exponentially $blocksize = ceil($blocksize * 1.3 + 1);
$blocksize = ceil($blocksize * 1.3 + 1); ::warning("A full record was not matched in a block. Increasing to --blocksize ".$blocksize."\n");
::warning("A full record was not matched in a block. Increasing to --blocksize ".$blocksize."\n"); }
} }
}
# If there is anything left in the buffer write it # If there is anything left in the buffer write it
substr($buf,0,0) = ""; substr($buf,0,0) = "";
write_record_to_pipe($chunk_number++,\$header,\$buf,$recstart,$recend,length $buf); write_record_to_pipe($chunk_number++,\$header,\$buf,$recstart,$recend,length $buf);
@ -4733,15 +4732,6 @@ sub populate {
# max line length is reached # max line length is reached
# Returns: N/A # Returns: N/A
my $self = shift; my $self = shift;
# if($opt::pipe) {
# # --pipe => Do no read any args
# $self->push([Arg->new("")]);
# return;
# }
# if($opt::roundrobin) {
# $self->push([Arg->new("")]);
# return;
# }
my $next_arg; my $next_arg;
while (not $self->{'arg_queue'}->empty()) { while (not $self->{'arg_queue'}->empty()) {
$next_arg = $self->{'arg_queue'}->get(); $next_arg = $self->{'arg_queue'}->get();
@ -5102,9 +5092,6 @@ sub replace_placeholders {
my $quote = shift; my $quote = shift;
my $context_replace = $self->{'context_replace'}; my $context_replace = $self->{'context_replace'};
my $replaced; my $replaced;
# print ::my_dump($self);
if($self->{'context_replace'}) { if($self->{'context_replace'}) {
$replaced = $self->context_replace_placeholders($target,$quote); $replaced = $self->context_replace_placeholders($target,$quote);
} else { } else {
@ -5158,7 +5145,7 @@ sub context_replace_placeholders {
$word{$1} ||= 1; $word{$1} ||= 1;
} }
if(not %word) { if(not %word) {
# The line did no contain any replacementstrings => return unchanged # The line did no contain any replacement strings => return unchanged
return $target; return $target;
} }
# For each word: Generate the replacement string for that word. # For each word: Generate the replacement string for that word.