XSTRAW.CPP 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/XSTRAW.CPP 1 3/03/97 10:26a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : XSTRAW.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/04/96 *
  26. * *
  27. * Last Update : July 4, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * BufferStraw::Get -- Fetch data from the straw's buffer holding tank. *
  32. * FileStraw::Get -- Fetch data from the file. *
  33. * FileStraw::~FileStraw -- The destructor for the file straw. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "FUNCTION.H"
  36. #include "xstraw.h"
  37. #include <stddef.h>
  38. #include <string.h>
  39. //---------------------------------------------------------------------------------------------------------
  40. // BufferStraw
  41. //---------------------------------------------------------------------------------------------------------
  42. /***********************************************************************************************
  43. * BufferStraw::Get -- Fetch data from the straw's buffer holding tank. *
  44. * *
  45. * This routine will copy the requested number of bytes from the buffer holding tank (as *
  46. * set up by the straw's constructor) to the buffer specified. *
  47. * *
  48. * INPUT: source -- Pointer to the buffer to be filled with data. *
  49. * *
  50. * length -- The number of bytes requested. *
  51. * *
  52. * OUTPUT: Returns with the number of bytes copied to the buffer. If this is less than *
  53. * requested, then it indicates that the data holding tank buffer is exhausted. *
  54. * *
  55. * WARNINGS: none *
  56. * *
  57. * HISTORY: *
  58. * 07/03/1996 JLB : Created. *
  59. *=============================================================================================*/
  60. int BufferStraw::Get(void * source, int slen)
  61. {
  62. int total = 0;
  63. if (Is_Valid() && source != NULL && slen > 0) {
  64. int len = slen;
  65. if (BufferPtr.Get_Size() != 0) {
  66. int theoretical_max = BufferPtr.Get_Size() - Index;
  67. len = (slen < theoretical_max) ? slen : theoretical_max;
  68. }
  69. if (len > 0) {
  70. memmove(source, ((char*)BufferPtr.Get_Buffer()) + Index, len);
  71. }
  72. Index += len;
  73. // Length -= len;
  74. // BufferPtr = ((char *)BufferPtr) + len;
  75. total += len;
  76. }
  77. return(total);
  78. }
  79. //---------------------------------------------------------------------------------------------------------
  80. // FileStraw
  81. //---------------------------------------------------------------------------------------------------------
  82. /***********************************************************************************************
  83. * FileStraw::Get -- Fetch data from the file. *
  84. * *
  85. * This routine will read data from the file (as specified in the straw's constructor) into *
  86. * the buffer indicated. *
  87. * *
  88. * INPUT: source -- Pointer to the buffer to hold the data. *
  89. * *
  90. * length -- The number of bytes requested. *
  91. * *
  92. * OUTPUT: Returns with the number of bytes stored into the buffer. If this number is less *
  93. * than the number requested, then this indicates that the file is exhausted. *
  94. * *
  95. * WARNINGS: none *
  96. * *
  97. * HISTORY: *
  98. * 07/03/1996 JLB : Created. *
  99. *=============================================================================================*/
  100. int FileStraw::Get(void * source, int slen)
  101. {
  102. if (Valid_File() && source != NULL && slen > 0) {
  103. if (!File->Is_Open()) {
  104. HasOpened = true;
  105. if (!File->Is_Available()) return(0);
  106. if (!File->Open(READ)) return(0);
  107. }
  108. return(File->Read(source, slen));
  109. }
  110. return(0);
  111. }
  112. /***********************************************************************************************
  113. * FileStraw::~FileStraw -- The destructor for the file straw. *
  114. * *
  115. * This destructor only needs to close the file if it was the one to open it. *
  116. * *
  117. * INPUT: none *
  118. * *
  119. * OUTPUT: none *
  120. * *
  121. * WARNINGS: none *
  122. * *
  123. * HISTORY: *
  124. * 07/03/1996 JLB : Created. *
  125. *=============================================================================================*/
  126. FileStraw::~FileStraw(void)
  127. {
  128. if (Valid_File() && HasOpened) {
  129. File->Close();
  130. HasOpened = false;
  131. File = NULL;
  132. }
  133. }