blib/lib/LW4/Reader.pm

filter: Statementstmt 100% 144 / 144 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 66.6% 2 / 3 totaltotal 98.2% 165 / 168 CC 11 SCAR 15.0
CC
11
Cov
99%
build_category_phrases
6.0
read_item_info
5.0
read_header
2.0
CRAP 4.5
↑ Summary
1  
2 package LW4::Reader;
3  
4 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use strict;
6 #use warnings;
7  
8 1 require Exporter;
9 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT_OK = qw( read_header read_item_info build_category_phrases );
13 1 our $VERSION = '0.01';
14  
15 1 ▶sub read_header {
Subroutine read_header: called CC=2 SCAR=6.9
Pod documented
16 # Read the show info information of the file.
17 #
18 # Passed: The file handle of the Lightwright file to read from.
19 # Returns: Hashref of header info.
20
21 1 my $lw4_file_fh = shift;
22 1 my $header_ref = { };
23
24 # Get file header info. This includes: * The file identifier * The
25 # date/time last saved, and the user saved by. * The show info
26 # (title of the document). This data should always predictably be
27 # found at the top of the file so there isn't a need to go seeking
28 # for it.
29
30 # There are some things in this format that I haven't
31 # figured out yet. For now read them in to a discardable
32 # variable and carry on. We'll come back and fill in the
33 # blanks when I have a better understanding of some of
34 # these specifics.
35
36 1 my $junk = <$lw4_file_fh>;
37 1 $junk = <$lw4_file_fh>;
38 1 $junk = <$lw4_file_fh>;
39 1 $header_ref->{file_ident} = <$lw4_file_fh>;
40 1 $junk = <$lw4_file_fh>;
41 1 $junk = <$lw4_file_fh>;
42 1 $junk = <$lw4_file_fh>;
43 1 $header_ref->{show_name} = <$lw4_file_fh>;
44 1 $header_ref->{sub_head_1} = <$lw4_file_fh>;
45 1 $header_ref->{sub_head_2} = <$lw4_file_fh>;
46 1 $header_ref->{sub_head_3} = <$lw4_file_fh>;
47 1 $header_ref->{sub_head_4} = <$lw4_file_fh>;
48 1 $header_ref->{sub_head_5} = <$lw4_file_fh>;
49 1 $header_ref->{sub_head_6} = <$lw4_file_fh>;
50 1 $junk = <$lw4_file_fh>;
51 1 $junk = <$lw4_file_fh>;
52 1 $junk = <$lw4_file_fh>;
53 1 $junk = <$lw4_file_fh>;
54 1 $junk = <$lw4_file_fh>;
55 1 $header_ref->{num_fixtures} = <$lw4_file_fh>;
56 1 $header_ref->{save_date} = <$lw4_file_fh>;
57 1 $junk = <$lw4_file_fh>;
58 1 $header_ref->{save_time} = <$lw4_file_fh>;
59 1 $junk = <$lw4_file_fh>;
60 1 $header_ref->{max_num_fixtures} = <$lw4_file_fh>;
61
62 # Some entries have preceding white space that we'll want to
63 # strip.
64
65 1 foreach my $key (keys %$header_ref) {
66 12 $header_ref->{$key} =~ s/^\s+//gxms;
67 12 $header_ref->{$key} =~ s/\s+$//gxms;
68 }
69
70 1 return $header_ref;
71 }
72  
73 1 ▶sub read_item_info {
Subroutine read_item_info: called CC=5 SCAR=16.1
Pod documented
74 # Read the data portions of the file.
75 #
76 # Passed: the file handle of the LW4 file.
77 # Returns: Array of hashes of item information extracted from
78 # the file.
79  
80 1 my $lw4_file_fh = shift;
81 1 my @lw4_item_info_AoH;
82  
83 # * Build the "vocabulary" the file uses to describe
84 # it's data.
85 1 my $phrase_table_AoA = build_category_phrases($lw4_file_fh);
86
87 1 my $current_count = 0;
88
89 # ** Seek to the line that contains '** Item Info:'
90 1 ▶ LINE: while (<$lw4_file_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $lw4_file_fh)) 2071 1 2072
91 2071 my $lw4_file_line = $_;
92 2071 chomp $lw4_file_line;
93
94 # Read lines from the file until we find Item info, then create
95 # an AoH of each item, substituting the references in the file
96 # for the names we have compiled in the phrase table.
97
98 2071 ▶ if ($lw4_file_line =~ m/^\*\* Item Info:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lw4_file_line =~ /^\*\* Item Info:/) 1 2070 2071
99 # There are two things at the top of the Item Info section,
100 # and I don't know what they are yet. We'll probably be able
101 # to use them for something once I figure out what they are,
102 # but for now just store them.
103 1 my $final_rec = 0;
104 1 my $item_count = <$lw4_file_fh>;
105 1 chomp $item_count;
106 1 $item_count =~ s/^\s//;
107 1 $item_count =~ s/\s+$//g;
108
109 1 my $mystery_item_head_two = <$lw4_file_fh>;
110 1 my $head_seperator = <$lw4_file_fh>;
111
112 1 ▶ ITEM: while ($current_count <= $item_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_count <= $item_count) 23 1 24
113 23 $lw4_file_line = <$lw4_file_fh>;
114 23 chomp $lw4_file_line;
115  
116 23 my %lw4_item;
117
118 # Build the item hash table, one lines at a time. There are
119 # still a lot of uknowns in this file, but we should be able
120 # to figure them out at some point in the future.
121 #
122 # Rather than doing phrase table substitutions in place,
123 # we'll batch clean out the newlines and leading whitespace
124 # after the fact, and then make the substitutions for the
125 # appropriate records.
126
127 23 $lw4_item{unknown1} = $lw4_file_line;
128 23 $lw4_item{channel} = <$lw4_file_fh>;
129 23 $lw4_item{dimmer} = <$lw4_file_fh>;
130 23 $lw4_item{unit} = <$lw4_file_fh>;
131 23 $lw4_item{watts} = <$lw4_file_fh>;
132 23 $lw4_item{circuit} = <$lw4_file_fh>;
133 23 $lw4_item{unknown2} = <$lw4_file_fh>;
134 23 $lw4_item{unknown3} = <$lw4_file_fh>;
135 23 $lw4_item{unknown4} = <$lw4_file_fh>;
136 23 $lw4_item{unknown5} = <$lw4_file_fh>;
137 23 $lw4_item{unknown6} = <$lw4_file_fh>;
138 23 $lw4_item{unknown7} = <$lw4_file_fh>;
139 23 $lw4_item{unknown8} = <$lw4_file_fh>;
140 23 $lw4_item{purpose} = <$lw4_file_fh>; # lookup needed
141 23 $lw4_item{position} = <$lw4_file_fh>; # lookup needed
142 23 $lw4_item{color} = <$lw4_file_fh>; # lookup needed
143 23 $lw4_item{accessory} = <$lw4_file_fh>; # lookup needed
144 23 $lw4_item{type} = <$lw4_file_fh>; # lookup needed
145 23 $lw4_item{pattern} = <$lw4_file_fh>; # lookup needed
146 23 $lw4_item{unknown10} = <$lw4_file_fh>;
147 23 $lw4_item{unknown11} = <$lw4_file_fh>;
148 23 $lw4_item{unknown12} = <$lw4_file_fh>;
149 23 $lw4_item{unknown13} = <$lw4_file_fh>;
150 23 $lw4_item{unknown14} = <$lw4_file_fh>;
151 23 $lw4_item{unknown15} = <$lw4_file_fh>;
152 23 $lw4_item{unknown16} = <$lw4_file_fh>;
153 23 $lw4_item{unknown17} = <$lw4_file_fh>;
154 23 $lw4_item{unknown18} = <$lw4_file_fh>;
155 23 $lw4_item{unknown19} = <$lw4_file_fh>;
156 23 $lw4_item{unknown20} = <$lw4_file_fh>;
157 23 $lw4_item{unknown21} = <$lw4_file_fh>;
158 23 $lw4_item{unknown22} = <$lw4_file_fh>;
159 23 $lw4_item{unknown23} = <$lw4_file_fh>;
160 23 $lw4_item{unknown24} = <$lw4_file_fh>;
161 23 $lw4_item{unknown25} = <$lw4_file_fh>;
162 23 $lw4_item{unknown26} = <$lw4_file_fh>;
163 23 $lw4_item{unknown27} = <$lw4_file_fh>;
164 23 $lw4_item{unknown28} = <$lw4_file_fh>;
165 23 $lw4_item{item_key} = <$lw4_file_fh>;
166 23 $lw4_item{unknown30} = <$lw4_file_fh>;
167 23 $lw4_item{unknown31} = <$lw4_file_fh>;
168 23 $lw4_item{unknown32} = <$lw4_file_fh>;
169 23 $lw4_item{unknown33} = <$lw4_file_fh>;
170 23 $lw4_item{unknown34} = <$lw4_file_fh>;
171 23 $lw4_item{unknown35} = <$lw4_file_fh>;
172 23 $lw4_item{unknown36} = <$lw4_file_fh>;
173 23 my $seperator = <$lw4_file_fh>;
174
175 # This should be the end of the item record.
176 # Eliminate leading whitespace and newlines.
177
178 23 foreach my $key (keys %lw4_item) {
179 1058 chomp $lw4_item{$key};
180 1058 $lw4_item{$key} =~ s/^\s//;
181 1058 $lw4_item{$key} =~ s/\s+$//g;
182 }
183
184 # Now perform the substitutions for the items in
185 # the phrase table.
186
187 23 my $temp_buff = $lw4_item{purpose};
188 23 $lw4_item{purpose} = $phrase_table_AoA->[1]->[$temp_buff];
189
190 23 $temp_buff = $lw4_item{position};
191 23 $lw4_item{position} = $phrase_table_AoA->[2]->[$temp_buff];
192
193 23 $temp_buff = $lw4_item{type};
194 23 $lw4_item{type} = $phrase_table_AoA->[3]->[$temp_buff];
195
196 23 $temp_buff = $lw4_item{accessory};
197 23 $lw4_item{accessory} = $phrase_table_AoA->[4]->[$temp_buff];
198
199 23 $temp_buff = $lw4_item{color};
200 23 $lw4_item{color} = $phrase_table_AoA->[5]->[$temp_buff];
201
202 23 $temp_buff = $lw4_item{pattern};
203 23 $lw4_item{pattern} = $phrase_table_AoA->[6]->[$temp_buff];
204
205 # Now that substitutions are completed, add a ref to the hash
206 # to the AoH.
207
208 23 push @lw4_item_info_AoH, \%lw4_item;
209
210 23 $current_count++;
211
212 } # end ITEM: while (<$lw4_file_fh>) {
213 } # end if ($lw4_file_line =~ m/^\*\* Item Info:/) {
214 } # end LINE: while (<$lw4_file_fh>) {
215
216 1 return \@lw4_item_info_AoH;
217 } # end sub read_item_info {
218  
219 2 ▶sub build_category_phrases {
Subroutine build_category_phrases: called CC=6 SCAR=17.9
Pod undocumented
220 # Passed: The file handle of the LW4 file.
221 # Returns: Array of Arrays of category phrases
222  
223 2 my $lw4_file_fh = shift;
224  
225 # This method of creating the ref to @category_phrases is a little
226 # bit wordy, but safe for use strict.
227
228 2 my @category_phrases = ( );
229 2 my $category_phrases = \@category_phrases;
230 2 my $category_index = 0;
231
232 # * Seek to '** Category Phrases:'
233  
234 2 ▶ LINE: while (<$lw4_file_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $lw4_file_fh)) 657 0 657
235 657 my $lw4_file_line = $_;
236 657 chomp $lw4_file_line;
237  
238 ##print "$lw4_file_line\n";
239  
240 # Read the Category Phrases out of the LW4 file. Everything is
241 # sequential, so an AoA seems to make the most sense. If we
242 # make sure that we're incrimenting the index on the newline that
243 # seperates the records BEFORE reading the first record, everything
244 # will start at index 1, so we won't need to read the Category
245 # Index table later.
246 657 ▶ if ($lw4_file_line =~ m/^\*\* Category Phrases:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lw4_file_line =~ /^\*\* Category Phrases:/) 2 655 657
247 2 ▶ PHRASE: while (<$lw4_file_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $lw4_file_fh)) 136 0 136
248 136 my $lw4_file_line = $_;
249 136 chomp $lw4_file_line;
250 136 ▶ if ($lw4_file_line =~ m/^\W+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($lw4_file_line =~ /^\W+$/) 36 100 136
251 {
252 36 $category_index++;
253 }
254 136 ▶ last LINE if $lw4_file_line =~ m/^\*\* Category Order Pointers:/;
Branch: 1 branch
BranchTrueFalseTotal
if $lw4_file_line =~ /^\*\* Category Order Pointers:/ 2 134 136
255  
256 # Apparently there is some trailing white space at the
257 # end of each record.
258 134 $lw4_file_line =~ s/\s+$//g;
259 134 push @{ $category_phrases->[$category_index] }, $lw4_file_line;
260
261 }
262 }
263 }
264
265 2 return $category_phrases;
266 }
267  
268  
269 1 1;
270
271 # Autoload methods go after =cut, and are processed by the autosplit program.
272  
273 __END__
↑ Summary