Files
damus/nostrdb/src/io.h
William Casarin 1ffbd80c67 nostrdb: move everything to src
Signed-off-by: William Casarin <jb55@jb55.com>
2025-08-11 16:39:43 -07:00

49 lines
789 B
C

#include <stdio.h>
static int read_fd(FILE *fd, unsigned char *buf, int buflen, int *written)
{
unsigned char *p = buf;
int len = 0;
*written = 0;
do {
len = fread(p, 1, 4096, fd);
*written += len;
p += len;
if (p > buf + buflen)
return 0;
} while (len == 4096);
return 1;
}
static int write_file(const char *filename, unsigned char *buf, int buflen)
{
FILE *file = NULL;
int ok;
file = fopen(filename, "w");
if (file == NULL)
return 0;
ok = fwrite(buf, buflen, 1, file);
fclose(file);
return ok;
}
static int read_file(const char *filename, unsigned char *buf, int buflen, int *written)
{
FILE *file = NULL;
int ok;
file = fopen(filename, "r");
if (file == NULL)
return 1;
ok = read_fd(file, buf, buflen, written);
fclose(file);
return ok;
}