nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

io.h (789B)


      1 
      2 #include <stdio.h>
      3 
      4 static int read_fd(FILE *fd, unsigned char *buf, int buflen, int *written)
      5 {
      6 	unsigned char *p = buf;
      7 	int len = 0;
      8 	*written = 0;
      9 
     10 	do {
     11 		len = fread(p, 1, 4096, fd);
     12 		*written += len;
     13 		p += len;
     14 		if (p > buf + buflen)
     15 			return 0;
     16 	} while (len == 4096);
     17 
     18 	return 1;
     19 }
     20 
     21 static int write_file(const char *filename, unsigned char *buf, int buflen)
     22 {
     23 	FILE *file = NULL;
     24 	int ok;
     25 
     26 	file = fopen(filename, "w");
     27 	if (file == NULL)
     28 		return 0;
     29 
     30 	ok = fwrite(buf, buflen, 1, file);
     31 	fclose(file);
     32 	return ok;
     33 }
     34 
     35 static int read_file(const char *filename, unsigned char *buf, int buflen, int *written)
     36 {
     37 	FILE *file = NULL;
     38 	int ok;
     39 
     40 	file = fopen(filename, "r");
     41 	if (file == NULL)
     42 		return 1;
     43 
     44 	ok = read_fd(file, buf, buflen, written);
     45 	fclose(file);
     46 	return ok;
     47 }
     48