1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-27 05:40:17 +05:30

C: reverse-ramdisk.c: apply D.R.Y. to conditional compilation

This commit is contained in:
Intel A80486DX2-66 2024-02-22 20:28:41 +03:00
parent d36ed5f273
commit 544ae06944
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -61,10 +61,11 @@ typedef struct {
int ID;
char* file_path;
#if IS_POSIX
int file;
int
#else
FILE* file;
FILE*
#endif
file;
} TempFile;
TempFile* temp_files = NULL;
@ -149,10 +150,11 @@ int tf_free(int ID) {
temp_files[index].locked = true;
#if IS_POSIX
close(temp_files[index].file);
close
#else
fclose(temp_files[index].file);
fclose
#endif
(temp_files[index].file);
// Delete the file
if (remove(temp_files[index].file_path) != 0) {
@ -204,11 +206,13 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size) {
return -1;
// Set the position
if (
#if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) {
lseek
#else
if (fseek(file, offset, SEEK_SET) == -1) {
fseek
#endif
(file, offset, SEEK_SET) == -1) {
LINE_FAIL(-1);
return -1;
}
@ -261,33 +265,42 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
temp_files[index].locked = true;
#if IS_POSIX
int file = temp_files[index].file;
if (file == -1)
int
#else
FILE* file = temp_files[index].file;
if (file == NULL)
FILE*
#endif
file = temp_files[index].file;
if (file ==
#if IS_POSIX
-1
#else
NULL
#endif
)
return -1;
// Read the data from the file
void* src = malloc(data_size);
if (src == NULL) {
#if IS_POSIX
close(file);
close
#else
fclose(file);
fclose
#endif
LINE_FAIL(-7);
(file);
LINE_FAIL(-8);
return -1;
}
memset(src, 0, data_size); // clear destination
// Set the position
if (
#if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) {
lseek
#else
if (fseek(file, offset, SEEK_SET) == -1) {
fseek
#endif
(file, offset, SEEK_SET) == -1) {
LINE_FAIL(-1);
return -1;
}