| 1 | package oEdtk::Spool; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use oEdtk::Main 0.42; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | our $VERSION = 2.1126; | ||
| 9 | |||
| 10 | # The maximum number of characters we emit before inserting | ||
| 11 | # a newline. | ||
| 12 | my $LINE_CUTOFF = 85; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ( $class, $in, $out ) = @_; | ||
| 16 | |||
| 17 | my $self = { | ||
| 18 | input => ref($in) eq 'GLOB' ? $in : \*STDIN, | ||
| 19 | output => ref($out) eq 'GLOB' ? $out : \*STDOUT, | ||
| 20 | emitted => 0 | ||
| 21 | }; | ||
| 22 | bless $self, $class; | ||
| 23 | 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 | sub parse { | ||
| 42 | my $self = shift; | ||
| 43 | my $processfn = shift; | ||
| 44 | my $fh = $self->{'input'}; | ||
| 45 | |||
| 46 | $self->{'XCORP'} = oe_corporation_get(); | ||
| 47 | |||
| 48 | # Read the input file line by line. | ||
| 49 | while ( my $line = <$fh> ) { | ||
| 50 | chomp($line); | ||
| 51 | |||
| 52 | if ( length $line == 0 ) { | ||
| 53 | warn "INFO: line $. is empty\n"; | ||
| 54 | next; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Get the first four characters. | ||
| 58 | die "ERROR: unexpected line format: \"$line\" at line $.\n" | ||
| 59 | unless $line =~ /^(.{3})([0-9+\- ]?)(.*)$/; | ||
| 60 | 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 | if ( $header =~ /^[0-9a-zA-Z]{3}$/ ) { # Case number 1. | ||
| 72 | $self->{'prev_inres'} = $self->{'inres'}; | ||
| 73 | $self->{'inres'} = $header; | ||
| 74 | $self->{'jump'} = $jump; | ||
| 75 | |||
| 76 | if ( $data =~ /^\s{1,13}EDT XCORP/ ) { | ||
| 77 | |||
| 78 | # warn "EDT XCORP >$data<\n"; | ||
| 79 | # Reinitialize the state. > EDT XCORP M0001< | ||
| 80 | $self->{'numln'} = 0; | ||
| 81 | $self->{'jumpln'} = 0; | ||
| 82 | |||
| 83 | # TODO: handle states in pairs: | ||
| 84 | # EDT XCORP VALUE STATE2 VALUE2 STATE3 VALUE3 etc. | ||
| 85 | $data =~ s/^(\s{1,13}EDT\s)(.*)/$2/; | ||
| 86 | while ($data) { | ||
| 87 | $data =~ s/^([\w\d]+)\s([\w\d]+)\s*(.*)/$3/; | ||
| 88 | $self->{$1} = $2; | ||
| 89 | } | ||
| 90 | |||
| 91 | # warn "EDT XCORP >$data<\n " . $self->{'XCORP'}. "\n"; | ||
| 92 | next; | ||
| 93 | |||
| 94 | } | ||
| 95 | else { | ||
| 96 | # Reinitialize the state. | ||
| 97 | $self->{'numln'} = 1; | ||
| 98 | $self->{'jumpln'} = 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | # Reinitialize the state. | ||
| 102 | $self->{'state'} = {}; | ||
| 103 | $processfn->( $self, $data ); | ||
| 104 | |||
| 105 | } | ||
| 106 | elsif ( $header eq ' ' ) { | ||
| 107 | $self->{'jump'} = $jump; | ||
| 108 | if ( !defined $self->{'inres'} ) { | ||
| 109 | die "ERROR: got seal while not in a resource at line $.\n"; | ||
| 110 | } | ||
| 111 | $self->{'numln'}++; | ||
| 112 | if ( defined($jump) && $jump =~ /\d/ ) { | ||
| 113 | $self->{'jumpln'} += $self->{'jump'}; | ||
| 114 | } | ||
| 115 | $processfn->( $self, $data ); | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | die "ERROR: unexpected line header: \"$header$jump\" at line $.\n"; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | 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 | |||