BOOL ParseMacroData(PCHAR szOriginal) { // find each {} PCHAR pBrace = strstr(szOriginal, "${"); if (!pBrace) return false; unsigned long NewLength; BOOL Changed = false; //PCHAR pPos; //PCHAR pStart; //PCHAR pIndex; CHAR szCurrent[MAX_STRING] = { 0 }; do { // find this brace's end PCHAR pEnd = &pBrace[1]; BOOL Quote = false; BOOL BeginParam = false; int nBrace = 1; while (nBrace) { ++pEnd; if (BeginParam) { BeginParam = false; if (*pEnd == '\"') { Quote = true; } continue; } if (*pEnd == 0) {// unmatched brace or quote goto pmdbottom; } if (Quote) { if (*pEnd == '\"') { if (pEnd[1] == ']' || pEnd[1] == ',') { Quote = false; } } } else { if (*pEnd == '}') { nBrace--; } else if (*pEnd == '{') { nBrace++; } else if (*pEnd == '[' || *pEnd == ',') BeginParam = true; } } *pEnd = 0; strcpy(szCurrent, &pBrace[2]); if (szCurrent[0] == 0) { goto pmdbottom; } if (ParseMacroData(szCurrent)) { unsigned long NewLength = strlen(szCurrent); memmove(&pBrace[NewLength + 1], &pEnd[1], strlen(&pEnd[1]) + 1); strncpy(pBrace, szCurrent, NewLength); pEnd = &pBrace[NewLength]; *pEnd = 0; } MQ2TYPEVAR Result; if (!ParseMQ2DataPortion(szCurrent, Result) || !Result.Type || !Result.Type->ToString(Result.VarPtr, szCurrent)) strcpy(szCurrent, "NULL"); NewLength = strlen(szCurrent); memmove(&pBrace[NewLength], &pEnd[1], strlen(&pEnd[1]) + 1); strncpy(pBrace, szCurrent, NewLength); Changed = true; pmdbottom:; } while (pBrace = strstr(&pBrace[1], "${")); if (Changed) while (ParseMacroData(szOriginal)) { } return Changed; }
BOOL ParseMacroData(PCHAR szOriginal, SIZE_T BufferSize) { // find each {} PCHAR pBrace = strstr(szOriginal, "${"); if (!pBrace) return false; unsigned long NewLength; BOOL Changed = false; //PCHAR pPos; //PCHAR pStart; //PCHAR pIndex; CHAR szCurrent[MAX_STRING] = { 0 }; MQ2TYPEVAR Result = { 0 }; do { // find this brace's end PCHAR pEnd = &pBrace[1]; BOOL Quote = false; BOOL BeginParam = false; int nBrace = 1; while (nBrace) { ++pEnd; if (BeginParam) { BeginParam = false; if (*pEnd == '\"') { Quote = true; } continue; } if (*pEnd == 0) {// unmatched brace or quote goto pmdbottom; } if (Quote) { if (*pEnd == '\"') { if (pEnd[1] == ']' || pEnd[1] == ',') { Quote = false; } } } else { if (*pEnd == '}') { nBrace--; } else if (*pEnd == '{') { nBrace++; } else if (*pEnd == '[' || *pEnd == ',') BeginParam = true; } } *pEnd = 0; strcpy_s(szCurrent, &pBrace[2]); if (szCurrent[0] == 0) { goto pmdbottom; } if (ParseMacroData(szCurrent, sizeof(szCurrent))) { unsigned long NewLength = strlen(szCurrent); memmove(&pBrace[NewLength + 1], &pEnd[1], strlen(&pEnd[1]) + 1); int addrlen = (int)(pBrace - szOriginal); memcpy_s(pBrace, BufferSize - addrlen, szCurrent, NewLength); pEnd = &pBrace[NewLength]; *pEnd = 0; } ZeroMemory(&Result, sizeof(Result)); Result.Type = 0; Result.Int64 = 0; if (!ParseMQ2DataPortion(szCurrent, Result) || !Result.Type || !Result.Type->ToString(Result.VarPtr, szCurrent)) { strcpy_s(szCurrent, "NULL"); } NewLength = strlen(szCurrent); memmove(&pBrace[NewLength], &pEnd[1], strlen(&pEnd[1]) + 1); int addrlen = (int)(pBrace - szOriginal); memcpy_s(pBrace, BufferSize - addrlen, szCurrent, NewLength); if (bAllowCommandParse == false) { bAllowCommandParse = true; Changed = false; break; } else { Changed = true; } pmdbottom:; } while (pBrace = strstr(&pBrace[1], "${")); if (Changed) while (ParseMacroData(szOriginal, BufferSize)) { } return Changed; }