Code:
char *substr(const char *pstr, int start, int numchars)
{
char *pnew = malloc(numchars+1);
strncpy(pnew, pstr + start, numchars);
pnew[numchars] = '\0';
return pnew;
}
int strrpos (unsigned char *pStrText, char pChar){ // Última ocorrência do caractere.
int iLen, iCont;
iLen = strlen (pStrText);
if (iLen > 0){
for (iCont=iLen - 1; iCont>=0; iCont--){
if (pStrText[iCont] == pChar)
return iCont;
}
return 0; // Não houve ocorrências
}
else { return 0; } // String Nula
}
int ProcessaChamado (char *pPacote, int pLen){
char *cTeste = malloc (30);
char *cFuncao;
char *cPacote = malloc(pLen);
int iPosStart, iPosEnd, iPosTam;
//Separa o Pacote
iPosStart = strrpos (pPacote, cSTX);
if(iPosStart >= 0)
iPosStart++;
iPosEnd = strrpos (pPacote, cCR);
iPosTam = iPosEnd - iPosStart;
if((iPosStart) && (iPosEnd)){
cPacote = substr(pPacote, iPosStart, iPosTam);
}
else { return 0; }
if(strlen (cPacote) > 0){
cFuncao = substr (cPacote, 0, 2);
sprintf (cTeste, "%s %s %c %c %d", cFuncao, cPacote, cFuncao[0], cFuncao[1], strlen(cFuncao));
printCom1 ("TESTE: %s\n", cTeste);
if (strcmp (cFuncao, "OP") == 0)
printCom1 ("OK!");
}
else { return 0; }
return 1;
}
I need got the XX (pos 1 e 2 of string) in send string
Send: XX34567890AB
Received: X3 X34567890AB X 3 2