CnC_Remastered_Collection

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

FIELD.CPP (6813B)


      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 /***************************************************************************
     17  *                                                                         *
     18  *                 Project Name : Westwood Auto Registration App           *
     19  *                                                                         *
     20  *                    File Name : FIELD.CPP                                *
     21  *                                                                         *
     22  *                   Programmer : Philip W. Gorrow                         *
     23  *                                                                         *
     24  *                   Start Date : 04/22/96                                 *
     25  *                                                                         *
     26  *                  Last Update : April 22, 1996 [PWG]                     *
     27  *                                                                         *
     28  *  Actual member function for the field class.                            *
     29  *-------------------------------------------------------------------------*
     30  * Functions:                                                              *
     31  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     32 #include <string.h>
     33 #include "field.h"
     34 
     35 FieldClass::FieldClass(char *id, char data)
     36 {
     37 	strncpy(ID, id, sizeof(ID));
     38 	DataType = TYPE_CHAR;
     39 	Size		= sizeof(data);
     40 	Data		= new char[Size];
     41 	memcpy(Data, &data, Size);
     42 	Next		= NULL;
     43 }
     44 
     45 FieldClass::FieldClass(char *id, unsigned char data)
     46 {
     47 	strncpy(ID, id, sizeof(ID));
     48 	DataType = TYPE_UNSIGNED_CHAR;
     49 	Size		= sizeof(data);
     50 	Data		= new char[Size];
     51 	memcpy(Data, &data, Size);
     52 	Next		= NULL;
     53 }
     54 
     55 FieldClass::FieldClass(char *id, short data)
     56 {
     57 	strncpy(ID, id, sizeof(ID));
     58 	DataType = TYPE_SHORT;
     59 	Size		= sizeof(data);
     60 	Data		= new char[Size];
     61 	memcpy(Data, &data, Size);
     62 	Next		= NULL;
     63 }
     64 
     65 FieldClass::FieldClass(char *id, unsigned short data)
     66 {
     67 	strncpy(ID, id, sizeof(ID));
     68 	DataType = TYPE_UNSIGNED_SHORT;
     69 	Size		= sizeof(data);
     70 	Data		= new char[Size];
     71 	memcpy(Data, &data, Size);
     72 	Next		= NULL;
     73 }
     74 
     75 FieldClass::FieldClass(char *id, long data)
     76 {
     77 	strncpy(ID, id, sizeof(ID));
     78 	DataType = TYPE_LONG;
     79 	Size		= sizeof(data);
     80 	Data		= new char[Size];
     81 	memcpy(Data, &data, Size);
     82 	Next		= NULL;
     83 }
     84 
     85 FieldClass::FieldClass(char *id, unsigned long data)
     86 {
     87 	strncpy(ID, id, sizeof(ID));
     88 	DataType = TYPE_UNSIGNED_LONG;
     89 	Size		= sizeof(data);
     90 	Data		= new char[Size];
     91 	memcpy(Data, &data, Size);
     92 	Next		= NULL;
     93 }
     94 
     95 FieldClass::FieldClass(char *id, char *data)
     96 {
     97 	strncpy(ID, id, sizeof(ID));
     98 	DataType = TYPE_STRING;
     99 	Size		= (unsigned short)(strlen(data)+1);
    100 	Data		= new char[Size];
    101 	memcpy(Data, data, Size);
    102 	Next		= NULL;
    103 }
    104 
    105 FieldClass::FieldClass(char *id, void *data, int length)
    106 {
    107 	strncpy(ID, id, sizeof(ID));
    108 	DataType = TYPE_CHUNK;
    109 	Size		= (unsigned short)length;
    110 	Data		= new char[Size];
    111 	memcpy(Data, data, Size);
    112 	Next		= NULL;
    113 }
    114 
    115 
    116 /**************************************************************************
    117  * PACKETCLASS::HOST_TO_NET_FIELD -- Converts host field to net format    *
    118  *                                                                        *
    119  * INPUT:		FIELD 	* to the data field we need to convert				  *
    120  *                                                                        *
    121  * OUTPUT:     none																		  *
    122  *                                                                        *
    123  * HISTORY:                                                               *
    124  *   04/22/1996 PWG : Created.                                            *
    125  *========================================================================*/
    126 void FieldClass::Host_To_Net(void)
    127 {
    128 	//
    129 	// Before we convert the data type, we should convert the actual data
    130 	//  sent.
    131 	//
    132 	switch (DataType) {
    133 		case TYPE_CHAR:
    134 		case TYPE_UNSIGNED_CHAR:
    135 		case TYPE_STRING:
    136 		case TYPE_CHUNK:
    137 			break;
    138 
    139 		case TYPE_SHORT:
    140 		case TYPE_UNSIGNED_SHORT:
    141 			*((unsigned short *)Data) = htons(*((unsigned short *)Data));
    142 			break;
    143 
    144 		case TYPE_LONG:
    145 		case TYPE_UNSIGNED_LONG:
    146 			*((unsigned long *)Data) = htonl(*((unsigned long *)Data));
    147 			break;
    148 
    149 		//
    150 		// Might be good to insert some type of error message here for unknown
    151 		//   datatypes -- but will leave that for later.
    152 		//
    153 		default:
    154 			break;
    155 	}
    156 	//
    157 	// Finally convert over the data type and the size of the packet.
    158 	//
    159 	DataType = htons(DataType);
    160 	Size 	 	= htons(Size);
    161 }
    162 /**************************************************************************
    163  * PACKETCLASS::NET_TO_HOST_FIELD -- Converts net field to host format    *
    164  *                                                                        *
    165  * INPUT:		FIELD 	* to the data field we need to convert				  *
    166  *                                                                        *
    167  * OUTPUT:     none																		  *
    168  *                                                                        *
    169  * HISTORY:                                                               *
    170  *   04/22/1996 PWG : Created.                                            *
    171  *========================================================================*/
    172 void FieldClass::Net_To_Host(void)
    173 {
    174 	//
    175 	// Convert the variables to host order.  This needs to be converted so
    176 	// the switch statement does compares on the data that follows.
    177 	//
    178 	Size 	 	= ntohs(Size);
    179 
    180 	DataType = ntohs(DataType);
    181 
    182 	//
    183 	// Before we convert the data type, we should convert the actual data
    184 	//  sent.
    185 	//
    186 	switch (DataType) {
    187 		case TYPE_CHAR:
    188 		case TYPE_UNSIGNED_CHAR:
    189 		case TYPE_STRING:
    190 		case TYPE_CHUNK:
    191 			break;
    192 
    193 		case TYPE_SHORT:
    194 		case TYPE_UNSIGNED_SHORT:
    195 			*((unsigned short *)Data) = ntohs(*((unsigned short *)Data));
    196 			break;
    197 
    198 		case TYPE_LONG:
    199 		case TYPE_UNSIGNED_LONG:
    200 			*((unsigned long *)Data) = ntohl(*((unsigned long *)Data));
    201 			break;
    202 
    203 		//
    204 		// Might be good to insert some type of error message here for unknown
    205 		//   datatypes -- but will leave that for later.
    206 		//
    207 		default:
    208 			break;
    209 	}
    210 }
    211