| 1 | package Cikl::Smrt::Parsers::ParseTxt; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Mouse; | ||
| 7 | use Cikl::Smrt::Parser; | ||
| 8 | extends 'Cikl::Smrt::Parser'; | ||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | use constant NAME => 'txt'; | ||
| 12 | sub name { return NAME; } | ||
| 13 | |||
| 14 | has 'regex' => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => 'Str', | ||
| 17 | required => 1 | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'regex_values' => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Str', | ||
| 23 | required => 1 | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub parse { | ||
| 27 | my $self = shift; | ||
| 28 | my $fh = shift; | ||
| 29 | my $broker = shift; | ||
| 30 | my $re = $self->regex; | ||
| 31 | $re = qr/$re/; | ||
| 32 | |||
| 33 | my @cols = split(/\s*,\s*/, $self->regex_values); | ||
| 34 | |||
| 35 | # This is more memory efficient. | ||
| 36 | while(!$fh->eof()) { | ||
| 37 | my $line = $fh->getline(); | ||
| 38 | next if($line =~ /^(#|<|$)/); | ||
| 39 | # Strip \n\r off the end of the line. | ||
| 40 | $line =~ s/[\r\n]+$//; | ||
| 41 | |||
| 42 | my @m = ($line =~ $re); | ||
| 43 | next unless(@m); | ||
| 44 | my $h = {}; | ||
| 45 | foreach (0 ... $#cols){ | ||
| 46 | $m[$_] = '' unless($m[$_]); | ||
| 47 | for($m[$_]){ | ||
| 48 | s/^\s+//; | ||
| 49 | s/\s+$//; | ||
| 50 | } | ||
| 51 | $h->{$cols[$_]} = $m[$_]; | ||
| 52 | } | ||
| 53 | # a work-around, we do some of this in iodef::pb::simple too | ||
| 54 | # adding this here makes the debugging messages a little less complicated | ||
| 55 | if($h->{'address_mask'}){ | ||
| 56 | $h->{'address'} .= '/'.$h->{'address_mask'}; | ||
| 57 | } | ||
| 58 | $broker->emit($h); | ||
| 59 | } | ||
| 60 | return(undef); | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | __PACKAGE__->meta->make_immutable; | ||
| 65 | |||
| 66 | 1; |