From 3f75cd530cba41956b21badd30d92f8da5c90de7 Mon Sep 17 00:00:00 2001 From: seedee Date: Sat, 27 Aug 2022 07:03:30 +0100 Subject: [PATCH] Remove HLRAD_TRANSFERDATA_COMPRESS ifdefs --- src/sdhlt/sdHLRAD/transfers.cpp | 192 ++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 src/sdhlt/sdHLRAD/transfers.cpp diff --git a/src/sdhlt/sdHLRAD/transfers.cpp b/src/sdhlt/sdHLRAD/transfers.cpp new file mode 100644 index 00000000..1825fcbd --- /dev/null +++ b/src/sdhlt/sdHLRAD/transfers.cpp @@ -0,0 +1,192 @@ +#include "qrad.h" + +#ifdef SYSTEM_WIN32 +#include +#include +#include "win32fix.h" +#endif + +#ifdef HAVE_SYS_STAT_H +#include +#endif + +/* + * ============= + * writetransfers + * ============= + */ + +void writetransfers(const char* const transferfile, const long total_patches) +{ + FILE *file; + + file = fopen(transferfile, "w+b"); + if (file != NULL) + { + unsigned amtwritten; + patch_t* patch; + + Log("Writing transfers file [%s]\n", transferfile); + + amtwritten = fwrite(&total_patches, sizeof(total_patches), 1, file); + if (amtwritten != 1) + { + goto FailedWrite; + } + + long patchcount = total_patches; + for (patch = g_patches; patchcount-- > 0; patch++) + { + amtwritten = fwrite(&patch->iIndex, sizeof(patch->iIndex), 1, file); + if (amtwritten != 1) + { + goto FailedWrite; + } + + if (patch->iIndex) + { + amtwritten = fwrite(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file); + if (amtwritten != patch->iIndex) + { + goto FailedWrite; + } + } + + amtwritten = fwrite(&patch->iData, sizeof(patch->iData), 1, file); + if (amtwritten != 1) + { + goto FailedWrite; + } + if (patch->iData) + { + if(g_rgb_transfers) + { + amtwritten = fwrite(patch->tRGBData, vector_size[g_rgbtransfer_compress_type], patch->iData, file); + } + else + { + amtwritten = fwrite(patch->tData, float_size[g_transfer_compress_type], patch->iData, file); + } + if (amtwritten != patch->iData) + { + goto FailedWrite; + } + } + } + + fclose(file); + } + else + { + Error("Failed to open incremenetal file [%s] for writing\n", transferfile); + } + return; + + FailedWrite: + fclose(file); + unlink(transferfile); + //Warning("Failed to generate incremental file [%s] (probably ran out of disk space)\n"); + Warning("Failed to generate incremental file [%s] (probably ran out of disk space)\n", transferfile); //--vluzacn +} + +/* + * ============= + * readtransfers + * ============= + */ + +bool readtransfers(const char* const transferfile, const long numpatches) +{ + FILE* file; + long total_patches; + + file = fopen(transferfile, "rb"); + if (file != NULL) + { + unsigned amtread; + patch_t* patch; + + Log("Reading transfers file [%s]\n", transferfile); + + amtread = fread(&total_patches, sizeof(total_patches), 1, file); + if (amtread != 1) + { + goto FailedRead; + } + if (total_patches != numpatches) + { + goto FailedRead; + } + + long patchcount = total_patches; + for (patch = g_patches; patchcount-- > 0; patch++) + { + amtread = fread(&patch->iIndex, sizeof(patch->iIndex), 1, file); + if (amtread != 1) + { + goto FailedRead; + } + if (patch->iIndex) + { + patch->tIndex = (transfer_index_t*)AllocBlock(patch->iIndex * sizeof(transfer_index_t *)); + hlassume(patch->tIndex != NULL, assume_NoMemory); + amtread = fread(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file); + if (amtread != patch->iIndex) + { + goto FailedRead; + } + } + + amtread = fread(&patch->iData, sizeof(patch->iData), 1, file); + if (amtread != 1) + { + goto FailedRead; + } + if (patch->iData) + { + if(g_rgb_transfers) + { + patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * vector_size[g_rgbtransfer_compress_type] + unused_size); + hlassume(patch->tRGBData != NULL, assume_NoMemory); + amtread = fread(patch->tRGBData, vector_size[g_rgbtransfer_compress_type], patch->iData, file); + } + else + { + patch->tData = (transfer_data_t*)AllocBlock(patch->iData * float_size[g_transfer_compress_type] + unused_size); + hlassume(patch->tData != NULL, assume_NoMemory); + amtread = fread(patch->tData, float_size[g_transfer_compress_type], patch->iData, file); + } + if (amtread != patch->iData) + { + goto FailedRead; + } + } + } + + fclose(file); + //Warning("Finished reading transfers file [%s] %d\n", transferfile); + Warning("Finished reading transfers file [%s]\n", transferfile); //--vluzacn + return true; + } + Warning("Failed to open transfers file [%s]\n", transferfile); + return false; + + FailedRead: + { + unsigned x; + patch_t* patch = g_patches; + + for (x = 0; x < g_num_patches; x++, patch++) + { + FreeBlock(patch->tData); + FreeBlock(patch->tIndex); + patch->iData = 0; + patch->iIndex = 0; + patch->tData = NULL; + patch->tIndex = NULL; + } + } + fclose(file); + unlink(transferfile); + return false; +}