CnC_Remastered_Collection

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

XSTRAW.CPP (9157B)


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