XPIPE.CPP 9.4 KB

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