diff --git a/src/Lib/IO.php b/src/Lib/IO.php index 09ba36c..47d1641 100644 --- a/src/Lib/IO.php +++ b/src/Lib/IO.php @@ -9,6 +9,7 @@ namespace MadLisp\Lib; use MadLisp\CoreFunc; use MadLisp\Env; +use MadLisp\Vector; class IO implements ILib { @@ -25,10 +26,82 @@ class IO implements ILib fn (string $dir) => chdir($dir) )); - $env->set('file?', new CoreFunc('file?', 'Check if file exists.', 1, 1, + $env->set('file?', new CoreFunc('file?', 'Check if file (or directory) exists.', 1, 1, fn (string $filename) => file_exists($filename) )); + $env->set('dir?', new CoreFunc('dir?', 'Check if directory exists and is not a file.', 1, 1, + fn (string $dir) => is_dir($dir) + )); + + $env->set('fsize', new CoreFunc('fsize', 'Return the size of a file.', 1, 1, + fn (string $file) => @filesize($file) + )); + + $env->set('ftime', new CoreFunc('ftime', 'Return the last modification time of a file.', 1, 1, + fn (string $file) => @filemtime($file) + )); + + $env->set('ftouch', new CoreFunc('ftouch', 'Set the modification time of a file to the given value.', 1, 2, + fn (string $file, ?int $time = null) => @touch($file, $time !== null ? $time : time()) + )); + + $env->set('fperms', new CoreFunc('fperms', 'Get the permissions of a file. Permission bits are 1=execute, 2=write, 4=read.', 1, 1, + function (string $file) { + $perms = @fileperms($file); + + if ($perms === false) { + return null; + } else { + return substr(sprintf('%o', $perms), -4); + } + } + )); + + $env->set('fmod', new CoreFunc('fmod', 'Set the permissions of a file. Permission bits are 1=execute, 2=write, 4=read.', 2, 2, + fn (string $file, string $perms) => @chmod($file, octdec($perms)) + )); + + $env->set('fown', new CoreFunc('fown', 'Get the owner of a file. Give second argument as true to get the user ID.', 1, 2, + function (string $file, bool $rawId = false) { + $id = @fileowner($file); + + if ($id === false) { + return null; + } elseif ($rawId) { + return $id; + } else { + $info = posix_getpwuid($id); + return $info['name']; + } + } + )); + + $env->set('fgrp', new CoreFunc('fgrp', 'Get the group of a file. Give second argument as true to get the group ID.', 1, 2, + function (string $file, bool $rawId = false) { + $id = @filegroup($file); + + if ($id === false) { + return null; + } elseif ($rawId) { + return $id; + } else { + $info = posix_getgrgid($id); + return $info['name']; + } + } + )); + + $env->set('fcache', new CoreFunc('fcache', 'Clear the cache of file information.', 0, 0, + fn () => clearstatcache() + )); + + $env->set('fdel', new CoreFunc('fdel', 'Delete a file.', 1, 1, + fn (string $file) => @unlink($file) + )); + + // Simple read/write using file_get_contents and file_put_contents + $env->set('fget', new CoreFunc('fget', 'Read contents of a file.', 1, 1, function (string $filename) { return @file_get_contents($filename); @@ -48,6 +121,8 @@ class IO implements ILib } )); + // Functions for working with file descriptors + $env->set('fopen', new CoreFunc('fopen', 'Open a file for reading or writing. Give mode as second argument.', 2, 2, fn ($file, $mode) => @fopen($file, $mode) )); @@ -72,6 +147,32 @@ class IO implements ILib fn ($handle) => @feof($handle) )); + // Functions for listing files + + $env->set('glob', new CoreFunc('glob', 'Search for files and directories that match a pattern.', 1, 1, + function (string $pattern) { + $result = @glob($pattern); + + if ($result === false) { + return null; + } else { + return new Vector($result); + } + } + )); + + $env->set('read-dir', new CoreFunc('read-dir', 'Read the contents of a directory.', 1, 1, + function (string $dir) { + $result = @scandir($dir); + + if ($result === false) { + return null; + } else { + return new Vector($result); + } + } + )); + // Readline support if (extension_loaded('readline')) { $env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1,