fixed.cpp 11 KB

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