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