buff.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/BUFF.CPP $*
  25. * *
  26. * $Author:: Eric_c $*
  27. * *
  28. * $Modtime:: 4/15/99 10:14a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Buffer::Buffer -- Constructor for buffer object. *
  35. * Buffer::Buffer -- Copy constructor for buffer object. *
  36. * Buffer::Buffer -- Self-allocating constructor for buffer object. *
  37. * Buffer::Reset -- Clears the buffer object to null state. *
  38. * Buffer::operator = -- Assignment operator for the buffer object. *
  39. * Buffer::~Buffer -- Destructor for buffer object. *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include "always.h"
  42. #include "buff.h"
  43. //#include <stddef.h>
  44. /***********************************************************************************************
  45. * Buffer::Buffer -- Constructor for buffer object. *
  46. * *
  47. * This is the normal constructor for a buffer object. The buffer pointer and size are *
  48. * specified as parameters. *
  49. * *
  50. * INPUT: buffer -- Pointer to the buffer. *
  51. * *
  52. * size -- The size of the buffer. *
  53. * *
  54. * OUTPUT: none *
  55. * *
  56. * WARNINGS: It is possible to construct a Buffer object that has a pointer but a size *
  57. * value of zero. The Buffer object can still be used for its pointer, but it *
  58. * any function that requires a size will fail. *
  59. * *
  60. * HISTORY: *
  61. * 07/29/1996 JLB : Created. *
  62. *=============================================================================================*/
  63. Buffer::Buffer(void * buffer, long size) :
  64. BufferPtr(buffer),
  65. Size(size),
  66. IsAllocated(false)
  67. {
  68. if (buffer == NULL && size > 0) {
  69. BufferPtr = new char[size];
  70. IsAllocated = true;
  71. }
  72. }
  73. // Alternate constructor for char * pointer.
  74. Buffer::Buffer(char * buffer, long size) :
  75. BufferPtr(buffer),
  76. Size(size),
  77. IsAllocated(false)
  78. {
  79. if (buffer == NULL && size > 0) {
  80. BufferPtr = new char[size];
  81. IsAllocated = true;
  82. }
  83. }
  84. // Alternate constructor for void const * pointer.
  85. Buffer::Buffer(void const * buffer, long size) :
  86. BufferPtr((void*)buffer),
  87. Size(size),
  88. IsAllocated(false)
  89. {
  90. if (buffer == NULL && size > 0) {
  91. BufferPtr = new char[size];
  92. IsAllocated = true;
  93. }
  94. }
  95. /***********************************************************************************************
  96. * Buffer::Buffer -- Self-allocating constructor for buffer object. *
  97. * *
  98. * This construtor for a buffer object will automatically allocate the bytes necessary *
  99. * to fulfill the size requested. This object is also responsible for deleting the buffer *
  100. * it allocated. *
  101. * *
  102. * INPUT: size -- The size of the buffer to allocated. *
  103. * *
  104. * OUTPUT: none *
  105. * *
  106. * WARNINGS: There is no way to tell if the allocation failed. To verify, call Get_Buffer *
  107. * and compare with NULL. *
  108. * *
  109. * HISTORY: *
  110. * 07/29/1996 JLB : Created. *
  111. *=============================================================================================*/
  112. Buffer::Buffer(long size) :
  113. BufferPtr(NULL),
  114. Size(size),
  115. IsAllocated(false)
  116. {
  117. if (size > 0) {
  118. BufferPtr = new char[size];
  119. IsAllocated = true;
  120. }
  121. }
  122. /***********************************************************************************************
  123. * Buffer::Buffer -- Copy constructor for buffer object. *
  124. * *
  125. * This will make a duplicate of the specified buffer object. The ownership of the pointer *
  126. * remains with the original object. This prevents multiple deletion of the same pointer. *
  127. * *
  128. * INPUT: buffer -- Reference to the buffer object to be dupilcated. *
  129. * *
  130. * OUTPUT: none *
  131. * *
  132. * WARNINGS: none *
  133. * *
  134. * HISTORY: *
  135. * 08/02/1996 JLB : Created. *
  136. *=============================================================================================*/
  137. Buffer::Buffer(Buffer const & buffer) :
  138. IsAllocated(false)
  139. {
  140. BufferPtr = buffer.BufferPtr;
  141. Size = buffer.Size;
  142. }
  143. /***********************************************************************************************
  144. * Buffer::operator = -- Assignment operator for the buffer object. *
  145. * *
  146. * This will make a duplicate of the buffer object specified. Any buffer pointed to by the *
  147. * left hand buffer will be lost (possibley freed as a result). *
  148. * *
  149. * INPUT: buffer -- Reference to the right hand buffer object. *
  150. * *
  151. * OUTPUT: Returns with a reference to the copied buffer object. *
  152. * *
  153. * WARNINGS: none *
  154. * *
  155. * HISTORY: *
  156. * 08/02/1996 JLB : Created. *
  157. *=============================================================================================*/
  158. Buffer & Buffer::operator = (Buffer const & buffer)
  159. {
  160. if (&buffer != this) {
  161. if (IsAllocated) {
  162. delete [] BufferPtr;
  163. }
  164. IsAllocated = false;
  165. BufferPtr = buffer.BufferPtr;
  166. Size = buffer.Size;
  167. }
  168. return(*this);
  169. }
  170. /***********************************************************************************************
  171. * Buffer::~Buffer -- Destructor for buffer object. *
  172. * *
  173. * This destructor will free any buffer it is responsible for. *
  174. * *
  175. * INPUT: none *
  176. * *
  177. * OUTPUT: none *
  178. * *
  179. * WARNINGS: none *
  180. * *
  181. * HISTORY: *
  182. * 07/29/1996 JLB : Created. *
  183. *=============================================================================================*/
  184. Buffer::~Buffer(void)
  185. {
  186. Reset();
  187. }
  188. /***********************************************************************************************
  189. * Buffer::Reset -- Clears the buffer object to null state. *
  190. * *
  191. * This routine will bring the buffer object into a null (newly constructed) state. If *
  192. * there was any buffer allocated or referred to by this object, it will be freed or *
  193. * dereferenced as necessary. *
  194. * *
  195. * INPUT: none *
  196. * *
  197. * OUTPUT: none *
  198. * *
  199. * WARNINGS: This routine will free the buffer if it is responsible for doing so when *
  200. * it is no longer referenced. *
  201. * *
  202. * HISTORY: *
  203. * 09/07/1996 JLB : Created. *
  204. *=============================================================================================*/
  205. void Buffer::Reset(void)
  206. {
  207. if (IsAllocated) {
  208. delete [] BufferPtr;
  209. }
  210. BufferPtr = NULL;
  211. Size = 0;
  212. IsAllocated = false;
  213. }