xpipe.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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:: /Commando/Code/Library/XPIPE.CPP $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 9/28/98 12:06p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * BufferPipe::Put -- Submit data to the buffered pipe segment. *
  35. * FilePipe::Put -- Submit a block of data to the pipe. *
  36. * FilePipe::End -- End the file pipe handler. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "always.h"
  39. #include "xpipe.h"
  40. #include <stddef.h>
  41. #include <string.h>
  42. //---------------------------------------------------------------------------------------------------------
  43. // BufferPipe
  44. //---------------------------------------------------------------------------------------------------------
  45. /***********************************************************************************************
  46. * BufferPipe::Put -- Submit data to the buffered pipe segment. *
  47. * *
  48. * The buffered pipe is a pipe terminator. That is, the data never flows onto subsequent *
  49. * pipe chains. The data is stored into the buffer previously submitted to the pipe. *
  50. * If the buffer is full, no more data is output to the buffer. *
  51. * *
  52. * INPUT: source -- Pointer to the data to submit. *
  53. * *
  54. * length -- The number of bytes to be submitted. *
  55. * *
  56. * OUTPUT: Returns with the number of bytes output to the destination buffer. *
  57. * *
  58. * WARNINGS: none *
  59. * *
  60. * HISTORY: *
  61. * 07/03/1996 JLB : Created. *
  62. *=============================================================================================*/
  63. int BufferPipe::Put(void const * 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(((char *)BufferPtr.Get_Buffer()) + Index, source, len);
  74. }
  75. Index += len;
  76. // Length -= len;
  77. // Buffer = ((char *)Buffer) + len;
  78. total += len;
  79. }
  80. return(total);
  81. }
  82. //---------------------------------------------------------------------------------------------------------
  83. // FilePipe
  84. //---------------------------------------------------------------------------------------------------------
  85. FilePipe::~FilePipe(void)
  86. {
  87. if (Valid_File() && HasOpened) {
  88. HasOpened = false;
  89. File->Close();
  90. File = NULL;
  91. }
  92. }
  93. /***********************************************************************************************
  94. * FilePipe::End -- End the file pipe handler. *
  95. * *
  96. * This routine is called when there will be no more data sent through the pipe. It is *
  97. * responsible for cleaning up anything it needs to. This is not handled by the *
  98. * destructor, although it serves a similar purpose, because pipe are linked together and *
  99. * the destructor order is not easily controlled. If the destructors for a pipe chain were *
  100. * called out of order, the result might be less than pleasant. *
  101. * *
  102. * INPUT: none *
  103. * *
  104. * OUTPUT: Returns with the number of bytes flushed out the final end of the pipe as a *
  105. * consequence of this routine. *
  106. * *
  107. * WARNINGS: Don't send any more data through the pipe after this routine is called. *
  108. * *
  109. * HISTORY: *
  110. * 07/05/1996 JLB : Created. *
  111. *=============================================================================================*/
  112. int FilePipe::End(void)
  113. {
  114. int total = Pipe::End();
  115. if (Valid_File() && HasOpened) {
  116. HasOpened = false;
  117. File->Close();
  118. }
  119. return(total);
  120. }
  121. /***********************************************************************************************
  122. * FilePipe::Put -- Submit a block of data to the pipe. *
  123. * *
  124. * Takes the data block submitted and writes it to the file. If the file was not already *
  125. * open, this routine will open it for write. *
  126. * *
  127. * INPUT: source -- Pointer to the data to submit to the file. *
  128. * *
  129. * length -- The number of bytes to write to the file. *
  130. * *
  131. * OUTPUT: Returns with the number of bytes written to the file. *
  132. * *
  133. * WARNINGS: none *
  134. * *
  135. * HISTORY: *
  136. * 07/03/1996 JLB : Created. *
  137. *=============================================================================================*/
  138. int FilePipe::Put(void const * source, int slen)
  139. {
  140. if (Valid_File() && source != NULL && slen > 0) {
  141. if (!File->Is_Open()) {
  142. HasOpened = true;
  143. File->Open(FileClass::WRITE);
  144. }
  145. return(File->Write(source, slen));
  146. }
  147. return(0);
  148. }