FIELD.CPP 6.5 KB

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