| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Lasse Tassing |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #include "StdAfx.h" |
|---|
| 27 | |
|---|
| 28 | CDDParse::CDDParse(void) |
|---|
| 29 | { |
|---|
| 30 | REGISTER_MODULE("DDParse"); |
|---|
| 31 | } |
|---|
| 32 | CDDParse::~CDDParse(void) |
|---|
| 33 | { |
|---|
| 34 | UNREGISTER_MODULE; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | // Parse the ASCII string to a DDObject |
|---|
| 38 | // (can return NULL, if a fatal error occurs) |
|---|
| 39 | CDDObject* CDDParse::Parse(const char *pszASCII) |
|---|
| 40 | { |
|---|
| 41 | m_pszData=new char[strlen(pszASCII)+2]; |
|---|
| 42 | strcpy(m_pszData,pszASCII); |
|---|
| 43 | m_pszDataPtr=m_pszData; |
|---|
| 44 | m_iLine=1; |
|---|
| 45 | |
|---|
| 46 | CDDObject *pObj=ParseObject(); |
|---|
| 47 | delete[] m_pszData; |
|---|
| 48 | return pObj; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // Try to load specified file and parse it as ASCII DDObject |
|---|
| 52 | CDDObject* CDDParse::ParseFromFile(const char *pszFilename) |
|---|
| 53 | { |
|---|
| 54 | // Open file |
|---|
| 55 | HANDLE hFile=CreateFile(pszFilename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); |
|---|
| 56 | |
|---|
| 57 | // Try with different sharing |
|---|
| 58 | if(hFile==INVALID_HANDLE_VALUE) hFile=CreateFile(pszFilename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); |
|---|
| 59 | |
|---|
| 60 | // Check error code |
|---|
| 61 | if(hFile==INVALID_HANDLE_VALUE) |
|---|
| 62 | { |
|---|
| 63 | LOGERROR "Unable to open config file: %s", pszFilename); |
|---|
| 64 | return false; |
|---|
| 65 | } |
|---|
| 66 | // Get Size of file |
|---|
| 67 | DWORD lLen=GetFileSize(hFile, NULL); |
|---|
| 68 | |
|---|
| 69 | // Allocate and read |
|---|
| 70 | m_pszData=new char[lLen+2]; |
|---|
| 71 | DWORD dwRead=0; |
|---|
| 72 | ReadFile(hFile, m_pszData, lLen, &dwRead, 0); |
|---|
| 73 | if(dwRead!=lLen) |
|---|
| 74 | { |
|---|
| 75 | LOGERROR "Failed to read from config file: %s", pszFilename); |
|---|
| 76 | CloseHandle(hFile); |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // Zero terminate |
|---|
| 81 | m_pszData[lLen]=0; |
|---|
| 82 | CloseHandle(hFile); |
|---|
| 83 | |
|---|
| 84 | m_pszDataPtr=m_pszData; |
|---|
| 85 | m_iLine=1; |
|---|
| 86 | |
|---|
| 87 | // Replace |
|---|
| 88 | char buffer[_MAX_PATH]; |
|---|
| 89 | LPTSTR pFilePart; |
|---|
| 90 | GetFullPathName(pszFilename, _MAX_PATH, buffer, &pFilePart); |
|---|
| 91 | *pFilePart=0; |
|---|
| 92 | if(buffer[strlen(buffer)-1]=='\\') buffer[strlen(buffer)-1]=0; |
|---|
| 93 | // if(buffer[strlen(buffer)-1]!='\\') strcat(buffer, "\\"); |
|---|
| 94 | faststring sSearch="$CurrentDir"; |
|---|
| 95 | faststring sReplace=buffer; |
|---|
| 96 | DoReplace(sSearch, sReplace); |
|---|
| 97 | |
|---|
| 98 | // Parse |
|---|
| 99 | CDDObject *pObj=ParseObject(); |
|---|
| 100 | delete[] m_pszData; |
|---|
| 101 | |
|---|
| 102 | return pObj; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // ----------- |
|---|
| 106 | void CDDParse::ParseDefine(void) |
|---|
| 107 | { |
|---|
| 108 | faststring sSearchStr, sReplaceStr; |
|---|
| 109 | |
|---|
| 110 | // Skip spaces to search string |
|---|
| 111 | SkipSpaces(); |
|---|
| 112 | const char *pszStartSearch=m_pszDataPtr; |
|---|
| 113 | |
|---|
| 114 | // Skip to end of search directive |
|---|
| 115 | while(!isspace(*m_pszDataPtr) && *m_pszDataPtr) ++m_pszDataPtr; |
|---|
| 116 | if(!m_pszDataPtr) |
|---|
| 117 | { |
|---|
| 118 | LOGWARNING "#define directive - abnormal string termination (missing space?) (line=%d)", m_iLine); |
|---|
| 119 | return; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | // Assign the string |
|---|
| 123 | sSearchStr.assign(pszStartSearch,(m_pszDataPtr-pszStartSearch)); |
|---|
| 124 | |
|---|
| 125 | // Skip to replace token |
|---|
| 126 | SkipSpaces(); |
|---|
| 127 | |
|---|
| 128 | const char *pszEnd=strchr(m_pszDataPtr,'\r'); |
|---|
| 129 | if(!pszEnd) |
|---|
| 130 | { |
|---|
| 131 | LOGWARNING "#define must run to EOL (line=%d)", m_iLine); |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | sReplaceStr.assign(m_pszDataPtr,(pszEnd-m_pszDataPtr)); |
|---|
| 135 | m_pszDataPtr=pszEnd+1; |
|---|
| 136 | |
|---|
| 137 | // Replace |
|---|
| 138 | DoReplace(sSearchStr, sReplaceStr); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | // ----------- |
|---|
| 142 | void CDDParse::DoReplace(faststring &sSearchStr, faststring &sReplaceStr) |
|---|
| 143 | { |
|---|
| 144 | int iOverlap=sSearchStr.size()-sReplaceStr.size(); |
|---|
| 145 | // Enough or more space to simply replace? |
|---|
| 146 | if(iOverlap>=0) |
|---|
| 147 | { |
|---|
| 148 | char *pszReplace=(char*)m_pszDataPtr; |
|---|
| 149 | while(pszReplace!=0) |
|---|
| 150 | { |
|---|
| 151 | pszReplace=strstr(pszReplace,sSearchStr.c_str()); |
|---|
| 152 | if(pszReplace) |
|---|
| 153 | { |
|---|
| 154 | strncpy(pszReplace, sReplaceStr.c_str(), sReplaceStr.size()); |
|---|
| 155 | pszReplace+=sReplaceStr.size(); |
|---|
| 156 | |
|---|
| 157 | if(iOverlap) |
|---|
| 158 | memcpy(pszReplace,pszReplace+iOverlap,strlen(pszReplace)-iOverlap); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } else |
|---|
| 162 | { |
|---|
| 163 | int iReplaceCount=0; |
|---|
| 164 | char *pszReplace=(char*)m_pszDataPtr; |
|---|
| 165 | while(pszReplace!=0) |
|---|
| 166 | { |
|---|
| 167 | pszReplace=strstr(pszReplace,sSearchStr.c_str()); |
|---|
| 168 | if(pszReplace) |
|---|
| 169 | { |
|---|
| 170 | iReplaceCount++; |
|---|
| 171 | pszReplace+=sSearchStr.size(); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | if(iReplaceCount) |
|---|
| 175 | { |
|---|
| 176 | iOverlap=-iOverlap; // negate the overlap count |
|---|
| 177 | |
|---|
| 178 | // BOUNDS check this section! |
|---|
| 179 | char *pszNewData=new char[(iReplaceCount*iOverlap)+strlen(m_pszData)+2]; |
|---|
| 180 | memset(pszNewData, 0, (iReplaceCount*iOverlap)+strlen(m_pszData)+2); |
|---|
| 181 | |
|---|
| 182 | // Copy the original data |
|---|
| 183 | strcpy(pszNewData,m_pszData); |
|---|
| 184 | |
|---|
| 185 | // Get the index |
|---|
| 186 | int iIndex=m_pszDataPtr-m_pszData; |
|---|
| 187 | |
|---|
| 188 | // Setup the replacment pointer |
|---|
| 189 | pszReplace=pszNewData+iIndex; |
|---|
| 190 | while(pszReplace!=0) |
|---|
| 191 | { |
|---|
| 192 | // Try to find the search string |
|---|
| 193 | pszReplace=strstr(pszReplace,sSearchStr.c_str()); |
|---|
| 194 | if(pszReplace) |
|---|
| 195 | { |
|---|
| 196 | // Found, move the rest to make room for the replacement |
|---|
| 197 | memmove( pszReplace+sReplaceStr.size(), |
|---|
| 198 | pszReplace+(sReplaceStr.size()-iOverlap), |
|---|
| 199 | strlen(pszReplace)-(sReplaceStr.size()-iOverlap)); |
|---|
| 200 | strncpy(pszReplace,sReplaceStr.c_str(),sReplaceStr.size()); |
|---|
| 201 | pszReplace+=sReplaceStr.size(); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | // Free the original data and setup the pointers for the new. |
|---|
| 205 | delete[] m_pszData; |
|---|
| 206 | m_pszDataPtr=pszNewData+iIndex; |
|---|
| 207 | m_pszData=pszNewData; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | // ----------- |
|---|
| 213 | void CDDParse::ParseInclude(void) |
|---|
| 214 | { |
|---|
| 215 | faststring sIncPath; |
|---|
| 216 | GetNextToken(sIncPath); |
|---|
| 217 | |
|---|
| 218 | // THIS IS A FIX: The first time the #include occurs it has not been replaced. |
|---|
| 219 | char buffer1[_MAX_PATH]; |
|---|
| 220 | strcpy(buffer1, sIncPath.c_str()); |
|---|
| 221 | char* pDest = strstr(buffer1,"$CurrentDir"); |
|---|
| 222 | if(pDest != NULL) |
|---|
| 223 | { |
|---|
| 224 | //replace $CurrentDir token |
|---|
| 225 | pDest = strstr(buffer1,"\\"); |
|---|
| 226 | pDest++; |
|---|
| 227 | // Replace |
|---|
| 228 | char buffer2[_MAX_PATH]; |
|---|
| 229 | LPTSTR pFilePart2; |
|---|
| 230 | GetFullPathName(pDest, _MAX_PATH, buffer2, &pFilePart2); |
|---|
| 231 | |
|---|
| 232 | sIncPath.clear(); |
|---|
| 233 | sIncPath.add( buffer2 ); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | // Open file |
|---|
| 237 | HANDLE hFile=CreateFile(sIncPath.c_str(),GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0); |
|---|
| 238 | |
|---|
| 239 | // Try with different sharing |
|---|
| 240 | if(hFile==INVALID_HANDLE_VALUE) hFile=CreateFile(sIncPath.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); |
|---|
| 241 | |
|---|
| 242 | // |
|---|
| 243 | if(hFile==INVALID_HANDLE_VALUE) |
|---|
| 244 | { |
|---|
| 245 | LOGERROR "Unable to open include file"); |
|---|
| 246 | return ; |
|---|
| 247 | } |
|---|
| 248 | DWORD dwSize=GetFileSize(hFile,NULL); |
|---|
| 249 | if(!dwSize) |
|---|
| 250 | { |
|---|
| 251 | CloseHandle(hFile); |
|---|
| 252 | return; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | char *pszNewData=new char[dwSize+strlen(m_pszData)+2]; |
|---|
| 256 | /* int iIndex=m_pszDataPtr-m_pszData; |
|---|
| 257 | |
|---|
| 258 | m_pszDataPtr=pszNewData+iIndex; |
|---|
| 259 | // Copy the first of the original data |
|---|
| 260 | memcpy(pszNewData,m_pszData,iIndex);*/ |
|---|
| 261 | |
|---|
| 262 | // Copy the included data |
|---|
| 263 | DWORD dwBytesRead; |
|---|
| 264 | // ReadFile(hFile,pszNewData+iIndex,dwSize,&dwBytesRead,NULL); |
|---|
| 265 | ReadFile(hFile,pszNewData,dwSize,&dwBytesRead,NULL); |
|---|
| 266 | CloseHandle(hFile); |
|---|
| 267 | |
|---|
| 268 | // memcpy(pszNewData+iIndex+dwBytesRead,m_pszData+iIndex,strlen(m_pszData)-iIndex); |
|---|
| 269 | memcpy(pszNewData+dwBytesRead,m_pszDataPtr,strlen(m_pszDataPtr)); |
|---|
| 270 | pszNewData[dwBytesRead+strlen(m_pszDataPtr)]=0; |
|---|
| 271 | delete[] m_pszData; |
|---|
| 272 | m_pszData=pszNewData; |
|---|
| 273 | m_pszDataPtr=m_pszData; |
|---|
| 274 | |
|---|
| 275 | // Replace |
|---|
| 276 | char buffer[_MAX_PATH]; |
|---|
| 277 | LPTSTR pFilePart; |
|---|
| 278 | GetFullPathName(sIncPath.c_str(), _MAX_PATH, buffer, &pFilePart); |
|---|
| 279 | *pFilePart=0; |
|---|
| 280 | // if(buffer[strlen(buffer)-1]!='\\') strcat(buffer, "\\"); |
|---|
| 281 | if(buffer[strlen(buffer)-1]=='\\') buffer[strlen(buffer)-1]=0; |
|---|
| 282 | faststring sSearch="$CurrentDir"; |
|---|
| 283 | faststring sReplace=buffer; |
|---|
| 284 | DoReplace(sSearch, sReplace); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | // ----------- |
|---|
| 288 | CDDObject *CDDParse::ParseObject(void) |
|---|
| 289 | { |
|---|
| 290 | CDDObject *pNewObject=new CDDObject; |
|---|
| 291 | |
|---|
| 292 | do |
|---|
| 293 | { |
|---|
| 294 | faststring sID; |
|---|
| 295 | if(!GetNextToken(sID)) break; |
|---|
| 296 | if(sID[0]=='}') break; // End of section |
|---|
| 297 | // Include / Define statments |
|---|
| 298 | if(sID[0]=='#') |
|---|
| 299 | { |
|---|
| 300 | faststring sTmp; |
|---|
| 301 | GetNextToken(sTmp); |
|---|
| 302 | // Handle defines |
|---|
| 303 | if(_stricmp(sTmp.c_str(),"define")==0) |
|---|
| 304 | { |
|---|
| 305 | ParseDefine(); |
|---|
| 306 | continue; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | // Handle includes |
|---|
| 310 | if(_stricmp(sTmp.c_str(),"include")==0) |
|---|
| 311 | { |
|---|
| 312 | ParseInclude(); |
|---|
| 313 | continue; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | if(IsTokenChar(sID[0])) |
|---|
| 318 | { |
|---|
| 319 | LOGWARNING "Expected to find value identifier, but found token char (line=%d, ID=%s) ", m_iLine, sID.c_str()); |
|---|
| 320 | break; |
|---|
| 321 | } |
|---|
| 322 | faststring sTmp; |
|---|
| 323 | if(!GetNextToken(sTmp)) |
|---|
| 324 | { |
|---|
| 325 | LOGWARNING "Expected token after value identifier (line=%d, ID=%s) ", m_iLine, sID.c_str()); |
|---|
| 326 | break; |
|---|
| 327 | } |
|---|
| 328 | if(sTmp[0]!='=') LOGWARNING "Expected '=' sign after value identifier (line=%d, ID=%s, Value=%s)", m_iLine, sID.c_str(), sTmp.c_str()); |
|---|
| 329 | if(!GetNextToken(sTmp)) |
|---|
| 330 | { |
|---|
| 331 | LOGWARNING "Expected token after '=' (line=%d, ID=%s, Value=%s)", m_iLine, sID.c_str(), sTmp.c_str()); |
|---|
| 332 | break; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | faststring sType=""; |
|---|
| 336 | // Is the next token type specifier? |
|---|
| 337 | if(sTmp[0]=='(') |
|---|
| 338 | { // yep, let's parse the type |
|---|
| 339 | if(!GetNextToken(sType)) |
|---|
| 340 | { |
|---|
| 341 | LOGWARNING "Expected type specifier after '(' (line=%d)", m_iLine); |
|---|
| 342 | break; |
|---|
| 343 | } |
|---|
| 344 | if(!GetNextToken(sTmp) || sTmp[0]!=')') |
|---|
| 345 | { |
|---|
| 346 | LOGWARNING "Expected ')' after type specifier (line=%d)", m_iLine); |
|---|
| 347 | break; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | if(sType.compare("int")==0) |
|---|
| 351 | ParseInt(sID.c_str(), pNewObject); |
|---|
| 352 | else if(sType.compare("float")==0) |
|---|
| 353 | ParseFloat(sID.c_str(), pNewObject); |
|---|
| 354 | else if(sType.compare("string")==0) |
|---|
| 355 | ParseString(sID.c_str(), pNewObject); |
|---|
| 356 | // else if(sType.compare("binary")==0) |
|---|
| 357 | // ParseBinary(sID.c_str(), pNewObject); |
|---|
| 358 | else if(sType.compare("ddobject")==0) |
|---|
| 359 | ParseDD(sID.c_str(), pNewObject); |
|---|
| 360 | else if(sType.compare("intlist")==0) |
|---|
| 361 | ParseIntList(sID.c_str(), pNewObject); |
|---|
| 362 | else if(sType.compare("floatlist")==0) |
|---|
| 363 | ParseFloatList(sID.c_str(), pNewObject); |
|---|
| 364 | else if(sType.compare("stringlist")==0) |
|---|
| 365 | ParseStringList(sID.c_str(),pNewObject); |
|---|
| 366 | else if(sType.compare("ddlist")==0) |
|---|
| 367 | ParseDDList(sID.c_str(), pNewObject); |
|---|
| 368 | else if(sType.compare("vec3")==0) |
|---|
| 369 | ParseVec3(sID.c_str(), pNewObject); |
|---|
| 370 | else if(sType.compare("vec3list")==0) |
|---|
| 371 | ParseVec3List(sID.c_str(), pNewObject); |
|---|
| 372 | else if(sType.compare("bool")==0) |
|---|
| 373 | ParseBool(sID.c_str(), pNewObject); |
|---|
| 374 | else if(sType.compare("vec2")==0) |
|---|
| 375 | ParseVec2(sID.c_str(), pNewObject); |
|---|
| 376 | else if(sType.compare("vec4")==0) |
|---|
| 377 | ParseVec4(sID.c_str(), pNewObject); |
|---|
| 378 | else if(sType.compare("vec4list")==0) |
|---|
| 379 | ParseVec4List(sID.c_str(), pNewObject); |
|---|
| 380 | else if(sType.compare("matrix")==0) |
|---|
| 381 | ParseMatrix(sID.c_str(), pNewObject); |
|---|
| 382 | else if(sType.compare("matrixlist")==0) |
|---|
| 383 | ParseMatrixList(sID.c_str(), pNewObject); |
|---|
| 384 | else |
|---|
| 385 | LOGWARNING "Unknown type identifier found (%s) at line %d", sType.c_str(), m_iLine); |
|---|
| 386 | } else |
|---|
| 387 | { |
|---|
| 388 | ParseUnknown(sTmp.c_str(), sID.c_str(), pNewObject); |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | GetNextToken(sTmp); |
|---|
| 392 | if(sTmp[0]!=';') |
|---|
| 393 | LOGWARNING "Expected ';' after value (line=%d, sID=%s, Value=%s)", m_iLine-1, sID.c_str(), sTmp.c_str()); |
|---|
| 394 | } while(true); |
|---|
| 395 | return pNewObject; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | // ----------- |
|---|
| 399 | bool CDDParse::IsTokenChar(const char cChar) |
|---|
| 400 | { |
|---|
| 401 | return (cChar=='=' || cChar==',' || cChar==';' || |
|---|
| 402 | cChar=='{' || cChar=='}' || cChar=='(' || cChar==')' || cChar=='#' ); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | // ----------- |
|---|
| 406 | bool CDDParse::GetNextToken(faststring& sResult) |
|---|
| 407 | { |
|---|
| 408 | // Skip white spacing |
|---|
| 409 | do |
|---|
| 410 | { |
|---|
| 411 | SkipSpaces(); |
|---|
| 412 | if(!*m_pszDataPtr) return false; |
|---|
| 413 | if(*m_pszDataPtr=='/' && *(m_pszDataPtr+1)=='*') |
|---|
| 414 | { |
|---|
| 415 | m_pszDataPtr+=2; |
|---|
| 416 | m_pszDataPtr=strstr(m_pszDataPtr, "*/"); |
|---|
| 417 | if(m_pszDataPtr==NULL) return false; |
|---|
| 418 | m_pszDataPtr+=2; |
|---|
| 419 | continue; |
|---|
| 420 | } |
|---|
| 421 | if(*m_pszDataPtr!='/' || *(m_pszDataPtr+1)!='/') break; |
|---|
| 422 | SkipToEOL(); |
|---|
| 423 | } while(true); |
|---|
| 424 | |
|---|
| 425 | // Check for end of line (or data) |
|---|
| 426 | if(!*m_pszDataPtr || *m_pszDataPtr=='\r' || *m_pszDataPtr=='\n') return false; |
|---|
| 427 | |
|---|
| 428 | // Check if we hit a token char |
|---|
| 429 | if(IsTokenChar(*m_pszDataPtr)) |
|---|
| 430 | { |
|---|
| 431 | sResult=*m_pszDataPtr; |
|---|
| 432 | m_pszDataPtr++; |
|---|
| 433 | return true; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | // Check for quoted token |
|---|
| 437 | if( *m_pszDataPtr=='"' ) |
|---|
| 438 | { |
|---|
| 439 | m_pszDataPtr++; // skip quote |
|---|
| 440 | |
|---|
| 441 | // TODO! This section should check for escaped characters |
|---|
| 442 | |
|---|
| 443 | // Find endquote |
|---|
| 444 | const char *pszEnd=strchr(m_pszDataPtr,'"'); |
|---|
| 445 | if(pszEnd==NULL) |
|---|
| 446 | { |
|---|
| 447 | LOGWARNING "Missing endquote after '%.20s' at line %d", m_pszDataPtr, m_iLine); |
|---|
| 448 | return false; |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | // Assign entire quoted expression |
|---|
| 452 | sResult.assign(m_pszDataPtr,(pszEnd-m_pszDataPtr)); |
|---|
| 453 | |
|---|
| 454 | // Go to end of quoted expression and skip endquote |
|---|
| 455 | m_pszDataPtr=pszEnd+1; |
|---|
| 456 | return true; |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | const char *pszBegin=m_pszDataPtr; |
|---|
| 460 | while( *m_pszDataPtr && !isspace(*m_pszDataPtr) && !IsTokenChar(*m_pszDataPtr)) |
|---|
| 461 | { |
|---|
| 462 | m_pszDataPtr++; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | sResult.assign(pszBegin, (m_pszDataPtr)-pszBegin); |
|---|
| 466 | return true; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | // ----------- |
|---|
| 470 | void CDDParse::ParseUnknown(const char *pszUnknown, const char *pszID,CDDObject *pObj) |
|---|
| 471 | { |
|---|
| 472 | char *pEndPtr; |
|---|
| 473 | int iValue=strtol(pszUnknown,&pEndPtr,0); |
|---|
| 474 | if(!iValue && pEndPtr==pszUnknown) |
|---|
| 475 | pObj->SetString(pszID, pszUnknown); |
|---|
| 476 | else |
|---|
| 477 | pObj->SetInt(pszID, iValue); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | // ----------- |
|---|
| 481 | void CDDParse::ParseInt(const char *pszID,CDDObject *pObj) |
|---|
| 482 | { |
|---|
| 483 | faststring sNumber; |
|---|
| 484 | if(GetNextToken(sNumber)) |
|---|
| 485 | { |
|---|
| 486 | char *pEndPtr; |
|---|
| 487 | int iValue=strtol(sNumber.c_str(),&pEndPtr,0); |
|---|
| 488 | pObj->SetInt(pszID,iValue); |
|---|
| 489 | } else |
|---|
| 490 | { |
|---|
| 491 | LOGWARNING "Could not get string value (parsing int %s) (line=%d)", pszID, m_iLine); |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | // ----------- |
|---|
| 496 | void CDDParse::ParseFloat(const char *pszID,CDDObject *pObj) |
|---|
| 497 | { |
|---|
| 498 | faststring sNumber; |
|---|
| 499 | if(GetNextToken(sNumber)) |
|---|
| 500 | { |
|---|
| 501 | char *pEndPtr; |
|---|
| 502 | double fValue=strtod(sNumber.c_str(),&pEndPtr); |
|---|
| 503 | pObj->SetFloat(pszID,(float)fValue); |
|---|
| 504 | } else |
|---|
| 505 | { |
|---|
| 506 | LOGWARNING "Could not get string value (parsing float %s) (line=%d)", pszID, m_iLine); |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | // ----------- |
|---|
| 511 | void CDDParse::ParseString(const char *pszID,CDDObject *pObj) |
|---|
| 512 | { |
|---|
| 513 | faststring sString; |
|---|
| 514 | if(GetNextToken(sString)) |
|---|
| 515 | { |
|---|
| 516 | pObj->SetString(pszID,sString.c_str()); |
|---|
| 517 | } else |
|---|
| 518 | { |
|---|
| 519 | LOGWARNING "Could not get string value at line %d", m_iLine); |
|---|
| 520 | } |
|---|
| 521 | } |
|---|
| 522 | /* |
|---|
| 523 | // ----------- |
|---|
| 524 | void CDDParse::ParseBinary(const char *pszID,CDDObject *pObj) |
|---|
| 525 | { |
|---|
| 526 | string sBinary; |
|---|
| 527 | if(GetNextToken(sBinary) && sBinary.size()>1) |
|---|
| 528 | { |
|---|
| 529 | if(sBinary.size()&1) |
|---|
| 530 | LOGWARNING "Binary value has uneven number of hex values"); |
|---|
| 531 | |
|---|
| 532 | int iBinLen=sBinary.size()/2; |
|---|
| 533 | BYTE *pBuffer=new BYTE[iBinLen]; |
|---|
| 534 | char digitbuf[6]; |
|---|
| 535 | strcpy(digitbuf,"0x"); // enforce heximal decode |
|---|
| 536 | digitbuf[4]=0; |
|---|
| 537 | for(int i=0;i<iBinLen;i++) |
|---|
| 538 | { |
|---|
| 539 | // copy the digits to the buffer |
|---|
| 540 | // (digitbuffer will then have the format 0xXX, where XX is the two copied |
|---|
| 541 | // ascii digits) |
|---|
| 542 | strncpy(&digitbuf[2],sBinary.c_str()+(i*2),2); |
|---|
| 543 | // convert the string to actual value |
|---|
| 544 | char *pTmpPtr; |
|---|
| 545 | pBuffer[i]=(BYTE)strtoul(digitbuf,&pTmpPtr,0); |
|---|
| 546 | } |
|---|
| 547 | pObj->SetBinary(sID.c_str(),pBuffer,iBinLen); |
|---|
| 548 | delete pBuffer; |
|---|
| 549 | } else |
|---|
| 550 | { |
|---|
| 551 | LOGERROR "Could not get string value (parsing binary)"); |
|---|
| 552 | } |
|---|
| 553 | }*/ |
|---|
| 554 | |
|---|
| 555 | // ----------- |
|---|
| 556 | void CDDParse::ParseDD(const char *pszID,CDDObject *pObj) |
|---|
| 557 | { |
|---|
| 558 | faststring sTmp; |
|---|
| 559 | GetNextToken(sTmp); |
|---|
| 560 | if(sTmp[0]!='{') |
|---|
| 561 | LOGWARNING "DDObject definitions should start with '{' (line=%d)", m_iLine); |
|---|
| 562 | CDDObject *pDDObj=ParseObject(); |
|---|
| 563 | if(pDDObj) |
|---|
| 564 | { |
|---|
| 565 | pObj->SetDDObject(pszID,pDDObj); |
|---|
| 566 | pDDObj->Release(); |
|---|
| 567 | } |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | // ----------- |
|---|
| 571 | void CDDParse::ParseDDList(const char *pszID,CDDObject *pObj) |
|---|
| 572 | { |
|---|
| 573 | fastvector<const CDDObject*> lDDObjects; |
|---|
| 574 | |
|---|
| 575 | faststring sTmp; |
|---|
| 576 | GetNextToken(sTmp); |
|---|
| 577 | if(sTmp[0]!='{') |
|---|
| 578 | LOGWARNING "DDlist definitions should start with '{' (line=%d)", m_iLine); |
|---|
| 579 | |
|---|
| 580 | do |
|---|
| 581 | { |
|---|
| 582 | GetNextToken(sTmp); |
|---|
| 583 | if(sTmp[0]!='{') |
|---|
| 584 | LOGWARNING "DDObject definitions should start with '{' (line=%d)", m_iLine); |
|---|
| 585 | CDDObject *pDDObj=ParseObject(); |
|---|
| 586 | lDDObjects.push_back(pDDObj); |
|---|
| 587 | // pDDObj->Release() should not be necessary |
|---|
| 588 | |
|---|
| 589 | GetNextToken(sTmp); |
|---|
| 590 | } while(sTmp[0]==','); |
|---|
| 591 | if(sTmp[0]!='}') |
|---|
| 592 | LOGWARNING "DDList definition should end with '}' (line=%d)", m_iLine); |
|---|
| 593 | pObj->SetDDList(pszID, lDDObjects,false); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | // ----------- |
|---|
| 597 | void CDDParse::ParseIntList(const char *pszID,CDDObject *pObj) |
|---|
| 598 | { |
|---|
| 599 | fastvector<int> lValues; |
|---|
| 600 | int iLevel=0; |
|---|
| 601 | faststring sTmp=""; |
|---|
| 602 | do |
|---|
| 603 | { |
|---|
| 604 | GetNextToken(sTmp); |
|---|
| 605 | if(sTmp[0]=='{') iLevel++; |
|---|
| 606 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 607 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 608 | { |
|---|
| 609 | char *pEndPtr; |
|---|
| 610 | lValues.push_back(strtol(sTmp.c_str(),&pEndPtr,0)); |
|---|
| 611 | } |
|---|
| 612 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 613 | pObj->SetIntList(pszID, lValues); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | // ----------- |
|---|
| 617 | void CDDParse::ParseFloatList(const char *pszID,CDDObject *pObj) |
|---|
| 618 | { |
|---|
| 619 | fastvector<float> lValues; |
|---|
| 620 | int iLevel=0; |
|---|
| 621 | faststring sTmp=""; |
|---|
| 622 | do |
|---|
| 623 | { |
|---|
| 624 | GetNextToken(sTmp); |
|---|
| 625 | if(sTmp[0]=='{') iLevel++; |
|---|
| 626 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 627 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 628 | { |
|---|
| 629 | char *pEndPtr; |
|---|
| 630 | lValues.push_back((float)strtod(sTmp.c_str(),&pEndPtr)); |
|---|
| 631 | } |
|---|
| 632 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 633 | pObj->SetFloatList(pszID, lValues); |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | // ----------- |
|---|
| 637 | void CDDParse::ParseStringList(const char *pszID,CDDObject *pObj) |
|---|
| 638 | { |
|---|
| 639 | vector<faststring> lValues; |
|---|
| 640 | int iLevel=0; |
|---|
| 641 | faststring sTmp=""; |
|---|
| 642 | do |
|---|
| 643 | { |
|---|
| 644 | GetNextToken(sTmp); |
|---|
| 645 | if(sTmp[0]=='{') iLevel++; |
|---|
| 646 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 647 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 648 | lValues.push_back(sTmp); |
|---|
| 649 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 650 | pObj->SetStringList(pszID, lValues); |
|---|
| 651 | } |
|---|
| 652 | /* |
|---|
| 653 | // ----------- |
|---|
| 654 | void CDDParse::ParseStringList(const char *pszID,CDDObject *pObj) |
|---|
| 655 | { |
|---|
| 656 | vector<string> lValues; |
|---|
| 657 | int iLevel=0; |
|---|
| 658 | string sTmp=""; |
|---|
| 659 | do |
|---|
| 660 | { |
|---|
| 661 | GetNextToken(sTmp); |
|---|
| 662 | if(sTmp[0]=='{') iLevel++; |
|---|
| 663 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 664 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 665 | lValues.push_back(sTmp); |
|---|
| 666 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 667 | pObj->SetStringList(sID.c_str(),&lValues); |
|---|
| 668 | }*/ |
|---|
| 669 | |
|---|
| 670 | void CDDParse::SkipToEOL(void) |
|---|
| 671 | { |
|---|
| 672 | while(*m_pszDataPtr!='\n' && *m_pszDataPtr) m_pszDataPtr++; |
|---|
| 673 | // ++m_iLine; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | void CDDParse::SkipSpaces(void) |
|---|
| 677 | { |
|---|
| 678 | while(*m_pszDataPtr && (*m_pszDataPtr=='\r' || *m_pszDataPtr=='\n' || isspace(*m_pszDataPtr))) |
|---|
| 679 | { |
|---|
| 680 | if(*(m_pszDataPtr++)=='\r') ++m_iLine; |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | // ----------- |
|---|
| 685 | void CDDParse::ParseVec3(const char *pszID,CDDObject *pObj) |
|---|
| 686 | { |
|---|
| 687 | faststring sNumber; |
|---|
| 688 | if(GetNextToken(sNumber)) |
|---|
| 689 | { |
|---|
| 690 | char *pEndPtr; |
|---|
| 691 | double fValue=strtod(sNumber.c_str(),&pEndPtr); |
|---|
| 692 | pObj->SetFloat(pszID,(float)fValue); |
|---|
| 693 | } else |
|---|
| 694 | { |
|---|
| 695 | LOGWARNING "Could not get string value (parsing vec3 %s) (line=%d)", pszID, m_iLine); |
|---|
| 696 | } |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | // ----------- |
|---|
| 700 | void CDDParse::ParseVec3List(const char *pszID,CDDObject *pObj) |
|---|
| 701 | { |
|---|
| 702 | fastvector<float> lValues; |
|---|
| 703 | int iLevel=0; |
|---|
| 704 | faststring sTmp=""; |
|---|
| 705 | do |
|---|
| 706 | { |
|---|
| 707 | GetNextToken(sTmp); |
|---|
| 708 | if(sTmp[0]=='{') iLevel++; |
|---|
| 709 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 710 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 711 | { |
|---|
| 712 | char *pEndPtr; |
|---|
| 713 | lValues.push_back((float)strtod(sTmp.c_str(),&pEndPtr)); |
|---|
| 714 | } |
|---|
| 715 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 716 | pObj->SetFloatList(pszID, lValues); |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | // ----------- |
|---|
| 720 | void CDDParse::ParseBool(const char *pszID,CDDObject *pObj) |
|---|
| 721 | { |
|---|
| 722 | faststring sNumber; |
|---|
| 723 | if(GetNextToken(sNumber)) |
|---|
| 724 | { |
|---|
| 725 | bool bValue = _stricmp(sNumber.c_str(), "true") == 0 ? true : false; |
|---|
| 726 | pObj->SetBool(pszID,bValue); |
|---|
| 727 | } else |
|---|
| 728 | { |
|---|
| 729 | LOGWARNING "Could not get string value (parsing int %s) (line=%d)", pszID, m_iLine); |
|---|
| 730 | } |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | // ----------- |
|---|
| 734 | void CDDParse::ParseVec2(const char *pszID,CDDObject *pObj) |
|---|
| 735 | { |
|---|
| 736 | faststring sNumber; |
|---|
| 737 | if(GetNextToken(sNumber)) |
|---|
| 738 | { |
|---|
| 739 | char *pEndPtr; |
|---|
| 740 | double fValue=strtod(sNumber.c_str(),&pEndPtr); |
|---|
| 741 | pObj->SetFloat(pszID,(float)fValue); |
|---|
| 742 | } else |
|---|
| 743 | { |
|---|
| 744 | LOGWARNING "Could not get string value (parsing vec2 %s) (line=%d)", pszID, m_iLine); |
|---|
| 745 | } |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | // ----------- |
|---|
| 749 | void CDDParse::ParseVec4(const char *pszID,CDDObject *pObj) |
|---|
| 750 | { |
|---|
| 751 | faststring sNumber; |
|---|
| 752 | if(GetNextToken(sNumber)) |
|---|
| 753 | { |
|---|
| 754 | char *pEndPtr; |
|---|
| 755 | double fValue=strtod(sNumber.c_str(),&pEndPtr); |
|---|
| 756 | pObj->SetFloat(pszID,(float)fValue); |
|---|
| 757 | } else |
|---|
| 758 | { |
|---|
| 759 | LOGWARNING "Could not get string value (parsing vec4 %s) (line=%d)", pszID, m_iLine); |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | // ----------- |
|---|
| 764 | void CDDParse::ParseVec4List(const char *pszID,CDDObject *pObj) |
|---|
| 765 | { |
|---|
| 766 | fastvector<float> lValues; |
|---|
| 767 | int iLevel=0; |
|---|
| 768 | faststring sTmp=""; |
|---|
| 769 | do |
|---|
| 770 | { |
|---|
| 771 | GetNextToken(sTmp); |
|---|
| 772 | if(sTmp[0]=='{') iLevel++; |
|---|
| 773 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 774 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 775 | { |
|---|
| 776 | char *pEndPtr; |
|---|
| 777 | lValues.push_back((float)strtod(sTmp.c_str(),&pEndPtr)); |
|---|
| 778 | } |
|---|
| 779 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 780 | pObj->SetFloatList(pszID, lValues); |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | // ----------- |
|---|
| 784 | void CDDParse::ParseMatrix(const char *pszID,CDDObject *pObj) |
|---|
| 785 | { |
|---|
| 786 | faststring sNumber; |
|---|
| 787 | if(GetNextToken(sNumber)) |
|---|
| 788 | { |
|---|
| 789 | char *pEndPtr; |
|---|
| 790 | double fValue=strtod(sNumber.c_str(),&pEndPtr); |
|---|
| 791 | pObj->SetFloat(pszID,(float)fValue); |
|---|
| 792 | } else |
|---|
| 793 | { |
|---|
| 794 | LOGWARNING "Could not get string value (parsing matrix %s) (line=%d)", pszID, m_iLine); |
|---|
| 795 | } |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | // ----------- |
|---|
| 799 | void CDDParse::ParseMatrixList(const char *pszID,CDDObject *pObj) |
|---|
| 800 | { |
|---|
| 801 | fastvector<float> lValues; |
|---|
| 802 | int iLevel=0; |
|---|
| 803 | faststring sTmp=""; |
|---|
| 804 | do |
|---|
| 805 | { |
|---|
| 806 | GetNextToken(sTmp); |
|---|
| 807 | if(sTmp[0]=='{') iLevel++; |
|---|
| 808 | else if(sTmp[0]=='}') iLevel--; |
|---|
| 809 | else if(!IsTokenChar(sTmp[0])) |
|---|
| 810 | { |
|---|
| 811 | char *pEndPtr; |
|---|
| 812 | lValues.push_back((float)strtod(sTmp.c_str(),&pEndPtr)); |
|---|
| 813 | } |
|---|
| 814 | } while(iLevel || sTmp[0]!='}'); |
|---|
| 815 | pObj->SetFloatList(pszID, lValues); |
|---|
| 816 | } |
|---|
| 817 | |
|---|