-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFIExtended.php
More file actions
52 lines (37 loc) · 1.01 KB
/
FFIExtended.php
File metadata and controls
52 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Fawno\GhostscriptAPI;
use FFI;
use FFI\CData;
class FFIExtended {
protected FFI $ffi;
protected function argsPtr(array $argv): ?CData {
$argv = array_values($argv);
$argc = count($argv);
if (!$argc) {
return null;
}
$p = $this->ffi->new("char *[$argc]", false);
foreach ($argv as $i => $arg) {
$p[$i] = self::strToCharPtr($arg);
}
$a = FFI::addr($p);
return $this->ffi->cast('char**', $a);
}
protected function strToCharPtr(string $string): CData {
$charArr = self::strToCharArr($string);
return $this->ffi->cast('char*', FFI::addr($charArr));
}
protected function strToCharArr(string $string): CData {
$string = preg_replace('~[\x00]+$~', "\x00", $string . "\x00");
$len = strlen($string);
if (!$len) {
return $this->ffi->new('char', false);
}
$charArr = $this->ffi->new("char[$len]", false);
foreach ($charArr as $i => $char) {
$charArr[$i]->cdata = $string[$i];
}
return $charArr;
}
}