| 1 |
package FFI::Platypus::Type::StringPointer; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use FFI::Platypus; |
| 7 |
use Scalar::Util qw( readonly ); |
| 8 |
|
| 9 |
# ABSTRACT: Convert a pointer to a string and back |
| 10 |
our $VERSION = '2.12'; # VERSION |
| 11 |
|
| 12 |
|
| 13 |
use constant _incantation => |
| 14 |
$^O eq 'MSWin32' && do { require Config; $Config::Config{archname} =~ /MSWin32-x64/ } |
| 15 |
? 'Q' |
| 16 |
: 'L!'; |
| 17 |
use constant _pointer_buffer => "P" . FFI::Platypus->new( api => 2 )->sizeof('opaque'); |
| 18 |
|
| 19 |
my @stack; |
| 20 |
|
| 21 |
sub perl_to_native |
| 22 |
{ |
| 23 |
if(defined $_[0]) |
| 24 |
{ |
| 25 |
my $packed = pack 'P', ${$_[0]}; |
| 26 |
my $pointer_pointer = pack 'P', $packed; |
| 27 |
my $unpacked = unpack _incantation, $pointer_pointer; |
| 28 |
push @stack, [ \$packed, \$pointer_pointer ]; |
| 29 |
return $unpacked; |
| 30 |
} |
| 31 |
else |
| 32 |
{ |
| 33 |
push @stack, []; |
| 34 |
return undef; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
sub perl_to_native_post |
| 39 |
{ |
| 40 |
my($packed) = @{ pop @stack }; |
| 41 |
return unless defined $packed; |
| 42 |
unless(readonly(${$_[0]})) |
| 43 |
{ |
| 44 |
${$_[0]} = unpack 'p', $$packed; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
sub native_to_perl |
| 49 |
{ |
| 50 |
return unless defined $_[0]; |
| 51 |
my $pointer_pointer = unpack(_incantation, unpack(_pointer_buffer, pack(_incantation, $_[0]))); |
| 52 |
$pointer_pointer ? \unpack('p', pack(_incantation, $pointer_pointer)) : \undef; |
| 53 |
} |
| 54 |
|
| 55 |
sub ffi_custom_type_api_1 |
| 56 |
{ |
| 57 |
return { |
| 58 |
native_type => 'opaque', |
| 59 |
perl_to_native => \&perl_to_native, |
| 60 |
perl_to_native_post => \&perl_to_native_post, |
| 61 |
native_to_perl => \&native_to_perl, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
1; |
| 66 |
|
| 67 |
__END__ |
| 68 |
|
| 69 |
=pod |
| 70 |
|
| 71 |
=encoding UTF-8 |
| 72 |
|
| 73 |
=head1 NAME |
| 74 |
|
| 75 |
FFI::Platypus::Type::StringPointer - Convert a pointer to a string and back |
| 76 |
|
| 77 |
=head1 VERSION |
| 78 |
|
| 79 |
version 2.12 |
| 80 |
|
| 81 |
=head1 SYNOPSIS |
| 82 |
|
| 83 |
In your C code: |
| 84 |
|
| 85 |
void |
| 86 |
string_pointer_argument(const char **string) |
| 87 |
{ |
| 88 |
... |
| 89 |
} |
| 90 |
const char ** |
| 91 |
string_pointer_return(void) |
| 92 |
{ |
| 93 |
... |
| 94 |
} |
| 95 |
|
| 96 |
In your Platypus::FFI code: |
| 97 |
|
| 98 |
use FFI::Platypus 2.00; |
| 99 |
|
| 100 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 101 |
$ffi->load_custom_type('::StringPointer' => 'string_pointer'); |
| 102 |
|
| 103 |
$ffi->attach(string_pointer_argument => ['string_pointer'] => 'void'); |
| 104 |
$ffi->attach(string_pointer_return => [] => 'string_pointer'); |
| 105 |
|
| 106 |
my $string = "foo"; |
| 107 |
|
| 108 |
string_pointer_argument(\$string); # $string may be modified |
| 109 |
|
| 110 |
$ref = string_pointer_return(); |
| 111 |
|
| 112 |
print $$ref; # print the string pointed to by $ref |
| 113 |
|
| 114 |
=head1 DESCRIPTION |
| 115 |
|
| 116 |
B<NOTE>: As of version 0.61, this custom type is now deprecated since |
| 117 |
pointers to strings are supported in the L<FFI::Platypus> directly |
| 118 |
without custom types. |
| 119 |
|
| 120 |
This module provides a L<FFI::Platypus> custom type for pointers to |
| 121 |
strings. |
| 122 |
|
| 123 |
=head1 SEE ALSO |
| 124 |
|
| 125 |
=over 4 |
| 126 |
|
| 127 |
=item L<FFI::Platypus> |
| 128 |
|
| 129 |
Main Platypus documentation. |
| 130 |
|
| 131 |
=item L<FFI::Platypus::Type> |
| 132 |
|
| 133 |
Platypus types documentation. |
| 134 |
|
| 135 |
=back |
| 136 |
|
| 137 |
=head1 AUTHOR |
| 138 |
|
| 139 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 140 |
|
| 141 |
Contributors: |
| 142 |
|
| 143 |
Bakkiaraj Murugesan (bakkiaraj) |
| 144 |
|
| 145 |
Dylan Cali (calid) |
| 146 |
|
| 147 |
pipcet |
| 148 |
|
| 149 |
Zaki Mughal (zmughal) |
| 150 |
|
| 151 |
Fitz Elliott (felliott) |
| 152 |
|
| 153 |
Vickenty Fesunov (vyf) |
| 154 |
|
| 155 |
Gregor Herrmann (gregoa) |
| 156 |
|
| 157 |
Shlomi Fish (shlomif) |
| 158 |
|
| 159 |
Damyan Ivanov |
| 160 |
|
| 161 |
Ilya Pavlov (Ilya33) |
| 162 |
|
| 163 |
Petr Písař (ppisar) |
| 164 |
|
| 165 |
Mohammad S Anwar (MANWAR) |
| 166 |
|
| 167 |
Håkon Hægland (hakonhagland, HAKONH) |
| 168 |
|
| 169 |
Meredith (merrilymeredith, MHOWARD) |
| 170 |
|
| 171 |
Diab Jerius (DJERIUS) |
| 172 |
|
| 173 |
Eric Brine (IKEGAMI) |
| 174 |
|
| 175 |
szTheory |
| 176 |
|
| 177 |
José Joaquín Atria (JJATRIA) |
| 178 |
|
| 179 |
Pete Houston (openstrike, HOUSTON) |
| 180 |
|
| 181 |
Lukas Mai (MAUKE) |
| 182 |
|
| 183 |
=head1 COPYRIGHT AND LICENSE |
| 184 |
|
| 185 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 186 |
|
| 187 |
This is free software; you can redistribute it and/or modify it under |
| 188 |
the same terms as the Perl 5 programming language system itself. |
| 189 |
|
| 190 |
=cut |