Skip to content

Commit

Permalink
Split file handling to a separate library with some added safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
katajakasa committed May 3, 2023
1 parent a839aa3 commit 2c24fdf
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
55 changes: 55 additions & 0 deletions src/utils/io.c
@@ -0,0 +1,55 @@

#include <string.h>

#include "utils/io.h"
#include "utils/log.h"
#include "utils/miscmath.h"

#define READ_BLOCK_SIZE 32

FILE *file_open(const char *file_name, const char *mode) {
FILE *handle = fopen(file_name, mode);
if(handle == NULL) {
PERROR("Unable to open file: %s: %d", file_name, strerror(errno));
abort();
}
return handle;
}

void file_seek(FILE *handle, long offset, int origin) {
if(fseek(handle, offset, origin)) {
PERROR("Unable to fseek to %d %d: %s", origin, offset, strerror(errno));
abort();
}
}

long file_size(FILE *handle) {
long pos, file_size;
pos = ftell(handle);
file_seek(handle, 0, SEEK_END);
file_size = ftell(handle);
file_seek(handle, pos, SEEK_SET);
return file_size;
}

void file_read(FILE *handle, char *buffer, long size) {
size_t ptr = 0;
size_t read_size;
while(1) {
if(feof(handle)) {
break;
}
if(ferror(handle)) {
PERROR("Error while reading file: %s", strerror(errno));
abort();
}
read_size = min2(size - ptr, READ_BLOCK_SIZE);
if(read_size <= 0)
break;
ptr += fread(buffer + ptr, 1, read_size, handle);
}
}

void file_close(FILE *handle) {
fclose(handle);
}
12 changes: 12 additions & 0 deletions src/utils/io.h
@@ -0,0 +1,12 @@
#ifndef UTILS_IO_H
#define UTILS_IO_H

#include <stdio.h>

FILE *file_open(const char *file_name, const char *mode);
void file_seek(FILE *handle, long offset, int origin);
long file_size(FILE *handle);
void file_read(FILE *handle, char *buffer, long size);
void file_close(FILE *handle);

#endif // UTILS_IO_H
35 changes: 8 additions & 27 deletions src/utils/str.c
@@ -1,5 +1,6 @@
#include "utils/str.h"
#include "utils/allocator.h"
#include "utils/io.h"
#include "utils/log.h"

#include <assert.h>
Expand All @@ -8,6 +9,8 @@
#include <stdlib.h>
#include <string.h>

#define READ_BLOCK_SIZE (1024)

#define STR_ALLOC(string, size) \
do { \
string->len = (size); \
Expand Down Expand Up @@ -42,35 +45,13 @@ void str_from_buf(str *dst, const char *buf, size_t len) {
STR_ZERO(dst);
}

#define READ_BLOCK_SIZE 4096

void str_from_file(str *dst, const char *file_name) {
FILE *handle = fopen(file_name, "rb");
if(handle == NULL) {
PERROR("Unable to read file: %s", file_name);
abort();
}

fseek(handle, 0, SEEK_END);
long file_size = ftell(handle);
fseek(handle, 0, SEEK_SET);

STR_ALLOC(dst, file_size);
size_t got;
char *ptr = dst->data;
while(1) {
if(feof(handle))
break;
if(ferror(handle))
break;
got = fread(ptr, 1, READ_BLOCK_SIZE, handle);
if(got != READ_BLOCK_SIZE)
break;
ptr += got;
}
FILE *handle = file_open(file_name, "rb");
long size = file_size(handle);
STR_ALLOC(dst, size + 1);
file_read(handle, dst->data, size);
file_close(handle);
STR_ZERO(dst);

fclose(handle);
}

void str_from_format(str *dst, const char *format, ...) {
Expand Down

0 comments on commit 2c24fdf

Please sign in to comment.