blib/lib/Text/Convert/PETSCII.pm

filter: Statementstmt 97.5% 158 / 162 Branchbran 96.1% 100 / 104 Conditioncond 83.9% 68 / 81 MC/DCmcdc 72.9% 35 / 48 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 91.8% 382 / 416 CC 83 SCAR 25.6
CC
83
Cov
94%
petscii_to_screen_codes
21.9
_petscii_to_screen_code
16.0
write_petscii_char
15.0
CRAP 13.0
↑ Summary PETSCII.xs »
1 package Text::Convert::PETSCII;
2  
3 =head1 NAME
4  
5 Text::Convert::PETSCII - ASCII/PETSCII text converter
6  
7 =head1 SYNOPSIS
8  
9 use Text::Convert::PETSCII qw/:all/;
10  
11 # Convert an ASCII string to a PETSCII string:
12 my $petscii_string = ascii_to_petscii($ascii_string);
13  
14 # Convert a PETSCII string to an ASCII string:
15 my $ascii_string = petscii_to_ascii($petscii_string);
16  
17 # Convert CBM screen codes to a PETSCII string:
18 my $petscii_string = screen_codes_to_petscii($screen_codes);
19  
20 # Convert a PETSCII string to CBM screen codes:
21 my $screen_codes = petscii_to_screen_codes($petscii_string);
22  
23 # Set mode for writing PETSCII character's representation to a file handle:
24 set_petscii_write_mode($write_mode);
25  
26 # Write PETSCII single character's textual representation to a file handle:
27 write_petscii_char($file_handle, $petscii_char);
28  
29 # Validate whether given PETSCII string text may normally be printed out:
30 my $is_printable = is_printable_petscii_string($petscii_string);
31  
32 # Validate whether given text may be considered a valid PETSCII string:
33 my $is_valid = is_valid_petscii_string($text_string);
34  
35 =head1 DESCRIPTION
36  
37 This package provides two basic methods for converting text format between ASCII and PETSCII character sets. PETSCII stands for the "PET Standard Code of Information Interchange" and is also known as CBM ASCII. PETSCII character set has been widely used in Commodore Business Machines (CBM)'s 8-bit home computers, starting with the PET from 1977 and including the VIC-20, C64, CBM-II, Plus/4, C16, C116 and C128.
38  
39 =head1 METHODS
40  
41 =cut
42  
43 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 our %EXPORT_TAGS = ();
45 3 $EXPORT_TAGS{'convert'} = [ qw(&ascii_to_petscii &petscii_to_ascii) ];
46 3 $EXPORT_TAGS{'display'} = [ qw(&set_petscii_write_mode &write_petscii_char) ];
47 3 $EXPORT_TAGS{'screen'} = [ qw(&screen_codes_to_petscii &petscii_to_screen_codes) ];
48 3 $EXPORT_TAGS{'validate'} = [ qw(&is_printable_petscii_string &is_valid_petscii_string) ];
49 3 $EXPORT_TAGS{'all'} = [ @{$EXPORT_TAGS{'convert'}}, @{$EXPORT_TAGS{'display'}}, @{$EXPORT_TAGS{'screen'}}, @{$EXPORT_TAGS{'validate'}} ];
50 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
51 3 our @EXPORT = qw();
52  
53 3 our $VERSION = '0.05';
54  
55 3 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 3 require XSLoader;
59 3 XSLoader::load(__PACKAGE__, $VERSION);
60  
61 3 our $WRITE_MODE = 'unshifted';
62  
63 =head2 ascii_to_petscii
64  
65 Convert an ASCII string to a PETSCII string:
66  
67 my $petscii_string = ascii_to_petscii($ascii_string);
68  
69 Input data is handled as a stream of bytes. When original ASCII string contains any non-ASCII character, a relevant warning will be triggered, providing detailed information about invalid character's integer code and its position within the source string.
70  
71 =cut
72  
73 11 ▶sub ascii_to_petscii {
Subroutine ascii_to_petscii: called CC=7 SCAR=19.5
Pod documented
74 11 my ($str_ascii) = @_;
75 11 my $str_petscii = '';
76 11 my $position = 1;
77 11 ▶ while ($str_ascii =~ s/^(.)(.*)$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_ascii =~ s/^(.)(.*)$/$2/) 22 11 33
78 22 my $c = ord $1;
79 22 my $code = $c & 0x7f;
80 22 ▶ if ($c != $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c != $code) 2 20 22
81 2 carp sprintf qq{Invalid ASCII code at position %d of converted text string: "0x%02x" (convertible codes include bytes between 0x00 and 0x7f)}, $position, $c;
82 }
83 22 ▶ if ($code >= ord 'A' && $code <= ord 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if ($code >= 65 and $code <= 90) { } 3 19 22
elsif ($code >= 97 and $code <= 122) { } 13 12 25
Condition100%3/3
$code >= 65 and $code <= 90
!ll&&!rl&&r
5143
MC/DC100%2/2
$code >= 65 and $code <= 90
$code >= 65 $code <= 90
Truth table100%3/3
A and B
A= $code >= 65B= $code <= 90
ABresult
0X0
100
111
84 3 $code += 32;
85 ▶ } elsif ($code >= ord 'a' && $code <= ord 'z') {
Condition66%2/3
$code >= 97 and $code <= 122
!ll&&!rl&&r
6013
MC/DC50%1/2
$code >= 97 and $code <= 122
$code >= 97 $code <= 122
Truth table66%2/3
A and B
A= $code >= 97B= $code <= 122
ABresult
0X0
100
111
86 13 $code -= 32;
87 }
88 22 $str_petscii .= chr $code;
89 22 $position++;
90 }
91 11 return $str_petscii;
92 }
93  
94 =head2 petscii_to_ascii
95  
96 Convert a PETSCII string to an ASCII string:
97  
98 my $ascii_string = petscii_to_ascii($petscii_string);
99  
100 Input data is handled as a stream of bytes. Note that integer codes between 0x80 and 0xff despite of being valid PETSCII codes are not convertible into any ASCII equivalents, therefore they trigger a relevant warning, providing detailed information about invalid character's integer code and its position within the source string.
101  
102 =cut
103  
104 5 ▶sub petscii_to_ascii {
Subroutine petscii_to_ascii: called CC=8 SCAR=20.9
Pod documented
105 5 my ($str_petscii) = @_;
106 5 my $str_ascii = '';
107 5 my $position = 1;
108 5 ▶ while ($str_petscii =~ s/^(.)(.*)$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_petscii =~ s/^(.)(.*)$/$2/) 23 5 28
109 23 my $c = ord $1;
110 23 my $code = $c & 0x7f;
111 23 ▶ if ($c != $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c != $code) 3 20 23
112 3 carp sprintf qq{Invalid PETSCII code at position %d of converted text string: "0x%02x" (convertible codes include bytes between 0x00 and 0x7f)}, $position, $c;
113 }
114 23 ▶ if ($code >= ord 'A' && $code <= ord 'Z') {
Branch: 3 branches
BranchTrueFalseTotal
if ($code >= 65 and $code <= 90) { } 12 11 23
elsif ($code >= 97 and $code <= 122) { } 8 3 11
elsif ($code == 127) { } 0 3 3
Condition100%3/3
$code >= 65 and $code <= 90
!ll&&!rl&&r
3812
MC/DC100%2/2
$code >= 65 and $code <= 90
$code >= 65 $code <= 90
Truth table100%3/3
A and B
A= $code >= 65B= $code <= 90
ABresult
0X0
100
111
115 12 $code += 32;
116 ▶ } elsif ($code >= ord 'a' && $code <= ord 'z') {
Condition66%2/3
$code >= 97 and $code <= 122
!ll&&!rl&&r
308
MC/DC50%1/2
$code >= 97 and $code <= 122
$code >= 97 $code <= 122
Truth table66%2/3
A and B
A= $code >= 97B= $code <= 122
ABresult
0X0
100
111
117 8 $code -= 32;
118 } elsif ($code == 0x7f) {
119 0 $code = 0x3f;
120 }
121 23 $str_ascii .= chr $code;
122 23 $position++;
123 }
124 5 return $str_ascii;
125 }
126  
127 =head2 screen_codes_to_petscii
128  
129 Convert CBM screen codes to a PETSCII string:
130  
131 my $petscii_string = screen_codes_to_petscii($screen_codes);
132  
133 Input screen codes are expected to be a scalar value that is handled as a stream of bytes. And so is a returned value.
134  
135 =cut
136  
137 17 ▶sub screen_codes_to_petscii {
Subroutine screen_codes_to_petscii: called CC=11 SCAR=24.0
Pod documented
138 17 my ($screen_codes) = @_;
139  
140 17 my $reverse_flag = 0;
141  
142 17 my $petscii_string;
143  
144 17 for my $screen_char (split //, $screen_codes) {
145  
146 42 my $screen_code = ord ($screen_char);
147  
148 # RVS ON:
149 42 ▶ if ($screen_code & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($screen_code & 128) { } 11 31 42
150 11 ▶ unless ($reverse_flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reverse_flag) 1 10 11
151 1 $reverse_flag = 1;
152 1 $petscii_string .= chr (0x12);
153 }
154 11 $screen_code ^= 0x80;
155 }
156 # RVS OFF:
157 else {
158 31 ▶ if ($reverse_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse_flag) 1 30 31
159 1 $reverse_flag = 0;
160 1 $petscii_string .= chr (0x92);
161 }
162 }
163  
164 # $20 .. $3f ("SPACE ($20)" .. "?"):
165 42 my $petscii_byte = $screen_code;
166 # $00 .. $1f ("@" .. "left arrow"):
167 42 ▶ if ($petscii_byte >= 0x00 && $petscii_byte < 0x20) {
Branch: 3 branches
BranchTrueFalseTotal
if ($petscii_byte >= 0 and $petscii_byte < 32) { } 12 30 42
elsif ($petscii_byte >= 64 and $petscii_byte < 96) { } 9 21 30
elsif ($petscii_byte >= 96 and $petscii_byte < 128) { } 3 36 39
Condition66%2/3
$petscii_byte >= 0 and $petscii_byte < 32
!ll&&!rl&&r
03012
MC/DC50%1/2
$petscii_byte >= 0 and $petscii_byte < 32
$petscii_byte >= 0 $petscii_byte < 32
Truth table66%2/3
A and B
A= $petscii_byte >= 0B= $petscii_byte < 32
ABresult
0X0
100
111
168 12 $petscii_byte += 0x40;
169 }
170 # $40 .. $5f ("horizontal line" .. "top-right triangle"):
171 ▶ elsif ($petscii_byte >= 0x40 && $petscii_byte < 0x60) {
Condition100%3/3
$petscii_byte >= 64 and $petscii_byte < 96
!ll&&!rl&&r
1839
MC/DC100%2/2
$petscii_byte >= 64 and $petscii_byte < 96
$petscii_byte >= 64 $petscii_byte < 96
Truth table100%3/3
A and B
A= $petscii_byte >= 64B= $petscii_byte < 96
ABresult
0X0
100
111
172 9 $petscii_byte += 0x20;
173 }
174 # $60 .. $7f ("SPACE ($60)" .. "racing square"):
175 ▶ elsif ($petscii_byte >= 0x60 && $petscii_byte < 0x80) {
Condition66%2/3
$petscii_byte >= 96 and $petscii_byte < 128
!ll&&!rl&&r
1803
MC/DC50%1/2
$petscii_byte >= 96 and $petscii_byte < 128
$petscii_byte >= 96 $petscii_byte < 128
Truth table66%2/3
A and B
A= $petscii_byte >= 96B= $petscii_byte < 128
ABresult
0X0
100
111
176 3 $petscii_byte += 0x40;
177 }
178  
179 42 $petscii_string .= chr ($petscii_byte);
180 }
181  
182 17 return $petscii_string;
183 }
184  
185 =head2 petscii_to_screen_codes
186  
187 Convert a PETSCII string to CBM screen codes:
188  
189 my $screen_codes = petscii_to_screen_codes($petscii_string);
190  
191 Input PETSCII string is expected to be a scalar value that is handled as a stream of bytes. And so is a returned value.
192  
193 =cut
194  
195 28 ▶sub petscii_to_screen_codes {
Subroutine petscii_to_screen_codes: called CC=21 SCAR=30.8
Pod documented
196 28 my ($petscii_string) = @_;
197  
198 28 my $reverse_flag = 0x00;
199  
200 28 my $screen_codes;
201  
202 28 for my $petscii_char (split //, $petscii_string) {
203  
204 59 my $petscii_byte = ord ($petscii_char);
205  
206 # RVS ON:
207 59 ▶ if ($petscii_byte == 0x12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($petscii_byte == 18) 1 58 59
208 1 $reverse_flag = 0x80;
209 1 next;
210 }
211  
212 # RVS OFF:
213 58 ▶ if ($petscii_byte == 0x92) {
Branch: 1 branch
BranchTrueFalseTotal
if ($petscii_byte == 146) 1 57 58
214 1 $reverse_flag = 0x00;
215 1 next;
216 }
217  
218 # $c0 .. $df are the same as $60 .. $7f
219 57 ▶ if ($petscii_byte >= 0xc0 && $petscii_byte < 0xe0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($petscii_byte >= 192 and $petscii_byte < 224) { } 1 56 57
elsif ($petscii_byte >= 224 and $petscii_byte < 255) { } 0 56 56
elsif ($petscii_byte == 255) { } 0 56 56
elsif ($petscii_byte >= 149 and $petscii_byte < 156) { } 2 102 104
Condition66%2/3
$petscii_byte >= 192 and $petscii_byte < 224
!ll&&!rl&&r
5601
MC/DC50%1/2
$petscii_byte >= 192 and $petscii_byte < 224
$petscii_byte >= 192 $petscii_byte < 224
Truth table66%2/3
A and B
A= $petscii_byte >= 192B= $petscii_byte < 224
ABresult
0X0
100
111
220 1 $petscii_byte -= 0x60;
221 }
222 # $e0 .. $fe are the same as $a0 .. $be
223 ▶ elsif ($petscii_byte >= 0xe0 && $petscii_byte < 0xff) {
Condition33%1/3
$petscii_byte >= 224 and $petscii_byte < 255
!ll&&!rl&&r
5600
MC/DC0%0/2
$petscii_byte >= 224 and $petscii_byte < 255
$petscii_byte >= 224 $petscii_byte < 255
Truth table33%1/3
A and B
A= $petscii_byte >= 224B= $petscii_byte < 255
ABresult
0X0
100
111
224 0 $petscii_byte -= 0x40;
225 }
226 # $ff is the same as $7e
227 elsif ($petscii_byte == 0xff) {
228 0 $petscii_byte = 0x7e;
229 }
230 # $95 .. $9b are the same as $75 .. $7b
231 ▶ elsif ($petscii_byte >= 0x95 && $petscii_byte < 0x9c) {
Condition100%3/3
$petscii_byte >= 149 and $petscii_byte < 156
!ll&&!rl&&r
4862
MC/DC100%2/2
$petscii_byte >= 149 and $petscii_byte < 156
$petscii_byte >= 149 $petscii_byte < 156
Truth table100%3/3
A and B
A= $petscii_byte >= 149B= $petscii_byte < 156
ABresult
0X0
100
111
232 2 $petscii_byte -= 0x20;
233 }
234  
235 # Skip all non-printable characters:
236 57 ▶ if ($petscii_byte >= 0x00 && $petscii_byte < 0x20 || $petscii_byte >= 0x80 && $petscii_byte < 0xa0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($petscii_byte >= 0 and $petscii_byte < 32 or $petscii_byte >= 128 and $petscii_byte < 160) 11 46 57
Condition88%8/9
$petscii_byte >= 0 and $petscii_byte < 32 or $petscii_byte >= 128 and $petscii_byte < 160
l!l&&r!l&&!r
5646
$petscii_byte >= 0 and $petscii_byte < 32
!ll&&!rl&&r
0525
$petscii_byte >= 128 and $petscii_byte < 160
!ll&&!rl&&r
4246
MC/DC75%3/4
$petscii_byte >= 0 and $petscii_byte < 32 or $petscii_byte >= 128 and $petscii_byte < 160
$petscii_byte >= 0 $petscii_byte < 32 $petscii_byte >= 128 $petscii_byte < 160
Truth table57%4/7
A and B or C and D
A= $petscii_byte >= 0B= $petscii_byte < 32C= $petscii_byte >= 128D= $petscii_byte < 160
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
237 11 next;
238 }
239  
240 # $20 .. $3f ("SPACE ($20)" .. "?"):
241 46 my $screen_code = $petscii_byte;
242 # $40 .. $5f ("@" .. "left arrow"):
243 46 ▶ if ($screen_code >= 0x40 && $screen_code < 0x60) {
Branch: 3 branches
BranchTrueFalseTotal
if ($screen_code >= 64 and $screen_code < 96) { } 12 34 46
elsif ($screen_code >= 96 and $screen_code < 128) { } 12 22 34
elsif ($screen_code >= 160 and $screen_code < 192) { } 4 36 40
Condition100%3/3
$screen_code >= 64 and $screen_code < 96
!ll&&!rl&&r
181612
MC/DC100%2/2
$screen_code >= 64 and $screen_code < 96
$screen_code >= 64 $screen_code < 96
Truth table100%3/3
A and B
A= $screen_code >= 64B= $screen_code < 96
ABresult
0X0
100
111
244 12 $screen_code -= 0x40;
245 }
246 # $60 .. $7f ("horizontal line" .. "top-right triangle"):
247 ▶ elsif ($screen_code >= 0x60 && $screen_code < 0x80) {
Condition100%3/3
$screen_code >= 96 and $screen_code < 128
!ll&&!rl&&r
18412
MC/DC100%2/2
$screen_code >= 96 and $screen_code < 128
$screen_code >= 96 $screen_code < 128
Truth table100%3/3
A and B
A= $screen_code >= 96B= $screen_code < 128
ABresult
0X0
100
111
248 12 $screen_code -= 0x20;
249 }
250 # $a0 .. $bf ("SPACE ($A0)" .. "racing square"):
251 ▶ elsif ($screen_code >= 0xa0 && $screen_code < 0xc0) {
Condition66%2/3
$screen_code >= 160 and $screen_code < 192
!ll&&!rl&&r
1804
MC/DC50%1/2
$screen_code >= 160 and $screen_code < 192
$screen_code >= 160 $screen_code < 192
Truth table66%2/3
A and B
A= $screen_code >= 160B= $screen_code < 192
ABresult
0X0
100
111
252 4 $screen_code -= 0x40;
253 }
254  
255 46 $screen_codes .= chr ($screen_code | $reverse_flag);
256 }
257  
258 28 return $screen_codes;
259 }
260  
261 =head2 set_petscii_write_mode
262  
263 Set mode for writing PETSCII character's textual representation to a file handle:
264  
265 set_petscii_write_mode('shifted');
266 set_petscii_write_mode('unshifted');
267  
268 There are two modes available. A "shifted" mode, also known as a "text" mode, refers to mode, in which lowercase letters occupy the range 0x41 .. 0x5a, and uppercase letters occupy the range 0xc1 .. 0xda. In "unshifted" mode, codes 0x60 .. 0x7f and 0xa0 .. 0xff are allocated to CBM-specific block graphics characters.
269  
270 If not set explicitly, writing PETSCII char defaults to "unshifted" mode.
271  
272 =cut
273  
274 9 ▶sub set_petscii_write_mode {
Subroutine set_petscii_write_mode: called CC=2 SCAR=7.6
Pod documented
275 9 my ($petscii_write_mode) = @_;
276 9 ▶ if (not defined $petscii_write_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $petscii_write_mode) 0 9 9
277 0 carp q{Failed to set PETSCII write mode: no mode to set has been specified};
278 }
279 9 _petscii_write_mode($petscii_write_mode);
280 }
281  
282 36 ▶sub _petscii_write_mode {
Subroutine _petscii_write_mode: called CC=3 SCAR=11.0
283 36 my ($petscii_write_mode) = @_;
284 36 ▶ if (defined $petscii_write_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $petscii_write_mode) 9 27 36
285 9 ▶ unless (grep { $petscii_write_mode eq $_ } qw/shifted unshifted/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$petscii_write_mode eq $_;} "shifted", "unshifted") 1 8 9
286 1 carp sprintf q{Failed to set PETSCII write mode, invalid PETSCII write mode: "%s"}, $petscii_write_mode;
287 1 return;
288 }
289 8 $WRITE_MODE = $petscii_write_mode;
290 }
291 35 return $WRITE_MODE;
292 }
293  
294 =head2 write_petscii_char
295  
296 Write PETSCII character's textual representation to a file handle:
297  
298 write_petscii_char($fh, $petscii_char);
299  
300 C<$fh> is expected to be an opened file handle that PETSCII character's textual representation may be written to, and C<$petscii_char> is expected to either be an integer code (between 0x20 and 0x7f as well as between 0xa0 and 0xff, since control codes between 0x00 and 0x1f and between 0x80 and 0x9f are not printable by design) or a character byte (the actual single byte with PETSCII data to be processed, same rules for possible printable characters apply).
301  
302 =cut
303  
304 33 ▶sub write_petscii_char {
Subroutine write_petscii_char: called CC=15 SCAR=27.1
Pod documented
305 33 my ($fh, $chr_petscii) = @_;
306  
307 # Check if character provided is actually a character or a numerical index:
308 33 my $screen_code = undef;
309 33 ▶ if (_is_integer($chr_petscii)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_is_integer($chr_petscii)) { } 21 12 33
elsif (&_is_string($chr_petscii)) { } 10 2 12
310 21 ▶ if ($chr_petscii < 0x20 or $chr_petscii > 0xff or ($chr_petscii >= 0x80 and $chr_petscii <= 0x9f)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chr_petscii < 32 or $chr_petscii > 255 or $chr_petscii >= 128 and $chr_petscii <= 159) { } 2 19 21
Condition77%7/9
$chr_petscii < 32 or $chr_petscii > 255 or $chr_petscii >= 128 and $chr_petscii <= 159
l!l&&r!l&&!r
2019
$chr_petscii < 32 or $chr_petscii > 255
l!l&&r!l&&!r
1119
$chr_petscii >= 128 and $chr_petscii <= 159
!ll&&!rl&&r
1090
MC/DC50%2/4
$chr_petscii < 32 or $chr_petscii > 255 or $chr_petscii >= 128 and $chr_petscii <= 159
$chr_petscii < 32 $chr_petscii > 255 $chr_petscii >= 128 $chr_petscii <= 159
Truth table80%4/5
A or B or C and D
A= $chr_petscii < 32B= $chr_petscii > 255C= $chr_petscii >= 128D= $chr_petscii <= 159
ABCDresult
1XXX1
01XX1
00111
000X0
00100
311 2 carp sprintf q{Value out of range: "0x%02x" (PETSCII character set supports printable characters in the range of 0x20 to 0x7f and 0xa0 to 0xff)}, $chr_petscii;
312 }
313 else {
314 19 $screen_code = _petscii_to_screen_code($chr_petscii);
315 }
316 }
317 elsif (_is_string($chr_petscii)) {
318 10 ▶ if (length $chr_petscii == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $chr_petscii == 0) { } 1 9 10
elsif (length $chr_petscii > 1) { } 1 8 9
319 1 carp q{PETSCII character byte missing, nothing to be printed out};
320 }
321 elsif (length $chr_petscii > 1) {
322 1 carp sprintf q{PETSCII character string too long: %d bytes (currently writing only a single character is supported)}, length $chr_petscii;
323 }
324 else {
325 8 $screen_code = _petscii_to_screen_code(ord $chr_petscii);
326 }
327 }
328 else {
329 2 my $invalid_data = Data::Dumper->new([$chr_petscii])->Terse(1)->Indent(0)->Dump();
330 2 carp qq{Not a valid PETSCII character to write: ${invalid_data} (expected integer code or character byte)};
331 }
332  
333 # Print out character's textual representation based on the calculated screen code:
334 33 ▶ if (defined $screen_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $screen_code) 27 6 33
335 27 ▶ my $shifted_mode = _petscii_write_mode() eq 'shifted' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_petscii_write_mode() eq 'shifted' ? : 8 19 27
336 27 my @font_data = _get_font_data($screen_code, $shifted_mode);
337 27 foreach my $font_line (@font_data) {
338 216 ▶ for (my $i = 0; $i < 8; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8) 1728 216 1944
339 1728 ▶ my $font_pixel = $font_line & 0x80 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$font_line & 128 ? : 541 1187 1728
340 1728 ▶ if ($font_pixel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font_pixel) { } 541 1187 1728
341 541 print q{*};
342 }
343 else {
344 1187 print q{-};
345 }
346 1728 $font_line <<= 1;
347 }
348 216 print qq{\n};
349 }
350 }
351  
352 33 return;
353 }
354  
355 =head2 is_printable_petscii_string
356  
357 Validate whether given PETSCII string text may normally be printed out:
358  
359 my $is_printable = is_printable_petscii_string($petscii_string);
360  
361 Returns true value upon successful validation, and false otherwise. False value will also be immediately returned when text string that is given as an argument is not a PETSCII string at all.
362  
363 =cut
364  
365 12 ▶sub is_printable_petscii_string {
Subroutine is_printable_petscii_string: called CC=4 SCAR=13.9
Pod documented
366 12 my ($text_string) = @_;
367  
368 12 ▶ return 0 unless is_valid_petscii_string($text_string);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_petscii_string($text_string) 3 9 12
369  
370 9 ▶ return 1 if length $text_string == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $text_string == 0 1 8 9
371  
372 8 ▶ unless ($text_string =~ m/^[^\x20-\x7f\xa0-\xff]*$/g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text_string =~ /^[^\x20-\x7f\xa0-\xff]*$/g) 4 4 8
373 4 return 1;
374 }
375  
376 4 return 0;
377 }
378  
379 =head2 is_valid_petscii_string
380  
381 Validate whether given text may be considered a valid PETSCII string:
382  
383 my $is_valid = is_valid_petscii_string($text_string);
384  
385 Returns true value upon successful validation, and false otherwise.
386  
387 =cut
388  
389 24 ▶sub is_valid_petscii_string {
Subroutine is_valid_petscii_string: called CC=5 SCAR=16.1
Pod documented
390 24 my ($text_string) = @_;
391  
392 24 ▶ return 0 unless defined $text_string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text_string 2 22 24
393 22 ▶ return 0 if ref $text_string;
Branch: 1 branch
BranchTrueFalseTotal
if ref $text_string 2 20 22
394  
395 20 ▶ return 1 if length $text_string == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $text_string == 0 2 18 20
396  
397 18 ▶ unless ($text_string =~ m/^[^\x00-\xff]*$/g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text_string =~ /^[^\x00-\xff]*$/g) 16 2 18
398 16 return 1;
399 }
400  
401 2 return 0;
402 }
403  
404 # TODO: Consider adding this method to the public interface of current package:
405 30 ▶sub _petscii_to_screen_code {
Subroutine _petscii_to_screen_code: called CC=16 SCAR=27.7
406 30 my ($num_petscii) = @_;
407 30 ▶ if ($num_petscii < 0x20 or $num_petscii > 0xff or ($num_petscii >= 0x80 and $num_petscii <= 0x9f)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_petscii < 32 or $num_petscii > 255 or $num_petscii >= 128 and $num_petscii <= 159) 2 28 30
Condition77%7/9
$num_petscii < 32 or $num_petscii > 255 or $num_petscii >= 128 and $num_petscii <= 159
l!l&&r!l&&!r
2028
$num_petscii < 32 or $num_petscii > 255
l!l&&r!l&&!r
1128
$num_petscii >= 128 and $num_petscii <= 159
!ll&&!rl&&r
1990
MC/DC50%2/4
$num_petscii < 32 or $num_petscii > 255 or $num_petscii >= 128 and $num_petscii <= 159
$num_petscii < 32 $num_petscii > 255 $num_petscii >= 128 $num_petscii <= 159
Truth table80%4/5
A or B or C and D
A= $num_petscii < 32B= $num_petscii > 255C= $num_petscii >= 128D= $num_petscii <= 159
ABCDresult
1XXX1
01XX1
00111
000X0
00100
408 2 croak sprintf q{Invalid PETSCII integer code: "0x%02x" (PETSCII character set supports printable characters in the range of 0x20 to 0x7f and 0xa0 to 0xff)}, $num_petscii;
409 }
410 28 my $screen_code = $num_petscii;
411 28 ▶ if ($num_petscii >= 64 && $num_petscii <= 95) {
Branch: 6 branches
BranchTrueFalseTotal
if ($num_petscii >= 64 and $num_petscii <= 95) { } 10 18 28
elsif ($num_petscii >= 96 and $num_petscii <= 127) { } 4 14 18
elsif ($num_petscii >= 160 and $num_petscii <= 191) { } 2 12 14
elsif ($num_petscii >= 192 and $num_petscii <= 223) { } 4 8 12
elsif ($num_petscii >= 224 and $num_petscii <= 254) { } 2 6 8
elsif ($num_petscii == 255) { } 1 5 6
Condition100%3/3
$num_petscii >= 64 and $num_petscii <= 95
!ll&&!rl&&r
51310
MC/DC100%2/2
$num_petscii >= 64 and $num_petscii <= 95
$num_petscii >= 64 $num_petscii <= 95
Truth table100%3/3
A and B
A= $num_petscii >= 64B= $num_petscii <= 95
ABresult
0X0
100
111
412 10 $screen_code -= 64;
413 }
414 ▶ elsif ($num_petscii >= 96 && $num_petscii <= 127) {
Condition100%3/3
$num_petscii >= 96 and $num_petscii <= 127
!ll&&!rl&&r
594
MC/DC100%2/2
$num_petscii >= 96 and $num_petscii <= 127
$num_petscii >= 96 $num_petscii <= 127
Truth table100%3/3
A and B
A= $num_petscii >= 96B= $num_petscii <= 127
ABresult
0X0
100
111
415 4 $screen_code -= 32;
416 }
417 ▶ elsif ($num_petscii >= 160 && $num_petscii <= 191) {
Condition100%3/3
$num_petscii >= 160 and $num_petscii <= 191
!ll&&!rl&&r
572
MC/DC100%2/2
$num_petscii >= 160 and $num_petscii <= 191
$num_petscii >= 160 $num_petscii <= 191
Truth table100%3/3
A and B
A= $num_petscii >= 160B= $num_petscii <= 191
ABresult
0X0
100
111
418 2 $screen_code -= 64;
419 }
420 ▶ elsif ($num_petscii >= 192 && $num_petscii <= 223) {
Condition100%3/3
$num_petscii >= 192 and $num_petscii <= 223
!ll&&!rl&&r
534
MC/DC100%2/2
$num_petscii >= 192 and $num_petscii <= 223
$num_petscii >= 192 $num_petscii <= 223
Truth table100%3/3
A and B
A= $num_petscii >= 192B= $num_petscii <= 223
ABresult
0X0
100
111
421 4 $screen_code -= 128;
422 }
423 ▶ elsif ($num_petscii >= 224 && $num_petscii <= 254) {
Condition100%3/3
$num_petscii >= 224 and $num_petscii <= 254
!ll&&!rl&&r
512
MC/DC100%2/2
$num_petscii >= 224 and $num_petscii <= 254
$num_petscii >= 224 $num_petscii <= 254
Truth table100%3/3
A and B
A= $num_petscii >= 224B= $num_petscii <= 254
ABresult
0X0
100
111
424 2 $screen_code -= 128;
425 }
426 elsif ($num_petscii == 255) {
427 1 $screen_code -= 161;
428 }
429 28 return $screen_code;
430 }
431  
432 =head1 BUGS
433  
434 There are no known bugs at the moment. Please report any bugs or feature requests.
435  
436 =head1 EXPORT
437  
438 No method is exported into the caller's namespace by default.
439  
440 Selected methods may be exported into the caller's namespace explicitly by using the following tags in the import list:
441  
442 =over
443  
444 =item *
445 C<convert> tag adds L</ascii_to_petscii> and L</petscii_to_ascii> subroutines to the list of symbols to be imported into the caller's namespace
446  
447 =item *
448 C<display> tag adds L</set_petscii_write_mode> and L</write_petscii_char> subroutines to the list of symbols to be imported into the caller's namespace
449  
450 =item *
451 C<screen> tag adds L</screen_codes_to_petscii> and </petscii_to_screen_codes> subroutines to the list of symbols to be imported into the caller's namespace
452  
453 =item *
454 C<validate> tag adds L</is_printable_petscii_string> and </is_valid_petscii_string> subroutines to the list of symbols to be imported into the caller's namespace
455  
456 =item *
457 C<all> tag adds all subroutines listed by C<convert>, C<display>, C<screen>, and C<validate> tags to the list of exported symbols
458  
459 =back
460  
461 =head1 AUTHOR
462  
463 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
464  
465 =head1 VERSION
466  
467 Version 0.05 (2013-03-08)
468  
469 =head1 COPYRIGHT AND LICENSE
470  
471 Copyright 2011, 2013 by Pawel Krol <pawelkrol@cpan.org>.
472  
473 This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
474  
475 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
476  
477 =cut
478  
479 3 1;
↑ Summary PETSCII.xs »