/* * Minimal bootstrap RTL (runtime library) - Writer (output) functions. * * Copyright © 2025 Samuel Lidén Borell * * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later */ #include #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); } }