Skip to content

Commit

Permalink
added to char** - ffi_char_variadic() function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Mar 12, 2023
1 parent 199e9e7 commit 6cf5fd2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ function ffi_char(string $string, bool $owned = false, bool $persistent = false)
return $ptr;
}

/**
* Convert PHP array of `string` to `char**` _string_.
*
* @param string $string
* @return CData __char**__
*/
function ffi_char_variadic(string ...$string): CData
{
$n = 0;
$string_args = \FFI::new('char*[' . (\count($string) + 2) . ']', false);
foreach ($string as $value) {
$string_args[$n] = \ffi_char($value);
$n++;
}
$string_args[$n] = NULL;

return \ze_cast('char**', $string_args);
}

/**
* Creates a `char` C data structure of size.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/900-misc_general_c.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var_dump($cc->value());
$cc->free();
$ct = c_array_type('zend_property_info_list', 'ze', 3, false);
var_dump($ct);

$commands = ffi_char_variadic('php', 'test.php', '2');
var_dump($commands);
var_dump(ffi_string($commands[0]));
var_dump(ffi_string($commands[1]));
var_dump(ffi_string($commands[2]));

--EXPECTF--
Original - 5208
int(22548)
Expand Down Expand Up @@ -123,3 +130,13 @@ object(CStruct)#%d (1) {
["type"]=>
string(21) "struct <anonymous>[3]"
}
object(FFI\CData:char**)#%d (1) {
[0]=>
object(FFI\CData:char*)#%d (1) {
[0]=>
string(1) "p"
}
}
string(3) "php"
string(8) "test.php"
string(1) "2"

0 comments on commit 6cf5fd2

Please sign in to comment.