1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-27 14:50:23 +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; int ID;
char* file_path; char* file_path;
#if IS_POSIX #if IS_POSIX
int file; int
#else #else
FILE* file; FILE*
#endif #endif
file;
} TempFile; } TempFile;
TempFile* temp_files = NULL; TempFile* temp_files = NULL;
@ -149,10 +150,11 @@ int tf_free(int ID) {
temp_files[index].locked = true; temp_files[index].locked = true;
#if IS_POSIX #if IS_POSIX
close(temp_files[index].file); close
#else #else
fclose(temp_files[index].file); fclose
#endif #endif
(temp_files[index].file);
// Delete the file // Delete the file
if (remove(temp_files[index].file_path) != 0) { 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; return -1;
// Set the position // Set the position
if (
#if IS_POSIX #if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) { lseek
#else #else
if (fseek(file, offset, SEEK_SET) == -1) { fseek
#endif #endif
(file, offset, SEEK_SET) == -1) {
LINE_FAIL(-1); LINE_FAIL(-1);
return -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; temp_files[index].locked = true;
#if IS_POSIX #if IS_POSIX
int file = temp_files[index].file; int
if (file == -1)
#else #else
FILE* file = temp_files[index].file; FILE*
if (file == NULL)
#endif #endif
file = temp_files[index].file;
if (file ==
#if IS_POSIX
-1
#else
NULL
#endif
)
return -1; return -1;
// Read the data from the file // Read the data from the file
void* src = malloc(data_size); void* src = malloc(data_size);
if (src == NULL) { if (src == NULL) {
#if IS_POSIX #if IS_POSIX
close(file); close
#else #else
fclose(file); fclose
#endif #endif
LINE_FAIL(-7); (file);
LINE_FAIL(-8);
return -1; return -1;
} }
memset(src, 0, data_size); // clear destination memset(src, 0, data_size); // clear destination
// Set the position // Set the position
if (
#if IS_POSIX #if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) { lseek
#else #else
if (fseek(file, offset, SEEK_SET) == -1) { fseek
#endif #endif
(file, offset, SEEK_SET) == -1) {
LINE_FAIL(-1); LINE_FAIL(-1);
return -1; return -1;
} }