add some i/o functions for working with files

This commit is contained in:
Pekka Laiho 2020-12-21 11:14:49 +07:00
parent c9a095a857
commit c38d29d805

View File

@ -9,6 +9,7 @@ namespace MadLisp\Lib;
use MadLisp\CoreFunc; use MadLisp\CoreFunc;
use MadLisp\Env; use MadLisp\Env;
use MadLisp\Vector;
class IO implements ILib class IO implements ILib
{ {
@ -25,10 +26,82 @@ class IO implements ILib
fn (string $dir) => chdir($dir) 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) 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, $env->set('fget', new CoreFunc('fget', 'Read contents of a file.', 1, 1,
function (string $filename) { function (string $filename) {
return @file_get_contents($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, $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) fn ($file, $mode) => @fopen($file, $mode)
)); ));
@ -72,6 +147,32 @@ class IO implements ILib
fn ($handle) => @feof($handle) 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 // Readline support
if (extension_loaded('readline')) { if (extension_loaded('readline')) {
$env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1, $env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1,