mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 19:19:53 +00:00
commit
d0570b55b1
@ -136,6 +136,15 @@ static Janet cfun_io_popen(int32_t argc, Janet *argv) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static Janet cfun_io_temp(int32_t argc, Janet *argv) {
|
||||||
|
(void)argv;
|
||||||
|
janet_fixarity(argc, 0);
|
||||||
|
FILE *tmp = tmpfile();
|
||||||
|
if (!tmp)
|
||||||
|
janet_panicf("unable to create temporary file - %s", strerror(errno));
|
||||||
|
return janet_makefile(tmp, JANET_FILE_WRITE|JANET_FILE_READ|JANET_FILE_BINARY);
|
||||||
|
}
|
||||||
|
|
||||||
static Janet cfun_io_fopen(int32_t argc, Janet *argv) {
|
static Janet cfun_io_fopen(int32_t argc, Janet *argv) {
|
||||||
janet_arity(argc, 1, 2);
|
janet_arity(argc, 1, 2);
|
||||||
const uint8_t *fname = janet_getstring(argv, 0);
|
const uint8_t *fname = janet_getstring(argv, 0);
|
||||||
@ -566,6 +575,12 @@ static const JanetReg io_cfuns[] = {
|
|||||||
"eprinf", cfun_io_eprinf,
|
"eprinf", cfun_io_eprinf,
|
||||||
JDOC("(eprinf fmt & xs)\n\n"
|
JDOC("(eprinf fmt & xs)\n\n"
|
||||||
"Like eprintf but with no trailing newline.")
|
"Like eprintf but with no trailing newline.")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file/temp", cfun_io_temp,
|
||||||
|
JDOC("(file/temp)\n\n"
|
||||||
|
"Open an anonymous temporary file that is removed on close."
|
||||||
|
"Raises an error on failure.")
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"file/open", cfun_io_fopen,
|
"file/open", cfun_io_fopen,
|
||||||
|
@ -277,4 +277,10 @@
|
|||||||
|
|
||||||
(assert (= (constantly) (constantly)) "comptime 1")
|
(assert (= (constantly) (constantly)) "comptime 1")
|
||||||
|
|
||||||
|
(with [f (file/temp)]
|
||||||
|
(file/write f "foo\n")
|
||||||
|
(file/flush f)
|
||||||
|
(file/seek f :set 0)
|
||||||
|
(assert (= (string (file/read f :all)) "foo\n") "temp files work"))
|
||||||
|
|
||||||
(end-suite)
|
(end-suite)
|
||||||
|
Loading…
Reference in New Issue
Block a user