Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
field.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/***************************************************************************
20 * *
21 * Project Name : Westwood Auto Registration App *
22 * *
23 * File Name : FIELD.CPP *
24 * *
25 * Programmer : Philip W. Gorrow *
26 * *
27 * Start Date : 04/22/96 *
28 * *
29 * Last Update : April 22, 1996 [PWG] *
30 * *
31 * Actual member function for the field class. *
32 *-------------------------------------------------------------------------*
33 * Functions: *
34 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35#include <string.h>
36#include <sys/types.h>
37#ifndef _WINDOWS
38#include <netinet/in.h>
39#else
40#define Win32_Winsock
41#include <windows.h>
42#endif
43#include "field.h"
44
45
46// private member func
47void FieldClass::Clear(void)
48{
49 delete[](Data);
50
51 strcpy(ID,"");
52 DataType=0;
53 Size=0;
54 Data=NULL;
55 Next=NULL;
56}
57
58
59FieldClass::FieldClass(char *id, char data)
60{
61 Data=NULL;
62 Set(id,data);
63}
64
65FieldClass::FieldClass(char *id, unsigned char data)
66{
67 Data=NULL;
68 Set(id,data);
69}
70
71FieldClass::FieldClass(char *id, short data)
72{
73 Data=NULL;
74 Set(id,data);
75}
76
77FieldClass::FieldClass(char *id, unsigned short data)
78{
79 Data=NULL;
80 Set(id,data);
81}
82
83FieldClass::FieldClass(char *id, long data)
84{
85 Data=NULL;
86 Set(id,data);
87}
88
89FieldClass::FieldClass(char *id, unsigned long data)
90{
91 Data=NULL;
92 Set(id,data);
93}
94
95FieldClass::FieldClass(char *id, char *data)
96{
97 Data=NULL;
98 Set(id,data);
99}
100
101FieldClass::FieldClass(char *id, void *data, int length)
102{
103 Data=NULL;
104 Set(id,data,length);
105}
106
107void FieldClass::Set(char *id, char data)
108{
109 FieldClass *Nextsave=Next;
110
111 Clear();
112 strncpy(ID, id, sizeof(ID));
113 DataType = TYPE_CHAR;
114 Size = sizeof(data);
115 Data = new char[Size];
116 memcpy(Data, &data, Size);
117 Next = Nextsave;
118}
119
120void FieldClass::Set(char *id, unsigned char data)
121{
122 FieldClass *Nextsave=Next;
123 Clear();
124 strncpy(ID, id, sizeof(ID));
125 DataType = TYPE_UNSIGNED_CHAR;
126 Size = sizeof(data);
127 Data = new char[Size];
128 memcpy(Data, &data, Size);
129 Next = Nextsave;
130}
131
132void FieldClass::Set(char *id, short data)
133{
134 FieldClass *Nextsave=Next;
135 Clear();
136 strncpy(ID, id, sizeof(ID));
137 DataType = TYPE_SHORT;
138 Size = sizeof(data);
139 Data = new char[Size];
140 memcpy(Data, &data, Size);
141 Next = Nextsave;
142}
143
144void FieldClass::Set(char *id, unsigned short data)
145{
146 FieldClass *Nextsave=Next;
147 Clear();
148 strncpy(ID, id, sizeof(ID));
149 DataType = TYPE_UNSIGNED_SHORT;
150 Size = sizeof(data);
151 Data = new char[Size];
152 memcpy(Data, &data, Size);
153 Next = Nextsave;
154}
155
156void FieldClass::Set(char *id, long data)
157{
158 FieldClass *Nextsave=Next;
159 Clear();
160 strncpy(ID, id, sizeof(ID));
161 DataType = TYPE_LONG;
162 Size = sizeof(data);
163 Data = new char[Size];
164 memcpy(Data, &data, Size);
165 Next = Nextsave;
166}
167
168void FieldClass::Set(char *id, unsigned long data)
169{
170 FieldClass *Nextsave=Next;
171 Clear();
172 strncpy(ID, id, sizeof(ID));
173 DataType = TYPE_UNSIGNED_LONG;
174 Size = sizeof(data);
175 Data = new char[Size];
176 memcpy(Data, &data, Size);
177 Next = Nextsave;
178}
179
180void FieldClass::Set(char *id, char *data)
181{
182 FieldClass *Nextsave=Next;
183 Clear();
184 strncpy(ID, id, sizeof(ID));
185 DataType = TYPE_STRING;
186 Size = (unsigned short)(strlen(data)+1);
187 Data = new char[Size];
188 memcpy(Data, data, Size);
189 Next = Nextsave;
190}
191
192
193void FieldClass::Set(char *id, void *data, int length)
194{
195 FieldClass *Nextsave=Next;
196 Clear();
197 strncpy(ID, id, sizeof(ID));
198 DataType = TYPE_CHUNK;
199 Size = (unsigned short)length;
200 Data = new char[Size];
201 memcpy(Data, data, Size);
202 Next = Nextsave;
203}
204
205
207{
208 Clear();
209}
210
211// Fetch the datatype
212int FieldClass::Get_Type(void)
213{
214 return(DataType);
215}
216
217void *FieldClass::Get_Data(void)
218{
219return(Data);
220}
221
222char *FieldClass::Get_ID(void)
223{
224return(ID);
225}
226
227/**************************************************************************
228 * PACKETCLASS::HOST_TO_NET_FIELD -- Converts host field to net format *
229 * *
230 * INPUT: FIELD * to the data field we need to convert *
231 * *
232 * OUTPUT: none *
233 * *
234 * HISTORY: *
235 * 04/22/1996 PWG : Created. *
236 *========================================================================*/
238{
239 //
240 // Before we convert the data type, we should convert the actual data
241 // sent.
242 //
243 switch (DataType) {
244 case TYPE_CHAR:
246 case TYPE_STRING:
247 break;
248
249 case TYPE_SHORT:
251 *((unsigned short *)Data) = htons(*((unsigned short *)Data));
252 break;
253
254 case TYPE_LONG:
256 *((unsigned long *)Data) = htonl(*((unsigned long *)Data));
257 break;
258
259 //
260 // Might be good to insert some type of error message here for unknown
261 // datatypes -- but will leave that for later.
262 //
263 default:
264 break;
265 }
266 //
267 // Finally convert over the data type and the size of the packet.
268 //
269 DataType = htons(DataType);
270 Size = htons(Size);
271}
272
273/**************************************************************************
274 * PACKETCLASS::NET_TO_HOST_FIELD -- Converts net field to host format *
275 * *
276 * INPUT: FIELD * to the data field we need to convert *
277 * *
278 * OUTPUT: none *
279 * *
280 * HISTORY: *
281 * 04/22/1996 PWG : Created. *
282 *========================================================================*/
284{
285 //
286 // Finally convert over the data type and the size of the packet.
287 //
288 DataType = ntohs(DataType);
289 Size = ntohs(Size);
290
291 //
292 // Before we convert the data type, we should convert the actual data
293 // sent.
294 //
295 switch (DataType) {
296 case TYPE_CHAR:
298 case TYPE_STRING:
299 break;
300
301 case TYPE_SHORT:
303 *((unsigned short *)Data) = ntohs(*((unsigned short *)Data));
304 break;
305
306 case TYPE_LONG:
308 *((unsigned long *)Data) = ntohl(*((unsigned long *)Data));
309 break;
310
311 //
312 // Might be good to insert some type of error message here for unknown
313 // datatypes -- but will leave that for later.
314 //
315 default:
316 break;
317 }
318}
319
#define NULL
Definition BaseType.h:92
int Get_Type(void)
Definition field.cpp:212
void Net_To_Host(void)
Definition field.cpp:283
void Host_To_Net(void)
Definition field.cpp:237
void * Get_Data(void)
Definition field.cpp:217
~FieldClass()
Definition field.cpp:206
FieldClass(void)
Definition field.h:59
char * Get_ID(void)
Definition field.cpp:222
void Set(char *id, char data)
Definition field.cpp:107
#define TYPE_UNSIGNED_CHAR
Definition field.h:41
#define TYPE_UNSIGNED_SHORT
Definition field.h:43
#define TYPE_CHAR
Definition field.h:40
#define TYPE_CHUNK
Definition field.h:47
#define TYPE_STRING
Definition field.h:46
#define TYPE_LONG
Definition field.h:44
#define TYPE_UNSIGNED_LONG
Definition field.h:45
#define TYPE_SHORT
Definition field.h:42