CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

XPIPE.CPP (9637B)


      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 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header: /CounterStrike/XPIPE.CPP 1     3/03/97 10:26a Joe_bostic $ */
     17 /***********************************************************************************************
     18  ***              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               ***
     19  ***********************************************************************************************
     20  *                                                                                             *
     21  *                 Project Name : Command & Conquer                                            *
     22  *                                                                                             *
     23  *                    File Name : XPIPE.CPP                                                    *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 07/04/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : July 5, 1996 [JLB]                                           *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   BufferPipe::Put -- Submit data to the buffered pipe segment.                              *
     34  *   FilePipe::Put -- Submit a block of data to the pipe.                                      *
     35  *   FilePipe::End -- End the file pipe handler.                                               *
     36  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     37 
     38 #include "FUNCTION.H"
     39 #include	"xpipe.h"
     40 #include	<stddef.h>
     41 #include	<string.h>
     42 
     43 
     44 //---------------------------------------------------------------------------------------------------------
     45 // BufferPipe
     46 //---------------------------------------------------------------------------------------------------------
     47 
     48 
     49 /***********************************************************************************************
     50  * BufferPipe::Put -- Submit data to the buffered pipe segment.                                *
     51  *                                                                                             *
     52  *    The buffered pipe is a pipe terminator. That is, the data never flows onto subsequent    *
     53  *    pipe chains. The data is stored into the buffer previously submitted to the pipe.        *
     54  *    If the buffer is full, no more data is output to the buffer.                             *
     55  *                                                                                             *
     56  * INPUT:   source   -- Pointer to the data to submit.                                         *
     57  *                                                                                             *
     58  *          length   -- The number of bytes to be submitted.                                   *
     59  *                                                                                             *
     60  * OUTPUT:  Returns with the number of bytes output to the destination buffer.                 *
     61  *                                                                                             *
     62  * WARNINGS:   none                                                                            *
     63  *                                                                                             *
     64  * HISTORY:                                                                                    *
     65  *   07/03/1996 JLB : Created.                                                                 *
     66  *=============================================================================================*/
     67 int BufferPipe::Put(void const * source, int slen)
     68 {
     69 	int total = 0;
     70 
     71 	if (Is_Valid() && source != NULL && slen > 0) {
     72 		int len = slen;
     73 		if (BufferPtr.Get_Size() != 0) {
     74 			int theoretical_max = BufferPtr.Get_Size() - Index;
     75 			len = (slen < theoretical_max) ? slen : theoretical_max;
     76 		}
     77 
     78 		if (len > 0) {
     79 			memmove(((char *)BufferPtr.Get_Buffer()) + Index, source, len);
     80 		}
     81 
     82 		Index += len;
     83 //		Length -= len;
     84 //		Buffer = ((char *)Buffer) + len;
     85 		total += len;
     86 	}
     87 	return(total);
     88 }
     89 
     90 
     91 //---------------------------------------------------------------------------------------------------------
     92 // FilePipe
     93 //---------------------------------------------------------------------------------------------------------
     94 
     95 FilePipe::~FilePipe(void)
     96 {
     97 	if (Valid_File() && HasOpened) {
     98 		HasOpened = false;
     99 		File->Close();
    100 		File = NULL;
    101 	}
    102 }
    103 
    104 
    105 /***********************************************************************************************
    106  * FilePipe::End -- End the file pipe handler.                                                 *
    107  *                                                                                             *
    108  *    This routine is called when there will be no more data sent through the pipe. It is      *
    109  *    responsible for cleaning up anything it needs to. This is not handled by the             *
    110  *    destructor, although it serves a similar purpose, because pipe are linked together and   *
    111  *    the destructor order is not easily controlled. If the destructors for a pipe chain were  *
    112  *    called out of order, the result might be less than pleasant.                             *
    113  *                                                                                             *
    114  * INPUT:   none                                                                               *
    115  *                                                                                             *
    116  * OUTPUT:  Returns with the number of bytes flushed out the final end of the pipe as a        *
    117  *          consequence of this routine.                                                       *
    118  *                                                                                             *
    119  * WARNINGS:   Don't send any more data through the pipe after this routine is called.         *
    120  *                                                                                             *
    121  * HISTORY:                                                                                    *
    122  *   07/05/1996 JLB : Created.                                                                 *
    123  *=============================================================================================*/
    124 int FilePipe::End(void)
    125 {
    126 	int total = Pipe::End();
    127 	if (Valid_File() && HasOpened) {
    128 		HasOpened = false;
    129 		File->Close();
    130 	}
    131 	return(total);
    132 }
    133 
    134 
    135 /***********************************************************************************************
    136  * FilePipe::Put -- Submit a block of data to the pipe.                                        *
    137  *                                                                                             *
    138  *    Takes the data block submitted and writes it to the file. If the file was not already    *
    139  *    open, this routine will open it for write.                                               *
    140  *                                                                                             *
    141  * INPUT:   source   -- Pointer to the data to submit to the file.                             *
    142  *                                                                                             *
    143  *          length   -- The number of bytes to write to the file.                              *
    144  *                                                                                             *
    145  * OUTPUT:  Returns with the number of bytes written to the file.                              *
    146  *                                                                                             *
    147  * WARNINGS:   none                                                                            *
    148  *                                                                                             *
    149  * HISTORY:                                                                                    *
    150  *   07/03/1996 JLB : Created.                                                                 *
    151  *=============================================================================================*/
    152 int FilePipe::Put(void const * source, int slen)
    153 {
    154 	if (Valid_File() && source != NULL && slen > 0) {
    155 		if (!File->Is_Open()) {
    156 			HasOpened = true;
    157 			File->Open(WRITE);
    158 		}
    159 
    160 		return(File->Write(source, slen));
    161 	}
    162 	return(0);
    163 }