Token.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ================
  24. idToken::NumberValue
  25. ================
  26. */
  27. void idToken::NumberValue( void ) {
  28. int i, pow, div, c;
  29. const char *p;
  30. double m;
  31. assert( type == TT_NUMBER );
  32. p = c_str();
  33. floatvalue = 0;
  34. intvalue = 0;
  35. // floating point number
  36. if ( subtype & TT_FLOAT ) {
  37. if ( subtype & ( TT_INFINITE | TT_INDEFINITE | TT_NAN ) ) {
  38. if ( subtype & TT_INFINITE ) { // 1.#INF
  39. unsigned int inf = 0x7f800000;
  40. floatvalue = (double) *(float*)&inf;
  41. }
  42. else if ( subtype & TT_INDEFINITE ) { // 1.#IND
  43. unsigned int ind = 0xffc00000;
  44. floatvalue = (double) *(float*)&ind;
  45. }
  46. else if ( subtype & TT_NAN ) { // 1.#QNAN
  47. unsigned int nan = 0x7fc00000;
  48. floatvalue = (double) *(float*)&nan;
  49. }
  50. }
  51. else {
  52. while( *p && *p != '.' && *p != 'e' ) {
  53. floatvalue = floatvalue * 10.0 + (double) (*p - '0');
  54. p++;
  55. }
  56. if ( *p == '.' ) {
  57. p++;
  58. for( m = 0.1; *p && *p != 'e'; p++ ) {
  59. floatvalue = floatvalue + (double) (*p - '0') * m;
  60. m *= 0.1;
  61. }
  62. }
  63. if ( *p == 'e' ) {
  64. p++;
  65. if ( *p == '-' ) {
  66. div = true;
  67. p++;
  68. }
  69. else if ( *p == '+' ) {
  70. div = false;
  71. p++;
  72. }
  73. else {
  74. div = false;
  75. }
  76. pow = 0;
  77. for ( pow = 0; *p; p++ ) {
  78. pow = pow * 10 + (int) (*p - '0');
  79. }
  80. for ( m = 1.0, i = 0; i < pow; i++ ) {
  81. m *= 10.0;
  82. }
  83. if ( div ) {
  84. floatvalue /= m;
  85. }
  86. else {
  87. floatvalue *= m;
  88. }
  89. }
  90. }
  91. intvalue = idMath::Ftol( floatvalue );
  92. }
  93. else if ( subtype & TT_DECIMAL ) {
  94. while( *p ) {
  95. intvalue = intvalue * 10 + (*p - '0');
  96. p++;
  97. }
  98. floatvalue = intvalue;
  99. }
  100. else if ( subtype & TT_IPADDRESS ) {
  101. c = 0;
  102. while( *p && *p != ':' ) {
  103. if ( *p == '.' ) {
  104. while( c != 3 ) {
  105. intvalue = intvalue * 10;
  106. c++;
  107. }
  108. c = 0;
  109. }
  110. else {
  111. intvalue = intvalue * 10 + (*p - '0');
  112. c++;
  113. }
  114. p++;
  115. }
  116. while( c != 3 ) {
  117. intvalue = intvalue * 10;
  118. c++;
  119. }
  120. floatvalue = intvalue;
  121. }
  122. else if ( subtype & TT_OCTAL ) {
  123. // step over the first zero
  124. p += 1;
  125. while( *p ) {
  126. intvalue = (intvalue << 3) + (*p - '0');
  127. p++;
  128. }
  129. floatvalue = intvalue;
  130. }
  131. else if ( subtype & TT_HEX ) {
  132. // step over the leading 0x or 0X
  133. p += 2;
  134. while( *p ) {
  135. intvalue <<= 4;
  136. if (*p >= 'a' && *p <= 'f')
  137. intvalue += *p - 'a' + 10;
  138. else if (*p >= 'A' && *p <= 'F')
  139. intvalue += *p - 'A' + 10;
  140. else
  141. intvalue += *p - '0';
  142. p++;
  143. }
  144. floatvalue = intvalue;
  145. }
  146. else if ( subtype & TT_BINARY ) {
  147. // step over the leading 0b or 0B
  148. p += 2;
  149. while( *p ) {
  150. intvalue = (intvalue << 1) + (*p - '0');
  151. p++;
  152. }
  153. floatvalue = intvalue;
  154. }
  155. subtype |= TT_VALUESVALID;
  156. }
  157. /*
  158. ================
  159. idToken::ClearTokenWhiteSpace
  160. ================
  161. */
  162. void idToken::ClearTokenWhiteSpace( void ) {
  163. whiteSpaceStart_p = NULL;
  164. whiteSpaceEnd_p = NULL;
  165. linesCrossed = 0;
  166. }