1#include "include/pw.h"
 2#include "include/pwlib/file.h"
 3
 4[[nodiscard]] bool pw_write_text_file(PwValuePtr filename, PwValuePtr content)
 5{
 6    PwValue f = PW_NULL;
 7    if (!pw_file_open_unbuffered(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666, &f)) {
 8        return false;
 9    }
10    char buffer[4096];
11    unsigned size = 0;
12    PwStringIter iter;
13    _pw_string_iter(content, &iter);
14    char32_t chr;
15    while (_pw_string_iter_next(&iter, &chr)) {
16        if (size >= sizeof(buffer) - 4) {
17            if (!pw_write(&f, buffer, size, nullptr)) {
18                pw_close(&f);
19                return false;
20            }
21            size = 0;
22        }
23        size += pw_char32_to_utf8(chr, &buffer[size]);
24    }
25    if (size) {
26        if (!pw_write(&f, buffer, size, nullptr)) {
27            pw_close(&f);
28            return false;
29        }
30    }
31    pw_close(&f);
32    return true;
33}