From 51bc3824139ebf5e825286bf8bdfab5c9fedd6d8 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Fri, 19 Jun 2020 14:13:21 +0700 Subject: [PATCH] add more IO functions --- README.md | 10 ++++++++-- src/Lib/IO.php | 28 ++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1cabbf1..6c470d2 100644 --- a/README.md +++ b/README.md @@ -200,8 +200,14 @@ Name | Example | Example result | Description wd | `(wd)` | `"/home/pekka/code/madlisp/"` | Get the current working directory. chdir | `(chdir "/tmp")` | `true` | Change the current working directory. file? | `(file? "test.txt")` | `true` | Return true if the file exists. -fread | `(fread "test.txt")` | `"content"` | Read the contents of a file. -fwrite | `(fwrite "test.txt" "content")` | `true` | Write string to file. Give optional third parameter as `true` to append. +fget | `(fget "test.txt")` | `"content"` | Read the contents of a file using [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php). +fput | `(fput "test.txt" "content")` | `true` | Write string to file using [file_put_contents](https://www.php.net/manual/en/function.file-put-contents.php). Give optional third parameter as `true` to append. +fopen | `(def f (fopen "test.txt" "w"))` | `` | Open a file for reading or writing. Give the mode as second argument. +fclose | `(fclose f)` | `true` | Close a file resource. +fwrite | `(fwrite f "abc")` | `3` | Write to a file resource. +fflush | `(fflush f)` | `true` | Persist buffered writes to disk for a file resource. +fread | `(fread f 16)` | `"abc"` | Read from a file resource. +feof? | `(feof? f)` | `true` | Return true if end of file has been reached for a file resource. ### Json functions diff --git a/src/Lib/IO.php b/src/Lib/IO.php index e15859e..f38d6bb 100644 --- a/src/Lib/IO.php +++ b/src/Lib/IO.php @@ -23,13 +23,13 @@ class IO implements ILib fn (string $filename) => file_exists($filename) )); - $env->set('fread', new CoreFunc('fread', 'Read contents of a file.', 1, 1, + $env->set('fget', new CoreFunc('fget', 'Read contents of a file.', 1, 1, function (string $filename) { return @file_get_contents($filename); } )); - $env->set('fwrite', new CoreFunc('fwrite', 'Write string (second argument) to file (first argument). Give true as optional third argument to append instead of overwrite.', 2, 3, + $env->set('fput', new CoreFunc('fput', 'Write string (second argument) to file (first argument). Give true as optional third argument to append instead of overwrite.', 2, 3, function (string $filename, $data, $append = false) { $flags = 0; if ($append) { @@ -41,5 +41,29 @@ class IO implements ILib return $result !== false; } )); + + $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) + )); + + $env->set('fclose', new CoreFunc('fclose', 'Close a file resource.', 1, 1, + fn ($handle) => @fclose($handle) + )); + + $env->set('fread', new CoreFunc('fread', 'Read from a file resource. Give length in bytes as second argument.', 2, 2, + fn ($handle, $length) => @fread($handle, $length) + )); + + $env->set('fwrite', new CoreFunc('fwrite', 'Write to a file resource.', 2, 2, + fn ($handle, $data) => @fwrite($handle, $data) + )); + + $env->set('fflush', new CoreFunc('fflush', 'Persist buffered writes to disk for a file resource.', 1, 1, + fn ($handle) => @fflush($handle) + )); + + $env->set('feof?', new CoreFunc('feof?', 'Return true if end of file has been reached for a file resource.', 1, 1, + fn ($handle) => @feof($handle) + )); } }