Added ungetchString function

This commit is contained in:
R. Eric Wheeler 2021-01-07 14:13:05 -08:00
parent 47a4525b60
commit 98d4ec011f
3 changed files with 28 additions and 4 deletions

View File

@ -31,13 +31,13 @@ final class Getch
if (self::$ffi === null) {
$osFamily = PHP_OS_FAMILY;
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') {
if (!file_exists($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 {
throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
}
@ -51,6 +51,21 @@ final class Getch
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;
}
}

View File

@ -16,7 +16,7 @@ int _getch(void)
return ch;
}
int _ungetch(int ch)
int _ungetch(char ch)
{
return ungetc(ch, stdin);
}

View File

@ -28,3 +28,12 @@ if(!function_exists('ungetch')) {
return $g->ungetch($char);
}
}
if(!function_exists('ungetchString')) {
function ungetchString(string $string, string $linuxLibrary = null): bool
{
$g = new Getch($linuxLibrary);
return $g->ungetchString($string);
}
}