blib/lib/Metacode/Reader.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 144 CC 18 SCAR 56.4
CC
18
Cov
0%
translate_record
342.0
translate_file
2.0
BEGIN
2.0
CRAP 280.2
1 package XeroxMetacode;
2 use strict;
3 use vars qw(@ISA @EXPORT_OK $VERSION);
4 require Exporter;
5 @ISA = ('Exporter');
6 @EXPORT_OK = ('translate_file', 'translate_record');
7 $VERSION = '0.01';
8  
9 sub translate_file {
10 # read each record from the input metacode file and append each to the
11 # parsed text output
12 my $parse = '';
13 my $sourceMetacode = ${$_[0]};
14
15 my $sourceMetacodeLen = length $sourceMetacode;
16 my $sourceMetacodePos = 0;
17 my $recordLen = 0;
18 my $record;
19 my @fonts = ();
20
21 # Loop until the entire file has been traversed. This reads a series of
22 # records from the binary file and passes each off to be individually
23 # parsed. State is maintained between records in the @fonts array. While
24 # proper metacode processing probably requires more state I just don't
25 # know about that yet. Each record consists of a two byte record length
26 # and then the binary record.
27
28 while ($sourceMetacodePos < $sourceMetacodeLen) {
29 $recordLen = unpack
30 'n',
31 substr
32 $sourceMetacode,
33 $sourceMetacodePos,
34 2;
35  
36 $record = substr
37 $sourceMetacode,
38 $sourceMetacodePos + 2,
39 $recordLen - 2;
40  
41 $parse .= ${ translate_record( \ $record,
42 $sourceMetacodePos,
43 \ @fonts ) };
44
45 $sourceMetacodePos += $recordLen;
46 }
47  
48
49 return \ $parse;
50 }
51  
52 sub translate_record {
53 # this returns an array ref to a parse of the input metacode data. Each
54 # element of the array is either a text node or another array ref. The
55 # plain text nodes are just that. It's the text embedded in the metacode.
56 # The array ref nodes have further meaning. The first element of the
57 # array indicates which code was encountered. there may be other elements
58 # following and you may want to do something with that depending on your
59 # skill and knowledge level.
60 my $parse = '';
61
62 my $record = ${$_[0]};
63 my $foffset = $_[1];
64 my $fonts = $_[2];
65 my $font = 0;
66 my @image = ();
67
68 while (not $record =~ m/\G\z/cog) {
69 # Font switch - the second byte is a packed char
70 if ($record =~ m/\G\000([\000-\377])/cog) {
71 $font = unpack 'C', $1;
72 $parse .= "FontSwitch $font "
73 . ( $fonts->[$font]
74 ? $fonts->[$font]
75 : '?')
76 . "\n";
77 next;
78 }
79  
80 # record terminator
81 if ($record =~ m/\G\001/cog) {
82 $parse .= "EndOfLine\n";
83 next;
84 }
85  
86 if ($record =~ m/\G([\002-\003])/cog) {
87 $parse .= "Orientation " . unpack('C', $1) . "\n";
88 next;
89 }
90  
91 if ($record =~ m/\G([\004-\007])([\000-\377][\000-\377])/cog ) {
92 $parse .= "Positional "
93 . join( ' ', unpack('C', $1),
94 unpack('s', $2) )
95 . "\n";
96 next;
97 }
98  
99 if ($record =~ m/\G\010([\000-\377][\000-\377])([\000-\377])/cog ) {
100 $parse .= "Drawing " . unpack('s',$1) . " $2\n";
101 next;
102 }
103  
104 if ($record =~ m/\G([\011-\037])/cog ) {
105 $parse .= unpack('C',$1) . "\n";
106 next;
107 }
108  
109 # Plain text here. This is all mutually exclusive with the other
110 # codes so there is no chance of a match coinciding with another code
111 if ($record =~ m/\G([\040-\377]+)/cog ) {
112 if ($font) {
113 # print this
114 $parse .= "Text $1\n";
115 next;
116 }
117  
118 # I assume IDEN was set to $DJDE$ - your mileage may vary
119 my $text = $1;
120 next unless $text =~ s/^.\$DJDE\$\s*//;
121  
122 while (not $text =~ m/\G\z/) {
123 $text =~ m/\G\s+/gc and next;
124 $text =~ m/\G,/gc and next;
125 $text =~ m/\G;/gc and next;
126  
127 # FONTS start
128 if ($text =~ m/\GFONTS/gc) {
129 @$fonts = ();
130
131 # skip ahead to the first token;
132 if ($text =~ m/\G\s*=\s*\(/gc) {
133 # loop until the ending parenthesis is found
134 while (not $text =~ m/\G\)/gc) {
135 # grab a token
136 push @$fonts, $1 if $text =~ m/\G([^),]+)/gc;
137 # optional comma and white space
138 $text =~ m/\G,?\s*/gc;
139 }
140  
141 $parse .= "DJDE FONTS " . join(' ',@$fonts) . "\n";
142 }
143 next;
144 }
145 # FONTS end
146  
147 # IMAGE start
148 if ($text =~ m/\GIMAGE/gc) {
149 @image = ();
150  
151 # skip ahead to the first token
152 if ($text =~ m/\G\s*=\s*\(/gc) {
153 # loop until the ending parenthesis is found
154 while (not $text =~ m/\G\)/gc) {
155 # grab a token
156 push @image, $1 if $text =~ m/\G([^),]+)/gc;
157 # optional comma and white space
158 $text =~ m/\G,?\s*/gc;
159 }
160  
161 $parse .= "DJDE IMAGE " . join(' ', @image) . "\n";
162 }
163 next;
164 }
165 # IMAGE end
166  
167 if ($text =~ m/\GEND/gc) {
168 $parse .= "DJDE END";
169 next;
170 }
171  
172 $text =~ m/\G./gc and next;
173 }
174  
175 next;
176 }
177 # End text record
178  
179 # Danger, danger Will Robinson!
180 # This really shouldn't ever get here and if it does then it's likely
181 # to be a script error. It's also possible that the source Metacode
182 # wasn't well formed and somehow the script was led astray
183 $record =~ m/\G([\000-\377])/cog;
184 $parse .= sprintf q[ERROR Unexpected byte 0x%x at offset 0x%x. Reco]
185 . q[rd starts at 0x%x and is 0x%x bytes long.],
186 unpack('C',$1),
187 $foffset + pos $record,
188 $foffset,
189 length $record;
190 next;
191 }
192
193 $parse .= "\n";
194 return \ $parse;
195 }
196  
197 1;
198  
199 __DATA__