Commit 61c4e3b0

61c4e3b0ef23Merged

ydah <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

style(runtime): split file io helpers

Details

Commit
61c4e3b0ef23
Parent
c4d5f9e267d2

lib/sp_runtime.hmodifiedView file

@@ -1743,8 +1743,35 @@
17431743 and `\r\n` becomes `\r\r\n`. fread's actual byte count drives
17441744 null-termination because text mode shrinks the byte count below
17451745 ftell's raw-file size. */
1746-static const char *sp_file_read(const char *path) { if (sp_file_directory(path)) sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path)); FILE *f = fopen(path, "r"); if (!f) return &("\xff" "")[1]; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = sp_str_alloc(sz); size_t n = 0; if (sz > 0) { n = fread(buf, 1, sz, f); } buf[n] = 0; fclose(f); return buf; }
1747-static void sp_file_write(const char *path, const char *data) { if (sp_file_directory(path)) sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ rb_sysopen - %s", path)); FILE *f = fopen(path, "wb"); if (f) { fwrite(data, 1, sp_str_byte_len(data), f); fclose(f); } }
1746+static const char *sp_file_read(const char *path) {
1747+ if (sp_file_directory(path)) {
1748+ sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path));
1749+ }
1750+ FILE *f = fopen(path, "r");
1751+ if (!f) return &("\xff" "")[1];
1752+ fseek(f, 0, SEEK_END);
1753+ long sz = ftell(f);
1754+ fseek(f, 0, SEEK_SET);
1755+ char *buf = sp_str_alloc(sz);
1756+ size_t n = 0;
1757+ if (sz > 0) {
1758+ n = fread(buf, 1, sz, f);
1759+ }
1760+ buf[n] = 0;
1761+ fclose(f);
1762+ return buf;
1763+}
1764+
1765+static void sp_file_write(const char *path, const char *data) {
1766+ if (sp_file_directory(path)) {
1767+ sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ rb_sysopen - %s", path));
1768+ }
1769+ FILE *f = fopen(path, "wb");
1770+ if (f) {
1771+ fwrite(data, 1, sp_str_byte_len(data), f);
1772+ fclose(f);
1773+ }
1774+}
17481775 static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }
17491776 static void sp_file_delete(const char *path) { remove(path); }
17501777 static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }
@@ -1767,7 +1794,9 @@
17671794 null-termination and stops at the first 0x00 byte — wrong for
17681795 binary data (e.g. .nes ROM files). */
17691796 static sp_IntArray *sp_file_binread_bytes(const char *path) {
1770- if (sp_file_directory(path)) sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path));
1797+ if (sp_file_directory(path)) {
1798+ sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path));
1799+ }
17711800 FILE *f = fopen(path, "rb");
17721801 sp_IntArray *a = sp_IntArray_new();
17731802 if (!f) return a;