-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split file handling to a separate library with some added safety checks
- Loading branch information
1 parent
a839aa3
commit 2c24fdf
Showing
3 changed files
with
75 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters