FIXED.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/FIXED.CPP 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : FIXED.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 06/20/96 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * fixed::As_ASCII -- Returns a pointer (static) of this number as an ASCII string. *
  32. * fixed::To_ASCII -- Convert a fixed point number into an ASCII string. *
  33. * fixed::fixed -- Constructor for fixed integral from ASCII initializer. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "fixed.h"
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. /*
  41. ** These are some handy fixed point constants. Using these constants instead of manually
  42. ** constructing them is not only faster, but more readable.
  43. */
  44. const fixed fixed::_1_2(1, 2); // 1/2
  45. const fixed fixed::_1_3(1, 3); // 1/3
  46. const fixed fixed::_1_4(1, 4); // 1/4
  47. const fixed fixed::_3_4(3, 4); // 3/4
  48. const fixed fixed::_2_3(2, 3); // 2/3
  49. fixed::fixed(int numerator, int denominator)
  50. {
  51. if (denominator == 0) {
  52. Data.Raw = 0U;
  53. } else {
  54. Data.Raw = (unsigned int)(((unsigned __int64)numerator * PRECISION) / denominator);
  55. }
  56. }
  57. /***********************************************************************************************
  58. * fixed::fixed -- Constructor for fixed integral from ASCII initializer. *
  59. * *
  60. * This will parse the ASCII initialization string into a fixed point number. *
  61. * The source string can be a conventional fixed point representation (e.g., "1.0", ".25") *
  62. * or a percent value (e.g. "100%", "25%", "150%"). For percent values, the trailing "%" *
  63. * is required. *
  64. * *
  65. * INPUT: ascii -- Pointer to the ascii source to translate into a fixed point number. *
  66. * *
  67. * OUTPUT: none *
  68. * *
  69. * WARNINGS: It is possible to specify an ASCII string that has more precision and *
  70. * magnitude than can be represented by the fixed point number. In such a case, *
  71. * the resulting value is undefined. *
  72. * *
  73. * HISTORY: *
  74. * 06/20/1996 JLB : Created. *
  75. *=============================================================================================*/
  76. fixed::fixed(char const * ascii)
  77. {
  78. /*
  79. ** If there is no valid pointer, then default to zero value. This takes care of any
  80. ** compiler confusion that would call this routine when the programmer wanted the
  81. ** integer parameter constructor to be called.
  82. */
  83. if (ascii == NULL) {
  84. Data.Raw = 0U;
  85. return;
  86. }
  87. /*
  88. ** The whole part (if any) always starts with the first legal characters.
  89. */
  90. char const * wholepart = ascii;
  91. /*
  92. ** Skip any leading white space.
  93. */
  94. while (isspace(*ascii)) {
  95. ascii++;
  96. }
  97. /*
  98. ** Determine if the number is expressed as a percentage. Detect this by
  99. ** seeing if there is a trailing "%" character.
  100. */
  101. char const * tptr = ascii;
  102. while (isdigit(*tptr)) {
  103. tptr++;
  104. }
  105. /*
  106. ** Percentage value is specified as a whole number but is presumed to be
  107. ** divided by 100 to get mathematical fixed point percentage value.
  108. */
  109. if (*tptr == '%') { // Removed '/' preceding '%'. ST - 5/8/2019
  110. Data.Raw = (unsigned int)(((unsigned __int64)atoi(ascii) * PRECISION) / 100ULL);
  111. } else {
  112. Data.Composite.Whole = Data.Composite.Fraction = 0U;
  113. if (wholepart && *wholepart != '.') {
  114. Data.Composite.Whole = (unsigned short)atoi(wholepart);
  115. }
  116. const char * fracpart = strchr(ascii, '.');
  117. if (fracpart) fracpart++;
  118. if (fracpart) {
  119. unsigned int frac = (unsigned int)atoi(fracpart);
  120. int len = 0;
  121. unsigned int base = 1;
  122. char const * fptr = fracpart;
  123. while (isdigit(*fptr)) {
  124. fptr++;
  125. len++;
  126. base *= 10U;
  127. }
  128. Data.Composite.Fraction = (unsigned short)(((unsigned __int64)frac * PRECISION) / base);
  129. }
  130. }
  131. }
  132. /***********************************************************************************************
  133. * fixed::To_ASCII -- Convert a fixed point number into an ASCII string. *
  134. * *
  135. * Use this routine to convert this fixed point number into an ASCII null terminated *
  136. * string. This is the counterpart to the fixed point constructor that takes an ASCII *
  137. * string. *
  138. * *
  139. * INPUT: buffer -- Pointer to the buffer to hold the fixed point ASCII string. *
  140. * *
  141. * maxlen -- The length of the buffer. *
  142. * *
  143. * OUTPUT: Returns with the number of characters placed in the buffer. The trailing null is *
  144. * not counted in this total. *
  145. * *
  146. * WARNINGS: none *
  147. * *
  148. * HISTORY: *
  149. * 07/03/1996 JLB : Created. *
  150. *=============================================================================================*/
  151. int fixed::To_ASCII(char * buffer, int maxlen) const
  152. {
  153. if (buffer == NULL) return(0);
  154. /*
  155. ** Determine the whole and fractional parts of the number. The fractional
  156. ** part number is the value in 1000ths.
  157. */
  158. unsigned int whole = Data.Composite.Whole;
  159. unsigned int frac = ((unsigned int)Data.Composite.Fraction * 1000U) / PRECISION;
  160. char tbuffer[32];
  161. /*
  162. ** If there number consists only of a whole part, then the number is simply
  163. ** printed into the buffer. If there is a fractional part, then there
  164. ** will be a decimal place followed by up to three digits of accuracy for the
  165. ** fractional component.
  166. */
  167. if (frac == 0) {
  168. sprintf(tbuffer, "%u", whole);
  169. } else {
  170. sprintf(tbuffer, "%u.%02u", whole, frac);
  171. char * ptr = &tbuffer[strlen(tbuffer)-1];
  172. while (*ptr == '0') {
  173. *ptr = '\0';
  174. ptr--;
  175. }
  176. }
  177. /*
  178. ** If no maximum length to the output buffer was specified, then presume the
  179. ** output buffer is just long enough to store the number and the trailing
  180. ** zero.
  181. */
  182. if (maxlen == -1) {
  183. maxlen = strlen(tbuffer)+1;
  184. }
  185. /*
  186. ** Fill the output buffer with the ASCII number.
  187. */
  188. strncpy(buffer, tbuffer, maxlen);
  189. /*
  190. ** Return with the number of ASCII characters placed into the output buffer.
  191. */
  192. int len = strlen(tbuffer);
  193. if (len < maxlen-1) return(len);
  194. return(maxlen-1);
  195. }
  196. /***********************************************************************************************
  197. * fixed::As_ASCII -- Returns a pointer (static) of this number as an ASCII string. *
  198. * *
  199. * This number will be converted into an ASCII string (using a static buffer) and the *
  200. * string pointer will be returned. *
  201. * *
  202. * INPUT: none *
  203. * *
  204. * OUTPUT: Returns with a pointer to the ASCII representation of this fixed point number. *
  205. * *
  206. * WARNINGS: As with all static return pointers, the pointer is valid only until such time *
  207. * as this routine is called again. *
  208. * *
  209. * HISTORY: *
  210. * 07/03/1996 JLB : Created. *
  211. *=============================================================================================*/
  212. char const * fixed::As_ASCII(void) const
  213. {
  214. static char buffer[32];
  215. To_ASCII(buffer, sizeof(buffer));
  216. return(buffer);
  217. }