📄 parsepkg.cpp
字号:
case QUOTED_STRING_TOKEN: expr->exprType=EInstCondPrimTypeString; memcpy(expr->pPrim, &m_tokenValue, sizeof(VARIANTVAL)); break; case ALPHA_TOKEN: { expr->exprType=EInstCondPrimTypeVariable; // check if it is "option1", "option2",... if(!wcsnicmp(m_tokenValue.pszString, L"option",6)) { LPWSTR temp,end; temp=&m_tokenValue.pszString[6]; DWORD optionNum = wcstol(temp, &end, 10); if (end==temp || errno==ERANGE) throw ErrUnknownVariable; expr->pPrim->dwNumber = EVarOptionBase+optionNum; } else { // convert string to variable token ID // Look for the option WORD wLoop; for(wLoop = 0; wLoop < NUMVAROPTIONS; wLoop++) { if(!wcsicmp(m_tokenValue.pszString, KVariableLookupTable[wLoop].pszOpt)) { // found match expr->pPrim->dwNumber = (WORD)KVariableLookupTable[wLoop].dwOpt; break; } } if(wLoop == NUMVAROPTIONS) throw ErrUnknownVariable; } } break; case NUMERIC_TOKEN: expr->exprType=EInstCondPrimTypeNumber; memcpy(expr->pPrim, &m_tokenValue, sizeof(VARIANTVAL)); break; } } break; default: throw ErrBadCondFormat; } GetNextToken(); return expr; }DWORD CParsePkg::ParseOption(const SParseOpt* options, DWORD dwNumOptions, DWORD* pdwOptions)// Parse the options part of an input line { DWORD option=0; ExpectToken(ALPHA_TOKEN); // Look for the option WORD wLoop; for(wLoop = 0; wLoop < dwNumOptions; wLoop++) { if(wcsicmp(m_tokenValue.pszString, options[wLoop].pszOpt) == 0) { option=options[wLoop].dwOpt; break; } } if(wLoop == dwNumOptions) throw ErrBadOption; *pdwOptions |= option; GetNextToken(); return option; }void CParsePkg::ExpectToken(TOKEN aToken) { if (m_token!=aToken) { if (m_pObserver) { _TCHAR msg[255]=_T("Expected "); if (aToken<=LAST_TOKEN) _tcscat(msg,GetTokenText(aToken)); else { _TCHAR tmp[2]={(_TCHAR)aToken,0}; _tcscat(msg,tmp); } _tcscat(msg,_T(" read ")); if (m_token<=LAST_TOKEN) _tcscat(msg,GetTokenText(m_token)); else { _TCHAR tmp[2]={(_TCHAR)m_token,0}; _tcscat(msg,tmp); } m_pObserver->DoErrMsg(msg); } throw ErrUnexpectedToken; } }const _TCHAR* CParsePkg::GetTokenText(TOKEN aToken) { switch(aToken) { case NUMERIC_TOKEN: return _T("numeric value"); case ALPHA_TOKEN: return _T("alphanumeric value"); case QUOTED_STRING_TOKEN: return _T("quoted string"); case AND_TOKEN: return _T("AND"); case OR_TOKEN: return _T("OR"); case NOT_TOKEN: return _T("NOT"); case EXISTS_TOKEN: return _T("EXISTS"); case GE_TOKEN: return _T(">="); case LE_TOKEN: return _T("<="); case NE_TOKEN: return _T("<>"); case IF_TOKEN: return _T("IF"); case ELSEIF_TOKEN: return _T("ELSEIF"); case ELSE_TOKEN: return _T("ELSE"); case ENDIF_TOKEN: return _T("ENDIF"); default: return _T("?"); } }void CParsePkg::GetNextToken()// lexical analyzer { // skip any white space & CR's while (m_pkgChar == '\n' || iswspace(m_pkgChar)) { if (m_pkgChar == '\n' && m_pObserver) m_pObserver->SetLineNumber(++m_nLineNo); GetNextChar(); } if (m_pkgChar == '\0') m_token=EOF_TOKEN; else if (IsNumericToken()) { GetNumericToken(); m_token=NUMERIC_TOKEN; } else if (iswalpha(m_pkgChar)) { // have some alphanumeric text GetAlphaNumericToken(); m_token=ALPHA_TOKEN; // check if it is a keyword for(WORD wLoop = 0; wLoop < NUMPARSETOKENS; wLoop++) { if(wcsicmp(m_tokenValue.pszString, KTokens[wLoop].pszOpt) == 0) { m_token=KTokens[wLoop].dwOpt; break; } } } else if (m_pkgChar == '\"') { // have a quoted string GetStringToken(); m_token=QUOTED_STRING_TOKEN; } else if (m_pkgChar == '>') { GetNextChar(); if (m_pkgChar == '=') { m_token=GE_TOKEN; GetNextChar(); } else m_token='>'; } else if (m_pkgChar == '<') { // check if start of an escaped string, e.g. <123>"abc" if (GetStringToken()) m_token=QUOTED_STRING_TOKEN; else { GetNextChar(); if (m_pkgChar == '=') { m_token=LE_TOKEN; GetNextChar(); } else if (m_pkgChar == '>') { m_token=NE_TOKEN; GetNextChar(); } else m_token='<'; } } else { m_token=m_pkgChar; GetNextChar(); } }BOOL CParsePkg::GetStringToken()// Purpose : Parse a quoted string from the input line// Inputs : m_pkgPtr - The string to parse// pszString - The output string// wMaxLength - The max length of pszString { DWORD wCount = 0; BOOL done=FALSE; BOOL finished=FALSE; DWORD escapeChars = 0; while (!finished) { if (m_pkgChar == '\"') { GetNextChar(); while(m_pkgChar && m_pkgChar != '\"') { if(wCount < (MAX_STRING - 1)) m_tokenValue.pszString[wCount++] = m_pkgChar; GetNextChar(); } if(m_pkgChar == '\0') throw ErrBadString; GetNextChar(); done=TRUE; } if (m_pkgChar == '<') { m_tokenValue.pszString[wCount] = L'\0'; escapeChars=ParseEscapeChars(); if (escapeChars>0) { done=TRUE; wCount+=escapeChars; if (wCount>=MAX_STRING) wCount=MAX_STRING-1; } } if (escapeChars==0 || m_pkgChar != '\"') finished=TRUE; } m_tokenValue.pszString[wCount] = L'\0'; return done; }WORD CParsePkg::ParseEscapeChars() { WORD found=0; WCHAR temp[MAX_STRING]; while (m_pkgChar == '<') { wcscpy(temp,m_tokenValue.pszString); DWORD fileOffset=::SetFilePointer(m_file, 0L, NULL, FILE_CURRENT); try { GetNextChar(); GetNumericToken(); if (m_pkgChar=='>') found++; else { ::SetFilePointer(m_file, fileOffset, NULL, FILE_BEGIN); break; } } catch (TParseException) { wcscpy(m_tokenValue.pszString,temp); ::SetFilePointer(m_file, fileOffset, NULL, FILE_BEGIN); break; } DWORD num=m_tokenValue.dwNumber; // watch for CP1252 escapes which aren't appropriate for UNICODE if (num>=0x80 && num<=0x9F) throw ErrInvalidEscape; DWORD len=wcslen(temp); wcscpy(m_tokenValue.pszString,temp); if (len+2<=MAX_STRING) { m_tokenValue.pszString[len]=(WCHAR)num; len++; m_tokenValue.pszString[len]='\0'; } GetNextChar(); } return found; }void CParsePkg::GetAlphaNumericToken()// Purpose : Parse an alphanumeric string from the input line// Inputs : m_pkgPtr - The string to parse// pszString - The output string// wMaxLength - The max length of pszString { WORD wCount = 0; while(m_pkgChar && (m_pkgChar == '_' || iswalnum(m_pkgChar))) { if(wCount < (MAX_STRING - 1)) m_tokenValue.pszString[wCount++] = m_pkgChar; GetNextChar(); } m_tokenValue.pszString[wCount] = L'\0'; }BOOL CParsePkg::IsNumericToken()// Purpose : Determines if the next lexeme is a numeric token { BOOL lexemeIsNumber = FALSE; if (iswdigit(m_pkgChar)) lexemeIsNumber = TRUE; else if (m_pkgChar == '+' || m_pkgChar == '-') { // we may have a number but we must look ahead one char to be certain WCHAR oldChar = m_pkgChar; DWORD fileOffset=::SetFilePointer(m_file, 0L, NULL, FILE_CURRENT); GetNextChar(); lexemeIsNumber = iswdigit(m_pkgChar); m_pkgChar = oldChar; ::SetFilePointer(m_file,fileOffset,NULL,FILE_BEGIN); } return lexemeIsNumber; }void CParsePkg::GetNumericToken()// Purpose : Parse a number from the input line// Inputs : m_pkgPtr - The string to parse// pdwNumber - The output number { WCHAR temp[MAX_STRING]; LPWSTR end; BOOL hexString = FALSE; DWORD dwBytesRead; DWORD fileOffset=::SetFilePointer(m_file, 0L, NULL, FILE_CURRENT); temp[0]=m_pkgChar; /* Read one digit for each iteration. */ (void)fileOffset; int i = 1; unsigned short tmp_char; int parse = TRUE; while(parse) { BOOL ok = FALSE; ok = ::ReadFile(m_file, &tmp_char, sizeof(tmp_char), &dwBytesRead, NULL); if(!ok || dwBytesRead != sizeof(tmp_char)) { throw ErrReadFailed; } if(iswxdigit(tmp_char) || tmp_char == 'x') { temp[i++] = (WCHAR)tmp_char; } else { parse = FALSE; } }; temp[i] = '\0'; /* Rewind */ (void)::SetFilePointer(m_file, -sizeof(tmp_char), NULL, FILE_CURRENT); hexString = (!wcsnicmp(temp, L"0x", 2) || !wcsnicmp(&temp[1], L"0x", 2)); m_tokenValue.dwNumber = wcstol(temp, &end, (hexString) ? 16 : 10); if (end==temp) throw ErrReadFailed; if (errno==ERANGE) throw ErrNumberOutOfRange; GetNextChar(); }void CParsePkg::GetNextChar() { DWORD dwBytesRead; unsigned short tmp; /* * NOTE! we are reading 4 bytes for each char, but only using 2 bytes */ if (!::ReadFile(m_file, (LPVOID)&tmp, 2, &dwBytesRead, NULL) || dwBytesRead!=2) tmp='\0'; m_pkgChar = tmp; }BOOL CParsePkg::DoesExist(LPWSTR pszFile, DWORD *pdwSize)// Purpose : Attempt to determine whether the file exists (w. or W.out the search path), and gets // it's file size.// Inputs : pszFile - The file to find (as a UNICODE string)// pdwSize - store its size here (set to zero if not found)// Returns : Yes or No { BOOL fFound = FALSE; *pdwSize = 0; /* Convert to Unix filename */ LPWSTR tmp = pszFile; while(*tmp) { if(*tmp == '\\') *tmp = '/'; tmp++; } try { HANDLE hFile = ::MakeSISOpenFile(pszFile, GENERIC_READ, OPEN_EXISTING); if (hFile != INVALID_HANDLE_VALUE) { *pdwSize = ::GetFileSize(hFile, NULL); ::CloseHandle(hFile); fFound = TRUE; } else { // If we are using a search directory if(m_pszSearchDir[0] != '\0') { WCHAR pszNewPath[MAX_PATH]; wcscpy(pszNewPath, m_pszSearchDir); wcscat(pszNewPath, pszFile); HANDLE hFile = ::MakeSISOpenFile(pszNewPath, GENERIC_READ, OPEN_EXISTING); if (hFile != INVALID_HANDLE_VALUE) { *pdwSize = ::GetFileSize(hFile, NULL); ::CloseHandle(hFile); wcscpy(pszFile, pszNewPath); fFound = TRUE; } } } } catch (TUtilsException excp) { // ignore error if writing a stub file if (!m_stubFile) throw excp; m_pObserver->DoVerbage(_T("warning :file does not exist")); fFound=TRUE; } return fFound; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -