saveutils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* =============================================================================
  2. * PROGRAM: ularn
  3. * FILENAME: saveutils.c
  4. *
  5. * DESCRIPTION:
  6. * This module contains utilities used in loading and saving games.
  7. *
  8. * =============================================================================
  9. * EXPORTED VARIABLES
  10. *
  11. * FileSum : The current checksum for the file being written/read.
  12. *
  13. * =============================================================================
  14. * EXPORTED FUNCTIONS
  15. *
  16. * sum : Checksum calculation function
  17. * bwrite : Binary write with checksum update
  18. * bread : Binary read with checksum update.
  19. *
  20. * =============================================================================
  21. */
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include "saveutils.h"
  25. #include "scores.h"
  26. #include "ularn_win.h"
  27. /* =============================================================================
  28. * Local variables
  29. */
  30. /* The number of characters written */
  31. static int w = 0;
  32. /* The number of characters read */
  33. static int r = 0;
  34. /* =============================================================================
  35. * Exported variables
  36. */
  37. int FileSum;
  38. /* =============================================================================
  39. * Exported functions
  40. */
  41. /* =============================================================================
  42. * FUNCTION: sum
  43. */
  44. unsigned int sum(unsigned char *data, int n) {
  45. unsigned int sum;
  46. int c, nb;
  47. sum = nb = 0;
  48. while (nb++ < n) {
  49. c = *data++;
  50. if (sum & 01)
  51. sum = (sum >> 1) + 0x8000;
  52. else
  53. sum >>= 1;
  54. sum += c;
  55. sum &= 0xFFFF;
  56. }
  57. return sum;
  58. }
  59. /* =============================================================================
  60. * FUNCTION: bwrite
  61. */
  62. void bwrite(FILE *fp, char *buf, long num) {
  63. int nwrote;
  64. static int ncalls = 0;
  65. ncalls++;
  66. nwrote = fwrite(buf, 1, num, fp);
  67. w += nwrote;
  68. if (nwrote != num) {
  69. Printf("Error writing to save file\n");
  70. Printf("wrote %d, wanted %d\n", nwrote, num);
  71. // Printf("errno = %d\t[%s]\n", errno, sys_errlist[errno]);
  72. Printf(" Wrote %d bytes so far\n", w);
  73. Printf(" Call: %d\n", ncalls);
  74. nap(4000);
  75. died(DIED_POST_MORTEM_DEATH, 0);
  76. }
  77. FileSum += sum((unsigned char *)buf, num);
  78. }
  79. /* =============================================================================
  80. * FUNCTION: bread
  81. */
  82. void bread(FILE *fp, char *buf, long num) {
  83. int nread;
  84. static int ncalls = 0;
  85. ncalls++;
  86. nread = fread(buf, 1, num, fp);
  87. r += nread;
  88. if (nread != num) {
  89. Printf("Error reading from save file\n");
  90. Printf(" Got %d, wanted %d bytes\n", nread, num);
  91. // Printf("errno = %d\t[%s]\n",errno,sys_errlist[errno]);
  92. Printf(" Read %d bytes so far\n", w);
  93. Printf(" Call: %d\n", ncalls);
  94. nap(4000);
  95. died(DIED_POST_MORTEM_DEATH, 0);
  96. }
  97. FileSum += sum((unsigned char *)buf, num);
  98. }