transfers.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(disable:4996)
  3. #endif
  4. // amckern - 64bit - 'size_t' to 'unsigned int'
  5. #pragma warning(disable:4267)
  6. #include "qrad.h"
  7. #ifdef SYSTEM_WIN32
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include "win32fix.h"
  11. #endif
  12. #ifdef HAVE_SYS_STAT_H
  13. #include <sys/stat.h>
  14. #endif
  15. /*
  16. * =============
  17. * writetransfers
  18. * =============
  19. */
  20. void writetransfers(const char* const transferfile, const long total_patches)
  21. {
  22. FILE *file;
  23. file = fopen(transferfile, "w+b");
  24. if (file != NULL)
  25. {
  26. unsigned amtwritten;
  27. patch_t* patch;
  28. Log("Writing transfers file [%s]\n", transferfile);
  29. amtwritten = fwrite(&total_patches, sizeof(total_patches), 1, file);
  30. if (amtwritten != 1)
  31. {
  32. goto FailedWrite;
  33. }
  34. long patchcount = total_patches;
  35. for (patch = g_patches; patchcount-- > 0; patch++)
  36. {
  37. amtwritten = fwrite(&patch->iIndex, sizeof(patch->iIndex), 1, file);
  38. if (amtwritten != 1)
  39. {
  40. goto FailedWrite;
  41. }
  42. if (patch->iIndex)
  43. {
  44. amtwritten = fwrite(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file);
  45. if (amtwritten != patch->iIndex)
  46. {
  47. goto FailedWrite;
  48. }
  49. }
  50. amtwritten = fwrite(&patch->iData, sizeof(patch->iData), 1, file);
  51. if (amtwritten != 1)
  52. {
  53. goto FailedWrite;
  54. }
  55. if (patch->iData)
  56. {
  57. #ifdef HLRAD_HULLU
  58. if(g_rgb_transfers)
  59. {
  60. amtwritten = fwrite(patch->tRGBData, sizeof(rgb_transfer_data_t), patch->iData, file);
  61. }
  62. else
  63. {
  64. amtwritten = fwrite(patch->tData, sizeof(transfer_data_t), patch->iData, file);
  65. }
  66. #else
  67. amtwritten = fwrite(patch->tData, sizeof(transfer_data_t), patch->iData, file);
  68. #endif
  69. if (amtwritten != patch->iData)
  70. {
  71. goto FailedWrite;
  72. }
  73. }
  74. }
  75. fclose(file);
  76. }
  77. else
  78. {
  79. Error("Failed to open incremenetal file [%s] for writing\n", transferfile);
  80. }
  81. return;
  82. FailedWrite:
  83. fclose(file);
  84. _unlink(transferfile);
  85. Warning("Failed to generate incremental file [%s] (probably ran out of disk space)\n");
  86. }
  87. /*
  88. * =============
  89. * readtransfers
  90. * =============
  91. */
  92. bool readtransfers(const char* const transferfile, const long numpatches)
  93. {
  94. FILE* file;
  95. long total_patches;
  96. file = fopen(transferfile, "rb");
  97. if (file != NULL)
  98. {
  99. unsigned amtread;
  100. patch_t* patch;
  101. Log("Reading transfers file [%s]\n", transferfile);
  102. amtread = fread(&total_patches, sizeof(total_patches), 1, file);
  103. if (amtread != 1)
  104. {
  105. goto FailedRead;
  106. }
  107. if (total_patches != numpatches)
  108. {
  109. goto FailedRead;
  110. }
  111. long patchcount = total_patches;
  112. for (patch = g_patches; patchcount-- > 0; patch++)
  113. {
  114. amtread = fread(&patch->iIndex, sizeof(patch->iIndex), 1, file);
  115. if (amtread != 1)
  116. {
  117. goto FailedRead;
  118. }
  119. if (patch->iIndex)
  120. {
  121. patch->tIndex = (transfer_index_t*)AllocBlock(patch->iIndex * sizeof(transfer_index_t *));
  122. hlassume(patch->tIndex != NULL, assume_NoMemory);
  123. amtread = fread(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file);
  124. if (amtread != patch->iIndex)
  125. {
  126. goto FailedRead;
  127. }
  128. }
  129. amtread = fread(&patch->iData, sizeof(patch->iData), 1, file);
  130. if (amtread != 1)
  131. {
  132. goto FailedRead;
  133. }
  134. if (patch->iData)
  135. {
  136. #ifdef HLRAD_HULLU
  137. if(g_rgb_transfers)
  138. {
  139. patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * sizeof(rgb_transfer_data_t *));
  140. hlassume(patch->tRGBData != NULL, assume_NoMemory);
  141. amtread = fread(patch->tRGBData, sizeof(rgb_transfer_data_t), patch->iData, file);
  142. }
  143. else
  144. {
  145. patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *));
  146. hlassume(patch->tData != NULL, assume_NoMemory);
  147. amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file);
  148. }
  149. #else
  150. patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *));
  151. hlassume(patch->tData != NULL, assume_NoMemory);
  152. amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file);
  153. #endif
  154. if (amtread != patch->iData)
  155. {
  156. goto FailedRead;
  157. }
  158. }
  159. }
  160. fclose(file);
  161. Warning("Finished reading transfers file [%s] %d\n", transferfile);
  162. return true;
  163. }
  164. Warning("Failed to open transfers file [%s]\n", transferfile);
  165. return false;
  166. FailedRead:
  167. {
  168. unsigned x;
  169. patch_t* patch = g_patches;
  170. for (x = 0; x < g_num_patches; x++, patch++)
  171. {
  172. FreeBlock(patch->tData);
  173. FreeBlock(patch->tIndex);
  174. patch->iData = 0;
  175. patch->iIndex = 0;
  176. patch->tData = NULL;
  177. patch->tIndex = NULL;
  178. }
  179. }
  180. fclose(file);
  181. _unlink(transferfile);
  182. return false;
  183. }