void Templateiser::processIF ( std::string &code, std::string::const_iterator &inItr, const std::string &str ) { std::string key; // read the rest of the key std::string::const_iterator itr = readKey(key,inItr,str); std::vector<std::string> commandParts = tokenize(key,std::string(" "),0,0); if (commandParts.size() < 2) { inItr = itr; return; } // check the params makelower(commandParts[0]); makelower(commandParts[1]); if ( commandParts[0] != "if" ) { inItr = itr; return; } std::string param; if (commandParts.size() > 2) param = commandParts[2]; // now get the code for the next section std::string trueSection,elseSection; std::vector<std::string> checkKeys; checkKeys.push_back(format("?else %s",commandParts[1].c_str())); checkKeys.push_back(format("?end %s",commandParts[1].c_str())); std::string keyFound; itr = findNextTag(checkKeys,keyFound,trueSection,itr,str); if (keyFound == checkKeys[0]) { // we hit an else, so we need to check for it // it was the else, so go and find the end too if (itr == str.end()) { inItr = itr; return; } checkKeys.erase(checkKeys.begin());// kill the else, find the end itr = findNextTag(checkKeys,keyFound,elseSection,itr,str); } // test the if, stuff that dosn't exist is false if (callIF(commandParts[1],param)) { std::string newCode; processTemplate(newCode,trueSection); code += newCode; } else { std::string newCode; processTemplate(newCode,elseSection); code += newCode; } inItr = itr; }
bool Templateiser::processTemplateFile ( std::string &code, const char *file ) { if (!file) return false; // find the file for (size_t i = 0; i < filePaths.size(); i++ ) { std::string path = filePaths[i] + file; FILE *fp = fopen(getPathForOS(path).c_str(),"rt"); std::string val; if (fp) { char c; while(fscanf(fp,"%c",&c) == 1) val += c; fclose(fp); processTemplate(code,val); return true; } } return false; }
RCType TDFParser::processTracePointDetail(const char *line, J9TDFTracepoint *tp, const char *module, unsigned int id, const char *fileName, unsigned int lineNumber) { RCType rc = RC_FAILED; char *tok = NULL; tp->level = -1; tp->hasEnv = true; /* Save original string because strtok mangles the source string by placing \0 at delimiter */ char *tokLine = strdup(line); /* read the first token */ tok = strtok(tokLine, " \t"); line = line + strlen(tok) + 1; while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tp->name = strdup(strchr(tok, '=') + 1); Port::omrmem_free((void **)&tokLine); /* now parse all of the optional fields */ tokLine = strdup(line); tok = strtok(tokLine, " \t"); while (NULL != tok) { if (StringUtils::startsWithUpperLower(tok, "Explicit")) { /* tp->isExplicit = true; */ FileUtils::printError("WARNING : obsolete keyword 'Explicit' in %s:%u\n", fileName, lineNumber); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Suffix")) { /* tp->isSuffix = true; */ FileUtils::printError("WARNING : obsolete keyword 'Suffix' in %s:%u\n", fileName, lineNumber); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Prefix")) { /* tp->isPrefix = true; */ FileUtils::printError("WARNING : obsolete keyword 'Prefix' in %s:%u\n", fileName, lineNumber); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Exception")) { /* tp->isException = true; */ FileUtils::printError("WARNING : obsolete keyword 'Exception' in %s:%u\n", fileName, lineNumber); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Private")) { /* tp->isPrivate = true; */ FileUtils::printError("WARNING : obsolete keyword 'Private' in %s:%u\n", fileName, lineNumber); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Test")) { tp->test = true; line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "NoEnv")) { tp->hasEnv = false; line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Obsolete")) { tp->obsolete = true; line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } tok = strtok(NULL, " \t"); } else if (StringUtils::startsWithUpperLower(tok, (UT_ASSERT_TYPE == tp->type ? "Assert=" : "Template="))) { tp->format = getTemplate(line, (UT_ASSERT_TYPE == tp->type ? "Assert=" : "Template=")); if (NULL == tp->format){ goto failed; } line = line + strlen((UT_ASSERT_TYPE == tp->type ? "Assert=" : "Template=")) + (strlen(tp->format) + 2 /* two quotes */); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } Port::omrmem_free((void **)&tokLine); tokLine = strdup(line); tok = strtok(tokLine, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Level=")) { if (RC_OK != StringUtils::getPositiveIntValue(tok, "Level=", &tp->level)) { goto failed; } line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } Port::omrmem_free((void **)&tokLine); tokLine = strdup(line); tok = strtok(tokLine, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Overhead=")) { if (RC_OK != StringUtils::getPositiveIntValue(tok, "Overhead=", &tp->overhead)) { goto failed; } line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } Port::omrmem_free((void **)&tokLine); tokLine = strdup(line); tok = strtok(tokLine, " \t"); } else if (StringUtils::startsWithUpperLower(tok, "Group=")) { tp->groups = getGroups(tok); line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } Port::omrmem_free((void **)&tokLine); tokLine = strdup(line); tok = strtok(tokLine, " \t"); } else { line = line + strlen(tok); while ((' ' == *line || '\t' == *line) && '\0' != *line) { line = line + 1; } Port::omrmem_free((void **)&tokLine); tokLine = strdup(line); tok = strtok(tokLine, " \t"); } } /* Assign default level */ if ((unsigned int)-1 == tp->level) { tp->level = 1; } if (NULL == tp->format) { tp ->format = (char *)Port::omrmem_calloc(1, (strlen("null") + 1)); strncpy(tp ->format, "null", strlen("null")); } if (UT_ASSERT_TYPE == tp->type) { /* Assertion parameters are always \"\\377\\4\\377\" for "** ASSERTION FAILED ** at %s:%d: %s" */ tp->parameters = NULL; rc = processAssertTemplate(tp->format, &tp->parmCount); } else { const char *key = "%"; const char *pch = strpbrk(tp->format, key); unsigned int percentSignCount = 0; while (NULL != pch) { percentSignCount += 1; pch = strpbrk(pch + 1, key); } size_t len = 5 /* \\\"\\\"\0 when no parameters specified */ + (percentSignCount * (strlen(TRACE_DATA_TYPE_STRING) + strlen(TRACE_DATA_TYPE_PRECISION))) /* TRACE_DATA_TYPE_STRING is the longest parameter string + precision specifier*/; tp->parameters = (char *)Port::omrmem_calloc(1, len); if (NULL == tp->parameters) { FileUtils::printError("Failed to allocate memory\n"); goto failed; } rc = processTemplate(tp->format, tp->parameters, &tp->parmCount, fileName, lineNumber); /* We pass NULL for no parameters not the empty string "". */ if (0 == tp->parmCount) { Port::omrmem_free((void **)&tp->parameters); tp->parameters = (char *)Port::omrmem_calloc(1, strlen("NULL") + 1); strcpy(tp->parameters, "NULL"); } } return rc; failed: return rc; }
void Templateiser::processLoop ( std::string &code, std::string::const_iterator &inItr, const std::string &str ) { std::string key,loopSection,emptySection,param; // read the rest of the key std::string::const_iterator itr = readKey(key,inItr,str); std::vector<std::string> commandParts = tokenize(key,std::string(" "),0,0); if (commandParts.size() < 2) { inItr = itr; return; } // check the params makelower(commandParts[0]); makelower(commandParts[1]); if (commandParts.size() > 2) param = commandParts[2]; if ( commandParts[0] != "start" ) { inItr = itr; return; } std::vector<std::string> checkKeys; checkKeys.push_back(format("*end %s",commandParts[1].c_str())); std::string keyFound; itr = findNextTag(checkKeys,keyFound,loopSection,itr,str); if (itr == str.end()) { inItr = itr; return; } // do the empty section // loops have to have both checkKeys.clear(); checkKeys.push_back(format("*empty %s",commandParts[1].c_str())); itr = findNextTag(checkKeys,keyFound,emptySection,itr,str); if (callLoop(commandParts[1],param)) { std::string newCode; processTemplate(newCode,loopSection); code += newCode; while(callLoop(commandParts[1],param)) { newCode = ""; processTemplate(newCode,loopSection); code += newCode; } } else { std::string newCode; processTemplate(newCode,emptySection); code += newCode; } inItr = itr; }