Fun
0.41.5
The programming language that makes You have fun
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
io
write_file.c
Go to the documentation of this file.
1
/*
2
* This file is part of the Fun programming language.
3
* https://fun-lang.xyz/
4
*
5
* Copyright 2025 Johannes Findeisen <you@hanez.org>
6
* Licensed under the terms of the Apache-2.0 license.
7
* https://opensource.org/license/apache-2-0
8
*/
9
30
31
case
OP_WRITE_FILE
: {
32
Value
data = pop_value(vm);
33
Value
path
= pop_value(vm);
34
if
(
path
.type !=
VAL_STRING
|| data.
type
!=
VAL_STRING
) {
35
fprintf
(stderr,
"WRITE_FILE expects (string, string)\n"
);
36
exit
(1);
37
}
38
const
char
*
p
=
path
.s ?
path
.s :
""
;
39
FILE *
f
= fopen(
p
,
"wb"
);
40
int
ok
= 0;
41
if
(
f
) {
42
size_t
len
= data.
s
? strlen(data.
s
) : 0;
43
ok
= (fwrite(data.
s
? data.
s
:
""
, 1,
len
,
f
) ==
len
);
44
fclose
(
f
);
45
}
46
free_value
(
path
);
47
free_value
(data);
48
push_value
(vm,
make_int
(
ok
? 1 : 0));
49
break
;
50
}
OP_WRITE_FILE
@ OP_WRITE_FILE
Definition
bytecode.h:137
ok
int ok
Definition
contains.c:38
len
size_t len
Definition
input_line.c:102
fclose
fclose(f)
f
FILE * f
Definition
read_file.c:38
p
const char * p
Definition
read_file.c:37
Value
Tagged union representing a Fun value.
Definition
value.h:68
Value::type
ValueType type
Definition
value.h:69
Value::s
char * s
Definition
value.h:73
make_int
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition
value.c:51
VAL_STRING
@ VAL_STRING
Definition
value.h:53
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
push_value
push_value(vm, make_int(ok ? 1 :0))
path
Value path
Definition
write_file.c:33
free_value
free_value(path)
Generated on
for Fun by
1.16.1