home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Line.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-21  |  12.9 KB  |  405 lines

  1. # $Id: Line.pm,v 1.24 2003/11/21 05:08:25 rcaputo Exp $
  2.  
  3. package POE::Filter::Line;
  4.  
  5. use strict;
  6.  
  7. use vars qw($VERSION);
  8. $VERSION = do {my@r=(q$Revision: 1.24 $=~/\d+/g);sprintf"%d."."%04d"x$#r,@r};
  9.  
  10. use Carp qw(carp croak);
  11.  
  12. sub DEBUG () { 0 }
  13.  
  14. sub FRAMING_BUFFER   () { 0 }
  15. sub INPUT_REGEXP     () { 1 }
  16. sub OUTPUT_LITERAL   () { 2 }
  17. sub AUTODETECT_STATE () { 3 }
  18.  
  19. sub AUTO_STATE_DONE   () { 0x00 }
  20. sub AUTO_STATE_FIRST  () { 0x01 }
  21. sub AUTO_STATE_SECOND () { 0x02 }
  22.  
  23. #------------------------------------------------------------------------------
  24.  
  25. sub new {
  26.   my $type = shift;
  27.  
  28.   croak "$type requires an even number of parameters" if @_ and @_ & 1;
  29.   my %params = @_;
  30.  
  31.   croak "$type cannot have both Regexp and Literal line endings"
  32.     if defined $params{Regexp} and defined $params{Literal};
  33.  
  34.   my ($input_regexp, $output_literal);
  35.   my $autodetect = AUTO_STATE_DONE;
  36.  
  37.   # Literal newline for both incoming and outgoing.  Every other known
  38.   # parameter conflicts with this one.
  39.   if (defined $params{Literal}) {
  40.     croak "A defined Literal must have a nonzero length"
  41.       unless defined($params{Literal}) and length($params{Literal});
  42.     $input_regexp   = quotemeta $params{Literal};
  43.     $output_literal = $params{Literal};
  44.     croak "$type cannot have Literal with any other parameter"
  45.       if ( exists $params{InputLiteral} or # undef means something
  46.            defined $params{InputRegexp} or
  47.            defined $params{OutputLiteral}
  48.          );
  49.   }
  50.  
  51.   # Input and output are specified separately, then.
  52.   else {
  53.  
  54.     # Input can be either a literal or a regexp.  The regexp may be
  55.     # compiled or not; we don't rightly care at this point.
  56.     if (exists $params{InputLiteral}) {
  57.       $input_regexp = $params{InputLiteral};
  58.  
  59.       # InputLiteral is defined.  Turn it into a regexp and be done.
  60.       # Otherwise we will autodetect it.
  61.       if (defined($input_regexp) and length($input_regexp)) {
  62.         $input_regexp = quotemeta $input_regexp;
  63.       }
  64.       else {
  65.         $autodetect   = AUTO_STATE_FIRST;
  66.         $input_regexp = '';
  67.       }
  68.  
  69.       croak "$type cannot have both InputLiteral and InputRegexp"
  70.         if defined $params{InputRegexp};
  71.     }
  72.     elsif (defined $params{InputRegexp}) {
  73.       $input_regexp = $params{InputRegexp};
  74.       croak "$type cannot have both InputLiteral and InputRegexp"
  75.         if defined $params{InputLiteral};
  76.     }
  77.     else {
  78.       $input_regexp = "(\\x0D\\x0A?|\\x0A\\x0D?)";
  79.     }
  80.  
  81.     if (defined $params{OutputLiteral}) {
  82.       $output_literal = $params{OutputLiteral};
  83.     }
  84.     else {
  85.       $output_literal = "\x0D\x0A";
  86.     }
  87.   }
  88.  
  89.   delete @params{qw(Literal InputLiteral OutputLiteral InputRegexp)};
  90.   carp("$type ignores unknown parameters: ", join(', ', sort keys %params))
  91.     if scalar keys %params;
  92.  
  93.   my $self =
  94.     bless [ '',              # FRAMING_BUFFER
  95.             $input_regexp,   # INPUT_REGEXP
  96.             $output_literal, # OUTPUT_LITERAL
  97.             $autodetect,     # AUTODETECT_STATE
  98.           ], $type;
  99.  
  100.   DEBUG and warn join ':', @$self;
  101.  
  102.   $self;
  103. }
  104.  
  105. #------------------------------------------------------------------------------
  106.  
  107. sub get {
  108.   my ($self, $stream) = @_;
  109.   my @lines;
  110.  
  111.   DEBUG and do {
  112.     my $temp = join '', @$stream;
  113.     $temp = unpack 'H*', $temp;
  114.     warn "got some raw data: $temp\n";
  115.   };
  116.  
  117.   $self->[FRAMING_BUFFER] .= join '', @$stream;
  118.  
  119.   # Process as many newlines an we can find.
  120. LINE:
  121.   while (1) {
  122.  
  123.     # Autodetect is done, or it never started.  Parse some buffer!
  124.     unless ($self->[AUTODETECT_STATE]) {
  125.       DEBUG and warn unpack 'H*', $self->[INPUT_REGEXP];
  126.       last LINE
  127.         unless $self->[FRAMING_BUFFER] =~ s/^(.*?)$self->[INPUT_REGEXP]//s;
  128.       DEBUG and warn "got line: <<", unpack('H*', $1), ">>\n";
  129.       push @lines, $1;
  130.       next LINE;
  131.     }
  132.  
  133.     # Waiting for the first line ending.  Look for a generic newline.
  134.     if ($self->[AUTODETECT_STATE] & AUTO_STATE_FIRST) {
  135.       last LINE
  136.         unless $self->[FRAMING_BUFFER] =~ s/^(.*?)(\x0D\x0A?|\x0A\x0D?)//;
  137.       push @lines, $1;
  138.  
  139.       # The newline can be complete under two conditions.  First: If
  140.       # it's two characters.  Second: If there's more data in the
  141.       # framing buffer.  Loop around in case there are more lines.
  142.       if ( (length($2) == 2) or
  143.            (length $self->[FRAMING_BUFFER])
  144.          ) {
  145.         DEBUG and warn "detected complete newline after line: <<$1>>\n";
  146.         $self->[INPUT_REGEXP] = $2;
  147.         $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
  148.         next LINE;
  149.       }
  150.  
  151.       # The regexp has matched a potential partial newline.  Save it,
  152.       # and move to the next state.  There is no more data in the
  153.       # framing buffer, so we're done.
  154.       DEBUG and warn "detected suspicious newline after line: <<$1>>\n";
  155.       $self->[INPUT_REGEXP] = $2;
  156.       $self->[AUTODETECT_STATE] = AUTO_STATE_SECOND;
  157.       last LINE;
  158.     }
  159.  
  160.     # Waiting for the second line beginning.  Bail out if we don't
  161.     # have anything in the framing buffer.
  162.     if ($self->[AUTODETECT_STATE] & AUTO_STATE_SECOND) {
  163.       last LINE unless length $self->[FRAMING_BUFFER];
  164.  
  165.       # Test the first character to see if it completes the previous
  166.       # potentially partial newline.
  167.       if ( substr($self->[FRAMING_BUFFER], 0, 1) eq
  168.            ( $self->[INPUT_REGEXP] eq "\x0D" ? "\x0A" : "\x0D" )
  169.          ) {
  170.  
  171.         # Combine the first character with the previous newline, and
  172.         # discard the newline from the buffer.  This is two statements
  173.         # for backward compatibility.
  174.         DEBUG and warn "completed newline after line: <<$1>>\n";
  175.         $self->[INPUT_REGEXP] .= substr($self->[FRAMING_BUFFER], 0, 1);
  176.         substr($self->[FRAMING_BUFFER], 0, 1) = '';
  177.       }
  178.       elsif (DEBUG) {
  179.         warn "decided prior suspicious newline is okay\n";
  180.       }
  181.  
  182.       # Regardless, whatever is in INPUT_REGEXP is now a complete
  183.       # newline.  End autodetection, post-process the found newline,
  184.       # and loop to see if there are other lines in the buffer.
  185.       $self->[INPUT_REGEXP] = $self->[INPUT_REGEXP];
  186.       $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
  187.       next LINE;
  188.     }
  189.  
  190.     die "consistency error: AUTODETECT_STATE = $self->[AUTODETECT_STATE]";
  191.   }
  192.  
  193.   \@lines;
  194. }
  195.  
  196. #------------------------------------------------------------------------------
  197. # 2001-07-27 RCC: Add get_one_start() and get_one() to correct filter
  198. # changing and make input flow control possible.
  199.  
  200. sub get_one_start {
  201.   my ($self, $stream) = @_;
  202.  
  203.   DEBUG and do {
  204.     my $temp = join '', @$stream;
  205.     $temp = unpack 'H*', $temp;
  206.     warn "got some raw data: $temp\n";
  207.   };
  208.  
  209.   $self->[FRAMING_BUFFER] .= join '', @$stream;
  210. }
  211.  
  212. # -><- There is a lot of code duplicated here.  What can be done?
  213.  
  214. sub get_one {
  215.   my $self = shift;
  216.  
  217.   # Process as many newlines an we can find.
  218. LINE:
  219.   while (1) {
  220.  
  221.     # Autodetect is done, or it never started.  Parse some buffer!
  222.     unless ($self->[AUTODETECT_STATE]) {
  223.       DEBUG and warn unpack 'H*', $self->[INPUT_REGEXP];
  224.       last LINE
  225.         unless $self->[FRAMING_BUFFER] =~ s/^(.*?)$self->[INPUT_REGEXP]//s;
  226.       DEBUG and warn "got line: <<", unpack('H*', $1), ">>\n";
  227.  
  228.       return [ $1 ];
  229.     }
  230.  
  231.     # Waiting for the first line ending.  Look for a generic newline.
  232.     if ($self->[AUTODETECT_STATE] & AUTO_STATE_FIRST) {
  233.       last LINE
  234.         unless $self->[FRAMING_BUFFER] =~ s/^(.*?)(\x0D\x0A?|\x0A\x0D?)//;
  235.  
  236.       my $line = $1;
  237.  
  238.       # The newline can be complete under two conditions.  First: If
  239.       # it's two characters.  Second: If there's more data in the
  240.       # framing buffer.  Loop around in case there are more lines.
  241.       if ( (length($2) == 2) or
  242.            (length $self->[FRAMING_BUFFER])
  243.          ) {
  244.         DEBUG and warn "detected complete newline after line: <<$1>>\n";
  245.         $self->[INPUT_REGEXP] = $2;
  246.         $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
  247.       }
  248.  
  249.       # The regexp has matched a potential partial newline.  Save it,
  250.       # and move to the next state.  There is no more data in the
  251.       # framing buffer, so we're done.
  252.       else {
  253.         DEBUG and warn "detected suspicious newline after line: <<$1>>\n";
  254.         $self->[INPUT_REGEXP] = $2;
  255.         $self->[AUTODETECT_STATE] = AUTO_STATE_SECOND;
  256.       }
  257.  
  258.       return [ $line ];
  259.     }
  260.  
  261.     # Waiting for the second line beginning.  Bail out if we don't
  262.     # have anything in the framing buffer.
  263.     if ($self->[AUTODETECT_STATE] & AUTO_STATE_SECOND) {
  264.       return [ ] unless length $self->[FRAMING_BUFFER];
  265.  
  266.       # Test the first character to see if it completes the previous
  267.       # potentially partial newline.
  268.       if ( substr($self->[FRAMING_BUFFER], 0, 1) eq
  269.            ( $self->[INPUT_REGEXP] eq "\x0D" ? "\x0A" : "\x0D" )
  270.          ) {
  271.  
  272.         # Combine the first character with the previous newline, and
  273.         # discard the newline from the buffer.  This is two statements
  274.         # for backward compatibility.
  275.         DEBUG and warn "completed newline after line: <<$1>>\n";
  276.         $self->[INPUT_REGEXP] .= substr($self->[FRAMING_BUFFER], 0, 1);
  277.         substr($self->[FRAMING_BUFFER], 0, 1) = '';
  278.       }
  279.       elsif (DEBUG) {
  280.         warn "decided prior suspicious newline is okay\n";
  281.       }
  282.  
  283.       # Regardless, whatever is in INPUT_REGEXP is now a complete
  284.       # newline.  End autodetection, post-process the found newline,
  285.       # and loop to see if there are other lines in the buffer.
  286.       $self->[INPUT_REGEXP] = $self->[INPUT_REGEXP];
  287.       $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
  288.       next LINE;
  289.     }
  290.  
  291.     die "consistency error: AUTODETECT_STATE = $self->[AUTODETECT_STATE]";
  292.   }
  293.  
  294.   return [ ];
  295. }
  296.  
  297. #------------------------------------------------------------------------------
  298. # New behavior.  First translate system newlines ("\n") into whichever
  299. # newlines are supposed to be sent.  Second, add a trailing newline if
  300. # one doesn't already exist.  Since the referenced output list is
  301. # supposed to contain one line per element, we also do a split and
  302. # join.  Bleah. ... why isn't the code doing what the comment says?
  303.  
  304. sub put {
  305.   my ($self, $lines) = @_;
  306.  
  307.   my @raw;
  308.   foreach (@$lines) {
  309.     push @raw, $_ . $self->[OUTPUT_LITERAL];
  310.   }
  311.  
  312.   \@raw;
  313. }
  314.  
  315. #------------------------------------------------------------------------------
  316.  
  317. sub get_pending {
  318.   my $self = shift;
  319.   return [ $self->[FRAMING_BUFFER] ] if length $self->[FRAMING_BUFFER];
  320.   return undef;
  321. }
  322.  
  323. ###############################################################################
  324. 1;
  325.  
  326. __END__
  327.  
  328. =head1 NAME
  329.  
  330. POE::Filter::Line - filter data as lines
  331.  
  332. =head1 SYNOPSIS
  333.  
  334.   $filter = POE::Filter::Line->new();
  335.   $arrayref_of_lines =
  336.     $filter->get($arrayref_of_raw_chunks_from_driver);
  337.   $arrayref_of_streamable_chunks_for_driver =
  338.     $filter->put($arrayref_of_lines);
  339.   $arrayref_of_leftovers =
  340.     $filter->get_pending();
  341.  
  342.   # Use a literal newline terminator for input and output:
  343.   $filter = POE::Filter::Line->new( Literal => "\x0D\x0A" );
  344.  
  345.   # Terminate input lines with a string regexp:
  346.   $filter = POE::Filter::Line->new( InputRegexp   => '[!:]',
  347.                                     OutputLiteral => "!"
  348.                                   );
  349.  
  350.   # Terminate input lines with a compiled regexp (requires perl 5.005
  351.   # or newer):
  352.   $filter = POE::Filter::Line->new( InputRegexp   => qr/[!:]/,
  353.                                     OutputLiteral => "!"
  354.                                   );
  355.  
  356.   # Autodetect the input line terminator:
  357.   $filter = POE::Filter::Line->new( InputLiteral => undef );
  358.  
  359. =head1 DESCRIPTION
  360.  
  361. The Line filter translates streams to and from separated lines.  The
  362. lines it returns do not include the line separator (usually newlines).
  363. Neither should the lines given to it.
  364.  
  365. Incoming newlines are recognized with a simple regular expression by
  366. default: C</(\x0D\x0A?|\x0A\x0D?)/>.  This regexp encompasses all the
  367. variations of CR and/or LF, but it has a race condition.
  368.  
  369. Consider a CRLF newline is broken into two stream chunks, one which
  370. ends with CR and the other which begins with LF:
  371.  
  372.    some stream dataCR
  373.    LFother stream data
  374.  
  375. The default regexp will recognize the CR as one end-of-line marker and
  376. the LF as another.  The line filter will emit two lines: "some stream
  377. data" and a blank line.  B<People are advised to specify custom
  378. literal newlines or autodetect the newline style in applications where
  379. blank lines are significant.>
  380.  
  381. Outgoing lines have traditional network newlines (CRLF) appended to
  382. them by default.
  383.  
  384. =head1 PUBLIC FILTER METHODS
  385.  
  386. Please see POE::Filter.
  387.  
  388. =head1 SEE ALSO
  389.  
  390. POE::Filter.
  391.  
  392. The SEE ALSO section in L<POE> contains a table of contents covering
  393. the entire POE distribution.
  394.  
  395. =head1 BUGS
  396.  
  397. The default input newline regexp has a race condition where incomplete
  398. newlines can generate spurious blank input lines.
  399.  
  400. =head1 AUTHORS & COPYRIGHTS
  401.  
  402. Please see L<POE> for more information about authors and contributors.
  403.  
  404. =cut
  405.