| 1 | package oEdtk::Spool; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use oEdtk::Main 0.42; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = 2.1129; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # The maximum number of characters we emit before inserting | ||||||||||||||||||||||||||||
| 11 | # a newline. | ||||||||||||||||||||||||||||
| 12 | 2 | my $LINE_CUTOFF = 85; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ( $class, $in, $out ) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $self = { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 18 | input => ref($in) eq 'GLOB' ? $in : \*STDIN, | ||||||||||||||||||||||||||||
| 19 | output => ref($out) eq 'GLOB' ? $out : \*STDOUT, | ||||||||||||||||||||||||||||
| 20 | emitted => 0 | ||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||
| 22 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 23 | 0 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Input stream format, line by line. | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # The first four characters of the line determine the case. | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # case 1: | ||||||||||||||||||||||||||||
| 31 | # /^(\d{3}) (.*)$/ | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # $1 = resource | ||||||||||||||||||||||||||||
| 34 | # $2 = data | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # case 2, inside a resource: | ||||||||||||||||||||||||||||
| 37 | # /^ (\d)(.*)$/ | ||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||
| 39 | # $1 = channel jump | ||||||||||||||||||||||||||||
| 40 | # $2 = data | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 0 | my $processfn = shift; | |||||||||||||||||||||||||||
| 44 | 0 | my $fh = $self->{'input'}; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $self->{'XCORP'} = oe_corporation_get(); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Read the input file line by line. | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | chomp($line); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( length $line == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | warn "INFO: line $. is empty\n"; | |||||||||||||||||||||||||||
| 54 | 0 | next; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Get the first four characters. | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | die "ERROR: unexpected line format: \"$line\" at line $.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | unless $line =~ /^(.{3})([0-9+\- ]?)(.*)$/; | ||||||||||||||||||||||||||||
| 60 | 0 | my ( $header, $jump, $data ) = ( $1, $2, $3 ); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # PROBLEM, on 2 lines like the ones below | ||||||||||||||||||||||||||||
| 63 | #026 *** LAURIANE 20/09/1984 01 REFERENCES DECOMPTE: 07/01/2009RO181 0002* | ||||||||||||||||||||||||||||
| 64 | # 2 *** MAELENN 02/12/1993 11 REFERENCES DECOMPTE: 30/12/2008RO422 0002* | ||||||||||||||||||||||||||||
| 65 | # jump must not be empty in the first case, otherwise $data has 1 more character the first time; in the second case it has one character less | ||||||||||||||||||||||||||||
| 66 | # which shifts the splitting in the main application | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Introduce a technical prefix line, used only to populate state | ||||||||||||||||||||||||||||
| 69 | #004 EDT XCORP M0001 | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( $header =~ /^[0-9a-zA-Z]{3}$/ ) { # Case number 1. | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 0 | $self->{'prev_inres'} = $self->{'inres'}; | |||||||||||||||||||||||||||
| 73 | 0 | $self->{'inres'} = $header; | |||||||||||||||||||||||||||
| 74 | 0 | $self->{'jump'} = $jump; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ( $data =~ /^\s{1,13}EDT XCORP/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # warn "EDT XCORP >$data<\n"; | ||||||||||||||||||||||||||||
| 79 | # Reinitialize the state. > EDT XCORP M0001< | ||||||||||||||||||||||||||||
| 80 | 0 | $self->{'numln'} = 0; | |||||||||||||||||||||||||||
| 81 | 0 | $self->{'jumpln'} = 0; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # TODO: handle states in pairs: | ||||||||||||||||||||||||||||
| 84 | # EDT XCORP VALUE STATE2 VALUE2 STATE3 VALUE3 etc. | ||||||||||||||||||||||||||||
| 85 | 0 | $data =~ s/^(\s{1,13}EDT\s)(.*)/$2/; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | while ($data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $data =~ s/^([\w\d]+)\s([\w\d]+)\s*(.*)/$3/; | |||||||||||||||||||||||||||
| 88 | 0 | $self->{$1} = $2; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # warn "EDT XCORP >$data<\n " . $self->{'XCORP'}. "\n"; | ||||||||||||||||||||||||||||
| 92 | 0 | next; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | # Reinitialize the state. | ||||||||||||||||||||||||||||
| 97 | 0 | $self->{'numln'} = 1; | |||||||||||||||||||||||||||
| 98 | 0 | $self->{'jumpln'} = 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Reinitialize the state. | ||||||||||||||||||||||||||||
| 102 | 0 | $self->{'state'} = {}; | |||||||||||||||||||||||||||
| 103 | 0 | $processfn->( $self, $data ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | elsif ( $header eq ' ' ) { | ||||||||||||||||||||||||||||
| 107 | 0 | $self->{'jump'} = $jump; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( !defined $self->{'inres'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | die "ERROR: got seal while not in a resource at line $.\n"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | $self->{'numln'}++; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ( defined($jump) && $jump =~ /\d/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $jump and $jump =~ /\d/
MC/DC0%0/2
defined $jump and $jump =~ /\d/
defined $jump
$jump =~ /\d/
Truth table0%0/3
A and B
A= defined $jumpB= $jump =~ /\d/
| |||||||||||||||||||||||||||||
| 113 | 0 | $self->{'jumpln'} += $self->{'jump'}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | $processfn->( $self, $data ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||
| 118 | 0 | die "ERROR: unexpected line header: \"$header$jump\" at line $.\n"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | oEdtk::Spool - Helper module for parsing printer spool files | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | use oEdtk::Main; | ||||||||||||||||||||||||||||
| 134 | use oEdtk::Spool; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | oe_new_job($ARGV[0], $ARGV[1]); | ||||||||||||||||||||||||||||
| 137 | my $s = oEdtk::Spool->new(\*IN, \*OUT); | ||||||||||||||||||||||||||||
| 138 | $s->parse(\&process); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | ... | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | oe_compo_link($ARGV[0], $ARGV[1]); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub process($$) { | ||||||||||||||||||||||||||||
| 145 | my ($s, $line) = @_; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | if ($s->{'inres'} eq 'XYZ') { | ||||||||||||||||||||||||||||
| 148 | ... | ||||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||
| 150 | ... | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This module handles the repetitive tasks associated with the parsing of | ||||||||||||||||||||||||||||
| 157 | spool files: it extracts the identifiers of the resource blocks, | ||||||||||||||||||||||||||||
| 158 | the channel jumps, and passes this information along with the current line | ||||||||||||||||||||||||||||
| 159 | to a callback function. As a result, the first four characters of the original | ||||||||||||||||||||||||||||
| 160 | line from the stream are stripped. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 METHODS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =over 4 | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item new | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | The C<new> method creates a Spool object given two filehandles: the first one | ||||||||||||||||||||||||||||
| 169 | for input, and the second one for output. However, the second filehandle is | ||||||||||||||||||||||||||||
| 170 | currently unused. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item parse | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The C<parse> method takes a function reference as a parameter, and calls this | ||||||||||||||||||||||||||||
| 175 | function for each line of the input file, passing it the Spool object as the | ||||||||||||||||||||||||||||
| 176 | parameter, and the current line as the second parameter. The first four | ||||||||||||||||||||||||||||
| 177 | characters of the original line from the stream are stripped. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =over 4 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item $s->{'inres'} | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | The identifier of the current resource block. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item $s->{'prev_inres'} | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | The identifier of the previous resource block. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =item $s->{'numln'} | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | The current line number (starting at 1) in the resource block. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item $s->{'jump'} | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The channel jump, if any. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item $s->{'state'} | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | A hash reference used as a state within the scope of a resource block. It will | ||||||||||||||||||||||||||||
| 204 | be emptied before the callback function is called at the beginning of each | ||||||||||||||||||||||||||||
| 205 | resource block. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =back | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | L<oEdtk::Main|oEdtk> | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | The oEdtk team. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Copyright 2009 - Maxime Henrion <mhenrion@gmail.com> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||