mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
add more IO functions
This commit is contained in:
parent
3b58f280f7
commit
51bc382413
10
README.md
10
README.md
@ -200,8 +200,14 @@ Name | Example | Example result | Description
|
|||||||
wd | `(wd)` | `"/home/pekka/code/madlisp/"` | Get the current working directory.
|
wd | `(wd)` | `"/home/pekka/code/madlisp/"` | Get the current working directory.
|
||||||
chdir | `(chdir "/tmp")` | `true` | Change the current working directory.
|
chdir | `(chdir "/tmp")` | `true` | Change the current working directory.
|
||||||
file? | `(file? "test.txt")` | `true` | Return true if the file exists.
|
file? | `(file? "test.txt")` | `true` | Return true if the file exists.
|
||||||
fread | `(fread "test.txt")` | `"content"` | Read the contents of a file.
|
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).
|
||||||
fwrite | `(fwrite "test.txt" "content")` | `true` | Write string to file. Give optional third parameter as `true` to append.
|
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"))` | `<resource>` | 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
|
### Json functions
|
||||||
|
|
||||||
|
@ -23,13 +23,13 @@ class IO implements ILib
|
|||||||
fn (string $filename) => file_exists($filename)
|
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) {
|
function (string $filename) {
|
||||||
return @file_get_contents($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) {
|
function (string $filename, $data, $append = false) {
|
||||||
$flags = 0;
|
$flags = 0;
|
||||||
if ($append) {
|
if ($append) {
|
||||||
@ -41,5 +41,29 @@ class IO implements ILib
|
|||||||
return $result !== false;
|
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)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user