EZGIMEX.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* Copyright (C) Electronic Arts Canada Inc. 1994-1998. All rights reserved. */
  19. /*------------------------------------------------------------------*/
  20. /* */
  21. /* ANSI Standard File and Memory Interface v1.04 */
  22. /* */
  23. /* by FrANK G. Barchard, EAC */
  24. /* */
  25. /* Code Module - May 24, 1995 */
  26. /* */
  27. /*------------------------------------------------------------------*/
  28. /* */
  29. /* Module Notes: */
  30. /* ------------- */
  31. /* This modules makes an easy basis for new Gimex low level */
  32. /* functions. You will need to make the following changes: */
  33. /* */
  34. /* function what to change */
  35. /* -------- -------------- */
  36. /* galloc/free put in your memory manager */
  37. /* LIBHANDLE.handle handle for your file system */
  38. /* gopen/gwopen fopen, fseek, ftell (find file size) */
  39. /* gclose fclose */
  40. /* gread fread and possibly memcpy */
  41. /* gwrite fwrite */
  42. /* gseek fseek */
  43. /* */
  44. /* The other routines should not need changing. */
  45. /* */
  46. /*------------------------------------------------------------------*/
  47. /* And increase stream buffer */
  48. /*
  49. setvbuf(f, NULL, _IOFBF, FILE_BUFFER_SIZE);
  50. */
  51. #define __NOINLINE__ 1
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <stdio.h>
  55. #include "gimex.h"
  56. //#include "wnd_file.h"
  57. /* Memory Functions */
  58. int galloccount=0;
  59. void * GCALL galloc(long size)
  60. {
  61. ++galloccount;
  62. return(malloc((size_t)size));
  63. }
  64. int GCALL gfree(void *memptr)
  65. {
  66. --galloccount;
  67. free(memptr);
  68. return(1);
  69. }
  70. /* get motorola memory */
  71. unsigned long ggetm(void *src, int bytes)
  72. {
  73. unsigned char *s = (unsigned char *) src;
  74. unsigned long value;
  75. value = 0L;
  76. while (bytes--)
  77. {
  78. value = (value<<8) + ((*s++));
  79. }
  80. return(value);
  81. }
  82. /* get intel memory */
  83. unsigned long ggeti(void *src, int bytes)
  84. {
  85. unsigned char *s = (unsigned char *) src;
  86. int i = 0;
  87. unsigned long value;
  88. value = 0L;
  89. while (bytes--)
  90. {
  91. value += ((*s++)) << (i);
  92. i += 8;
  93. }
  94. return(value);
  95. }
  96. /* put motorolla memory */
  97. void gputm(void *dst, unsigned long data, int bytes)
  98. {
  99. unsigned char *d = (unsigned char *) dst;
  100. unsigned long pval;
  101. data <<= (4-bytes)*8;
  102. while (bytes)
  103. {
  104. pval = data >> 24;
  105. *d++ = (unsigned char) pval;
  106. data <<= 8;
  107. --bytes;
  108. }
  109. }
  110. /* put intel memory */
  111. void gputi(void *dst, unsigned long data, int bytes)
  112. {
  113. unsigned char *d = (unsigned char *) dst;
  114. unsigned long pval;
  115. while (bytes)
  116. {
  117. pval = data;
  118. *d++ = (unsigned char) pval;
  119. data >>= 8;
  120. --bytes;
  121. }
  122. }
  123. /* File Functions */
  124. GSTREAM * GCALL gopen(const char *filename)
  125. {
  126. FILE *handle;
  127. handle = fopen( filename, "r+b" );
  128. if ( !handle ) {
  129. handle = fopen( filename, "rb" );
  130. }
  131. return((GSTREAM *) handle);
  132. }
  133. GSTREAM * GCALL gwopen(const char *filename)
  134. {
  135. FILE *handle;
  136. handle = fopen(filename,"w+b");
  137. if (!handle)
  138. handle = fopen(filename,"wb");
  139. return((GSTREAM *) handle);
  140. }
  141. int GCALL gclose(GSTREAM *g)
  142. {
  143. int ok=1;
  144. if (g)
  145. ok = !fclose((FILE*) g);
  146. return(ok);
  147. }
  148. int GCALL gread(GSTREAM *g, void *buf, long size)
  149. {
  150. return(fread(buf, (size_t) 1, (size_t) size, (FILE *) g));
  151. }
  152. int GCALL gwrite(GSTREAM *g, void *buf, long size)
  153. {
  154. return(fwrite(buf, (size_t)1, (size_t)size, (FILE *) g));
  155. }
  156. int GCALL gseek(GSTREAM *g, long offset)
  157. {
  158. return(!fseek((FILE *) g, offset, SEEK_SET));
  159. }
  160. long GCALL glen(GSTREAM *g)
  161. {
  162. long len;
  163. long oldpos = gtell(g);
  164. fseek((FILE *)g, 0, SEEK_END);
  165. len = gtell(g);
  166. fseek((FILE *)g, oldpos, SEEK_SET);
  167. return(len);
  168. }
  169. long GCALL gtell(GSTREAM *g)
  170. {
  171. return(ftell((FILE *) g));
  172. }