bool IsIPAddress(std::string ipaddr) { int countTokens = -1 ; vector< int > locations ; countTokens = CountToken(ipaddr, ".", locations ); if ( countTokens != 3 ) return false; string substr; for ( int i = 0 ; i <= countTokens ; i++ ){ if ( i == 0 ) substr = ipaddr.substr(0,locations[i]); else substr = ipaddr.substr(locations[i - 1] + 1,locations[i] - locations[i - 1] - 1); int quad = atoi(substr.c_str()); if (( quad < 0) || (quad > 255) ) return false; } return true; }
/* SetBuffer function Setup suim code. */ SUIMLIB_API int SetBuffer(const int *S, const char *pchSuimName, const char *pchBuffer, const int inVarCount, const int inOutCount, int inCallBackCount) { if(S== NULL) return (ERR_S_NULL); int inFlag = IsNullOrEmpty(pchSuimName); if(inFlag!= 0) return (ERR_NAME); inFlag = IsNullOrEmpty(pchBuffer); if(inFlag!= 0) return (ERR_BUFFLER); if(inVarCount>= MAX_VARIABLE_COUNT) return (ERR_OVERFLOW); struct ST_SUIM *pS = (struct ST_SUIM *)S; if(pS->pchSuimName!= NULL) return (ERR_SET_NAME); if(pS->pchBuffer!= NULL) return (ERR_SET_BUFFER); size_t szLength = strlen(pchSuimName); pS->pchSuimName = malloc(szLength + 1); if(pS->pchSuimName== NULL) return (ERR_MEMORY); strcpy(pS->pchSuimName, pchSuimName); szLength = strlen(pchBuffer); pS->pchBuffer = malloc(szLength + 1); if(pS->pchBuffer== NULL) return (ERR_MEMORY); strcpy(pS->pchBuffer, pchBuffer); int inMaxLine = 0; int inCnt, inMax = strlen(pchBuffer); for(inCnt= 0;inCnt< inMax;inCnt++) { if(pchBuffer[inCnt]== '\n') inMaxLine++; } pS->inMaxLineNum = inMaxLine; pS->ppchLines = malloc(sizeof(char *) * (inMaxLine)); if(pS->ppchLines== NULL) return (ERR_MEMORY); memset(pS->ppchLines, 0x00, sizeof(char *) * (inMaxLine)); //split lines inCnt = 0; char *pchPos = strtok( pS->pchBuffer, "\n" ); while( pchPos != NULL && inCnt < inMaxLine ) { pS->ppchLines[inCnt] = pchPos; inCnt++; pchPos = strtok( NULL, "\n" ); } int inCountFunction = 0; for(inCnt= 0;inCnt< inMaxLine;inCnt++) { if(pS->ppchLines[inCnt][0]== 'f' && pS->ppchLines[inCnt][1]== '|') inCountFunction++; } //define setting pS->inCountVariable = inVarCount; if(inVarCount> 0) { pS->ppstListVariable = malloc(sizeof(struct ST_VALUE *) * inVarCount); memset(pS->ppstListVariable, 0x00, sizeof(struct ST_VALUE *) * inVarCount); for(inCnt= 0;inCnt< inVarCount;inCnt++) { SetValueDouble(pS->ppstListVariable, inCnt, 0.0); } for(inCnt= 0;inCnt< inMaxLine;inCnt++) { if(!(pS->ppchLines[inCnt][0]== 's' && pS->ppchLines[inCnt][1]== '|')) continue; char *pchKey = strtok(&pS->ppchLines[inCnt][2], ":"); char *pchValue = strtok(NULL, ":"); const int inNo = CngKey2No(pchKey); if(inNo< 0) return (ERR_OVERFLOW); if(inNo>= inMax) return (ERR_OVERFLOW); int inErrCode = SetValueString(pS->ppstListVariable, inNo, pchValue); if(inErrCode!= SUCCESS) return (inErrCode); } } //callback setting pS->inCountCallBack = inCallBackCount; if(inCallBackCount> 0) { pS->ppstListCallBack = malloc(sizeof(struct ST_VALUE_CALLBACK *) * inCallBackCount); memset(pS->ppstListCallBack, 0x00, sizeof(struct ST_VALUE_CALLBACK *) * inCallBackCount); } //out setting pS->pchInArgs = NULL; pS->inNoOut = 0; pS->inCountOut = inOutCount; if(inOutCount> 0) { pS->pchOutArgsType = malloc(inOutCount + 1); memset(pS->pchOutArgsType, 0x00, inOutCount + 1); pS->ppstListOut = malloc(sizeof(struct ST_VALUE *) * inOutCount); memset(pS->ppstListOut, 0x00, sizeof(struct ST_VALUE *) * inOutCount); for(inCnt= 0;inCnt< inOutCount;inCnt++) { SetValueDouble(pS->ppstListOut, inCnt, 0.0); } } //function setting pS->inCountFunction = inCountFunction; if(inCountFunction> 0) { pS->ppstListFunction = malloc(sizeof(struct ST_VALUE *) * inCountFunction); memset(pS->ppstListFunction, 0x00, sizeof(struct ST_VALUE *) * inCountFunction); for(inCnt= 0;inCnt< inMaxLine;inCnt++) { if(!(pS->ppchLines[inCnt][0]== 'f' && pS->ppchLines[inCnt][1]== '|')) continue; char *pchKey = strtok(&pS->ppchLines[inCnt][2], ":"); char *pchValue = strtok(NULL, ":"); int inLineNo = atoi(pchValue); int inRes = AddFunctionDefine(pS, pchKey, inLineNo); if(inRes!= SUCCESS) return (inRes); } } //set start position //set jump line no //create cmp string pS->pchListStartPos = malloc(sizeof(char) * inMaxLine); pS->pinListJumpNo = malloc(sizeof(int) * inMaxLine); pS->ppchListCmp = malloc(sizeof(char *) * inMaxLine); memset(pS->ppchListCmp, 0x00, sizeof(char *) * inMaxLine); for(inCnt= 0;inCnt< inMaxLine;inCnt++) { const char *pchLine = pS->ppchLines[inCnt]; pS->pchListStartPos[inCnt] = 0; pS->pinListJumpNo[inCnt] = 0; pS->ppchListCmp[inCnt] = NULL; if(pchLine[0]== '-' && pchLine[1]== '-') continue; if(pchLine[1]== '|') continue; char *pchIndex = strchr(pchLine, ':'); if(pchIndex== NULL) continue; pchIndex+= 1; pS->pchListStartPos[inCnt] = (char)(pchIndex - pchLine); if(pchIndex[0]== 'i' && pchIndex[1]== ':') { char *pchIndex2 = strstr(pchIndex, ")e"); if(pchIndex2!= NULL) { pS->pinListJumpNo[inCnt] = atoi(pchIndex2 + 2); int inSize = (int)(pchIndex2 - &pchIndex[3]); pS->ppchListCmp[inCnt] = malloc(inSize + 1); memset(pS->ppchListCmp[inCnt], 0x00, inSize + 1); strncpy(pS->ppchListCmp[inCnt], &pchIndex[3], inSize); } continue; } if(pchIndex[0]== 'g' && pchIndex[1]== ':') { pchIndex = strchr(pchIndex, ':'); if(pchIndex!= NULL) { pS->pinListJumpNo[inCnt] = atoi(pchIndex + 1); } continue; } } //memory allocate int inSize = sizeof(int) * inMaxLine; pS->pinListTokenNum = malloc(inSize); memset(pS->pinListTokenNum, 0x00, inSize); inSize = sizeof(int) * inMaxLine; pS->pinListTokenSt = malloc(inSize); memset(pS->pinListTokenSt, 0x00, inSize); //count token int inStToken = 0; int inMaxToken = 0; for(inCnt= 0;inCnt< inMaxLine;inCnt++) { const char *pchLine = pS->ppchLines[inCnt]; if(pchLine[0]== '-' && pchLine[1]== '-') continue; if(pchLine[1]== '|') continue; if(pS->pchListStartPos[inCnt]== 0) continue; char *pchIndex = (char *)(pchLine + pS->pchListStartPos[inCnt]); if(pchIndex[0]== 'g' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'e' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'f' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'c' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'r' && pchIndex[1]== ':') continue; int inCntToken = 0; if(pchIndex[0]== 'o' && pchIndex[1]== ':') { inCntToken = CountToken(&pchIndex[2]); if(inMaxToken< inCntToken) inMaxToken = inCntToken; } /* if(pchIndex[0]== 's' && pchIndex[1]== ':') { inCntToken = CountToken(&pchIndex[5]); if(inMaxToken< inCntToken) inMaxToken = inCntToken; } if(pchIndex[0]== 'd' && pchIndex[1]== ':') { inCntToken = CountToken(&pchIndex[5]); if(inMaxToken< inCntToken) inMaxToken = inCntToken; } */ if(pchIndex[0]== 'i' && pchIndex[1]== ':') { inCntToken = CountToken(pS->ppchListCmp[inCnt]); if(inMaxToken< inCntToken) inMaxToken = inCntToken; } if(pchIndex[2]== '=') { inCntToken = CountToken(&pchIndex[3]); if(inMaxToken< inCntToken) inMaxToken = inCntToken; } pS->pinListTokenSt[inCnt] = inStToken; inStToken+= inCntToken; pS->pinListTokenNum[inCnt] = inCntToken; } //memory allocate pS->inMaxTokenNum = inMaxToken; inSize = sizeof(double) * inMaxToken * inMaxLine; pS->pdoListToken = malloc(inSize); memset(pS->pdoListToken, 0x00, inSize); inSize = sizeof(char) * inMaxToken * inMaxLine; pS->pchListTokenType = malloc(inSize); memset(pS->pchListTokenType, 0x00, inSize); inSize = sizeof(char) * MAX_WORD * inMaxToken * inMaxLine; pS->pchListTokenWord = malloc(inSize); memset(pS->pchListTokenWord, 0x00, inSize); //setup token data for(inCnt= 0;inCnt< inMaxLine;inCnt++) { const char *pchLine = pS->ppchLines[inCnt]; if(pchLine[0]== '-' && pchLine[1]== '-') continue; if(pchLine[1]== '|') continue; if(pS->pchListStartPos[inCnt]== 0) continue; char *pchIndex = (char *)(pchLine + pS->pchListStartPos[inCnt]); if(pchIndex[0]== 'g' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'e' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'f' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'c' && pchIndex[1]== ':') continue; if(pchIndex[0]== 'r' && pchIndex[1]== ':') continue; int inStToken = pS->pinListTokenSt[inCnt]; if(pchIndex[0]== 'o' && pchIndex[1]== ':') { SetTokenData(pS, &pchIndex[2], inStToken); } /* if(pchIndex[0]== 's' && pchIndex[1]== ':') { SetTokenData(pS, &pchIndex[5], inStToken); } if(pchIndex[0]== 'd' && pchIndex[1]== ':') { SetTokenData(pS, &pchIndex[5], inStToken); } */ if(pchIndex[0]== 'i' && pchIndex[1]== ':') { SetTokenData(pS, pS->ppchListCmp[inCnt], inStToken); } if(pchIndex[2]== '=') { SetTokenData(pS, &pchIndex[3], inStToken); } } //global line execute int inFunc = 0; for(inCnt= 0;inCnt< inMaxLine;inCnt++) { const char *pchLine = pS->ppchLines[inCnt]; if(pchLine[0]== '-' && pchLine[1]== '-') { int inLength = strlen(pchLine); if(inLength<= 2) continue; int inVersion = atoi(&pchLine[2]); if(inVersion== VERSION) continue; return (ERR_VERSION); } if(pchLine[1]== '|') continue; pS->inLineNo = inCnt; char *pchIndex = (char *)(pchLine + pS->pchListStartPos[inCnt]); if(pchIndex[0]== 'f') inFunc = 1; if(pchIndex[0]== 'e') inFunc = 0; if(inFunc== 1) continue; if(pchIndex[0]== 'e' && pchIndex[1]== ':') continue; int inLineNo = ExecLine(pS, pchIndex); if(inLineNo< 0) return (inLineNo); } return (SUCCESS); }