blob: b835f0b2ce818f9554002b188821d4ecc047e8ad (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/*
* Minimal bootstrap RTL (runtime library) - Writer (output) functions.
*
* Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
*
* SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
*/
#include <assert.h>
#include "rtl.h"
#include "internal.h"
struct Writer {
FILE *file;
/**
* Filename for deleting file on error (TODO) and for error messages.
* Note that the filename should NOT be exposed to the application.
*/
const char *filename;
};
static struct Writer *Writer__from_file(FILE *f, const char *filename)
{
struct Writer *wr = malloc(sizeof(struct Writer));
OOM_CHECK(wr);
wr->file = f;
wr->filename = filename;
return wr;
}
struct Writer *Writer__from_filename(const char *filename)
{
FILE *f;
f = fopen(filename, "wb");
if (!f) {
perror(filename);
exit(EXIT_FAILURE);
}
return Writer__from_file(f, filename);
}
void Writer_write_str(
struct Writer *wr,
const struct String *text)
{
SlulInt len;
const unsigned char *data = SLUL__decode_string(text, &len);
if (fwrite(data, len, 1, wr->file) != 1) {
perror(wr->filename);
exit(EXIT_FAILURE);
}
}
|