blib/lib/FFI/Platypus/Type/WideString.pm

filter: Statementstmt 95.7% 90 / 94 Branchbran 75.0% 24 / 32 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 89.8% 133 / 148 CC 20 SCAR 15.3
CC
20
Cov
90%
_compute_wide_string_encoding
9.4
__ANON__
6.0
ffi_custom_type_api_1
5.0
CRAP 4.6
« blib/lib/FFI/Platypus/Lang/Win32.pm ↑ Summary lib/FFI/Build/Plugin.pm »
1 package FFI::Platypus::Type::WideString;
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 2 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use FFI::Platypus::Memory qw( memcpy );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use FFI::Platypus::Buffer qw( buffer_to_scalar scalar_to_pointer scalar_to_buffer );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode qw( decode encode find_encoding );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Platypus custom type for Unicode "wide" strings
13 2 our $VERSION = '2.12'; # VERSION
14  
15  
16 2 my @stack; # To keep buffer alive.
17  
18 sub _compute_wide_string_encoding
19 6 ▶{
Subroutine _compute_wide_string_encoding: called CC=8 SCAR=22.4
20 6 foreach my $need (qw( wcslen wcsnlen ))
21 {
22 12 ▶ die "This type plugin needs $need from libc, and cannot find it"
Branch: 1 branch
BranchTrueFalseTotal
unless "FFI::Platypus::Memory"->can("_$need") 0 12 12
23 unless FFI::Platypus::Memory->can("_$need");
24 }
25  
26 6 my $ffi = FFI::Platypus->new( api => 2, lib => [undef] );
27  
28 6 my $size = eval { $ffi->sizeof('wchar_t') };
29 6 ▶ die 'no wchar_t' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
30  
31 6 my %orders = (
32 join('', 1..$size) => 'BE',
33 join('', reverse 1..$size) => 'LE',
34 );
35  
36 6 my $byteorder = join '', @{ $ffi->cast( "wchar_t*", "uint8[$size]", \hex(join '', map { "0$_" } 1..$size) ) };
37  
38 6 my $encoding;
39  
40 6 ▶ if($size == 2)
Branch: 2 branches
BranchTrueFalseTotal
if ($size == 2) { } 0 6 6
elsif ($size == 4) { } 6 0 6
41 {
42 0 $encoding = 'UTF-16';
43 }
44 elsif($size == 4)
45 {
46 6 $encoding = 'UTF-32';
47 }
48 else
49 {
50 0 die "not sure what encoding to use for size $size";
51 }
52  
53 6 ▶ if(defined $orders{$byteorder})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orders{$byteorder}) { } 6 0 6
54 {
55 6 $encoding .= $orders{$byteorder};
56 }
57 else
58 {
59 0 die "odd byteorder $byteorder not (yet) supported";
60 }
61  
62 6 ▶ die "Perl doesn't recognize $encoding as an encoding"
Branch: 1 branch
BranchTrueFalseTotal
unless &find_encoding($encoding) 0 6 6
63 unless find_encoding($encoding);
64  
65 6 return ($encoding, $size);
66 }
67  
68 sub ffi_custom_type_api_1
69 4 ▶{
Subroutine ffi_custom_type_api_1: called CC=5 SCAR=16.1
Pod undocumented
70 4 my %args = @_;
71  
72 # TODO: it wold be nice to allow arbitrary encodings, but we are
73 # relying on a couple of wcs* functions to compute the string, so
74 # we will leave that for future development.
75 4 my($encoding, $width) = __PACKAGE__->_compute_wide_string_encoding();
76  
77 # it is hard to come up with a default size for write buffers
78 # but 2048 is a multiple of 1024 that is large enough to fit
79 # any Windows PATH (260*4)+2 = 1042
80 #
81 # (assuming all characters in the PATH are in the BMP, which is
82 # admitedly unlikely, possilby impossible (?) and and a null
83 # termination of two bytes).
84 #
85 # it is arbitrary and based on a platform specific windows
86 # thing, but windows is where wide strings are most likely
87 # to be found, so seems good as anything.
88 4 ▶ my $size = $args{size} || 2048;
Condition50%1/2
$args{'size'} || 2048
l!l
04
MC/DC0%0/1
$args{'size'} || 2048
$args{'size'}
Truth table50%1/2
A
A= $args{'size'}
Aresult
11
00
89 4 ▶ my $access = $args{access} || 'read';
Condition100%2/2
$args{'access'} || 'read'
l!l
31
MC/DC100%1/1
$args{'access'} || 'read'
$args{'access'}
Truth table100%2/2
A
A= $args{'access'}
Aresult
11
00
90  
91 4 my %ct = (
92 native_type => 'opaque',
93 );
94  
95 8 ▶ $ct{native_to_perl} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 8 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 7 8
97 7 return decode($encoding,
98 buffer_to_scalar(
99 $_[0],
100 FFI::Platypus::Memory::_wcslen($_[0])*$width,
101 )
102 );
103 4 };
104  
105 4 ▶ if($access eq 'read')
Branch: 2 branches
BranchTrueFalseTotal
if ($access eq 'read') { } 2 2 4
elsif ($access eq 'write') { } 2 0 2
106 {
107 28 ▶ $ct{perl_to_native} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
108 28 ▶ if(defined $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 27 1 28
109 {
110 27 my $buf = encode($encoding, $_[0]."\0");
111 27 push @stack, \$buf;
112 27 return scalar_to_pointer $buf;
113 }
114 else
115 {
116 1 push @stack, undef;
117 1 return undef;
118 }
119 2 };
120  
121 28 ▶ $ct{perl_to_native_post} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 28 pop @stack;
123 28 return;
124 2 };
125  
126 }
127 elsif($access eq 'write')
128 {
129 2 my @stack;
130  
131 17 ▶ $ct{perl_to_native} = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
132 17 my $ref = shift;
133 17 ▶ if(ref($ref) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'ARRAY') { } 8 9 17
elsif (ref $ref eq 'SCALAR') { } 8 1 9
134 {
135 8 ▶ ${ $ref->[0] } = "\0" x $size unless defined ${ $ref->[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$ref[0];} 4 4 8
136 8 my $ptr = scalar_to_pointer ${ $ref->[0] };
137 8 ▶ if(defined $ref->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref->[0]) 8 0 8
138 {
139 8 my $init = encode($encoding, $ref->[1]);
140 8 my($sptr, $ssize) = scalar_to_buffer($init);
141 8 memcpy($ptr, $sptr, $ssize);
142 }
143 8 push @stack, \${ $ref->[0] };
144 8 return $ptr;
145 }
146 elsif(ref($ref) eq 'SCALAR')
147 {
148 8 push @stack, $ref;
149 8 ▶ $$ref = "\0" x $size unless defined $$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$ref 4 4 8
150 8 return scalar_to_pointer $$ref;
151 }
152 else
153 {
154 1 push @stack, $ref;
155 1 return undef;
156 }
157 2 };
158  
159 17 ▶ $ct{perl_to_native_post} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
160 17 my $ref = pop @stack;
161 17 ▶ return unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 1 16 17
162 16 my $len = length $$ref;
163 16 $len = FFI::Platypus::Memory::_wcsnlen($$ref, $len);
164 16 $$ref = decode($encoding, substr($$ref, 0, $len*$width));
165 2 };
166  
167 }
168 else
169 {
170 0 Carp::croak("Unknown access type $access");
171 }
172  
173 4 return \%ct;
174 }
175  
176 2 1;
177  
178 __END__
« blib/lib/FFI/Platypus/Lang/Win32.pm ↑ Summary lib/FFI/Build/Plugin.pm »