XSTRAW.CPP 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/XSTRAW.CPP 1 3/03/97 10:26a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : XSTRAW.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 07/04/96 *
  30. * *
  31. * Last Update : July 4, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * BufferStraw::Get -- Fetch data from the straw's buffer holding tank. *
  36. * FileStraw::Get -- Fetch data from the file. *
  37. * FileStraw::~FileStraw -- The destructor for the file straw. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "xstraw.h"
  40. #include <stddef.h>
  41. #include <string.h>
  42. //---------------------------------------------------------------------------------------------------------
  43. // BufferStraw
  44. //---------------------------------------------------------------------------------------------------------
  45. /***********************************************************************************************
  46. * BufferStraw::Get -- Fetch data from the straw's buffer holding tank. *
  47. * *
  48. * This routine will copy the requested number of bytes from the buffer holding tank (as *
  49. * set up by the straw's constructor) to the buffer specified. *
  50. * *
  51. * INPUT: source -- Pointer to the buffer to be filled with data. *
  52. * *
  53. * length -- The number of bytes requested. *
  54. * *
  55. * OUTPUT: Returns with the number of bytes copied to the buffer. If this is less than *
  56. * requested, then it indicates that the data holding tank buffer is exhausted. *
  57. * *
  58. * WARNINGS: none *
  59. * *
  60. * HISTORY: *
  61. * 07/03/1996 JLB : Created. *
  62. *=============================================================================================*/
  63. int BufferStraw::Get(void * source, int slen)
  64. {
  65. int total = 0;
  66. if (Is_Valid() && source != NULL && slen > 0) {
  67. int len = slen;
  68. if (BufferPtr.Get_Size() != 0) {
  69. int theoretical_max = BufferPtr.Get_Size() - Index;
  70. len = (slen < theoretical_max) ? slen : theoretical_max;
  71. }
  72. if (len > 0) {
  73. memmove(source, ((char*)BufferPtr.Get_Buffer()) + Index, len);
  74. }
  75. Index += len;
  76. // Length -= len;
  77. // BufferPtr = ((char *)BufferPtr) + len;
  78. total += len;
  79. }
  80. return(total);
  81. }
  82. //---------------------------------------------------------------------------------------------------------
  83. // FileStraw
  84. //---------------------------------------------------------------------------------------------------------
  85. /***********************************************************************************************
  86. * FileStraw::Get -- Fetch data from the file. *
  87. * *
  88. * This routine will read data from the file (as specified in the straw's constructor) into *
  89. * the buffer indicated. *
  90. * *
  91. * INPUT: source -- Pointer to the buffer to hold the data. *
  92. * *
  93. * length -- The number of bytes requested. *
  94. * *
  95. * OUTPUT: Returns with the number of bytes stored into the buffer. If this number is less *
  96. * than the number requested, then this indicates that the file is exhausted. *
  97. * *
  98. * WARNINGS: none *
  99. * *
  100. * HISTORY: *
  101. * 07/03/1996 JLB : Created. *
  102. *=============================================================================================*/
  103. int FileStraw::Get(void * source, int slen)
  104. {
  105. if (Valid_File() && source != NULL && slen > 0) {
  106. if (!File->Is_Open()) {
  107. HasOpened = true;
  108. if (!File->Is_Available()) return(0);
  109. if (!File->Open(READ)) return(0);
  110. }
  111. return(File->Read(source, slen));
  112. }
  113. return(0);
  114. }
  115. /***********************************************************************************************
  116. * FileStraw::~FileStraw -- The destructor for the file straw. *
  117. * *
  118. * This destructor only needs to close the file if it was the one to open it. *
  119. * *
  120. * INPUT: none *
  121. * *
  122. * OUTPUT: none *
  123. * *
  124. * WARNINGS: none *
  125. * *
  126. * HISTORY: *
  127. * 07/03/1996 JLB : Created. *
  128. *=============================================================================================*/
  129. FileStraw::~FileStraw(void)
  130. {
  131. if (Valid_File() && HasOpened) {
  132. File->Close();
  133. HasOpened = false;
  134. File = NULL;
  135. }
  136. }