Tons of refactoring and some fixes and enhancements... ;)
This commit is contained in:
parent
d1bbd2e501
commit
13919ad87d
82 changed files with 1775 additions and 1382 deletions
31
src/builtins_io.c
Normal file
31
src/builtins_io.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* File I/O helpers: return malloc'd buffers or status ints */
|
||||
|
||||
char *bio_read_file_strdup(const char *path) {
|
||||
if (!path) return strdup("");
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return strdup("");
|
||||
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return strdup(""); }
|
||||
long sz = ftell(f);
|
||||
if (sz < 0) { fclose(f); return strdup(""); }
|
||||
rewind(f);
|
||||
char *buf = (char*)malloc((size_t)sz + 1);
|
||||
if (!buf) { fclose(f); return strdup(""); }
|
||||
size_t n = fread(buf, 1, (size_t)sz, f);
|
||||
fclose(f);
|
||||
buf[n] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
int bio_write_file(const char *path, const char *data, size_t len) {
|
||||
if (!path) return 0;
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return 0;
|
||||
if (!data) data = "";
|
||||
int ok = (fwrite(data, 1, len, f) == len);
|
||||
fclose(f);
|
||||
return ok;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue