1
0
mirror of https://github.com/mon/2dxTools.git synced 2024-09-23 19:08:27 +02:00
2dxTools/shared.c
2017-03-23 22:36:32 +10:00

35 lines
815 B
C

#include "shared.h"
#include <stdlib.h>
// TODO: buffering for speed
void transfer_file(FILE* in, FILE* out, size_t bytes) {
char byte;
for(size_t i = 0; i < bytes; i++) {
if(!fread(&byte, 1, 1, in)) {
printf("Could not read all bytes from input!\n");
exit(1);
}
fwrite(&byte, 1, 1, out);
}
}
// returns 0 if file is empty or noexistant, filesize otherwise
size_t file_size(const char* path) {
FILE* f = fopen(path, "rb");
if(!f)
return 0;
fseek(f, 0L, SEEK_END);
size_t size = ftell(f);
fclose(f);
return size;
}
// platform agnostic and proves we can read a file too
int file_exists(const char *path) {
FILE *file;
if ((file = fopen(path, "rb"))) {
fclose(file);
return 1;
}
return 0;
}