Added ungetchString function
This commit is contained in:
parent
47a4525b60
commit
98d4ec011f
|
@ -31,13 +31,13 @@ final class Getch
|
||||||
if (self::$ffi === null) {
|
if (self::$ffi === null) {
|
||||||
$osFamily = PHP_OS_FAMILY;
|
$osFamily = PHP_OS_FAMILY;
|
||||||
if ($osFamily === 'Windows') {
|
if ($osFamily === 'Windows') {
|
||||||
self::$ffi = FFI::cdef('char _getch();', self::WINDOWS_LIBRARY);
|
self::$ffi = FFI::cdef('char _getch(); int _ungetch(char c);', self::WINDOWS_LIBRARY);
|
||||||
} elseif ($osFamily === 'Linux') {
|
} elseif ($osFamily === 'Linux') {
|
||||||
if (!file_exists($linuxLibrary)) {
|
if (!file_exists($linuxLibrary)) {
|
||||||
throw new RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary));
|
throw new RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary));
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$ffi = FFI::cdef('char _getch(); int _ungetch(int ch);', $linuxLibrary);
|
self::$ffi = FFI::cdef('char _getch(); int _ungetch(char ch);', $linuxLibrary);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
|
throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,21 @@ final class Getch
|
||||||
|
|
||||||
public function ungetch(string $char)
|
public function ungetch(string $char)
|
||||||
{
|
{
|
||||||
return self::$ffi->_ungetch($char);
|
return self::$ffi->_ungetch($char[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ungetchString(string $string): bool
|
||||||
|
{
|
||||||
|
$stringReverse = \strrev($string);
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
foreach(\str_split($stringReverse) as $char) {
|
||||||
|
$result = self::$ffi->_ungetch($char) > 0;
|
||||||
|
if(!$result) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ int _getch(void)
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _ungetch(int ch)
|
int _ungetch(char ch)
|
||||||
{
|
{
|
||||||
return ungetc(ch, stdin);
|
return ungetc(ch, stdin);
|
||||||
}
|
}
|
|
@ -28,3 +28,12 @@ if(!function_exists('ungetch')) {
|
||||||
return $g->ungetch($char);
|
return $g->ungetch($char);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!function_exists('ungetchString')) {
|
||||||
|
function ungetchString(string $string, string $linuxLibrary = null): bool
|
||||||
|
{
|
||||||
|
$g = new Getch($linuxLibrary);
|
||||||
|
|
||||||
|
return $g->ungetchString($string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue