LR中有什么函数来截取字符串?
LR中有什么函数来截取字符串?"\x012\x013\x012\x012\x048227\x019\x03ONE\x014\x16\x00\x00\f\x012\x013\x012\x012\x048227\x0210\x03TWO\x014\x18\x00\x00"
我想截出x019 strncpy() 土匪快说说 找出x019的左右分隔标识符,然后使用C语言的字符处理函数。 memcpy和sprintf 两个函数各有各的好处
我经常用sprintf strtok()或者用lr_save_var() /* FUNCTIONS */
void save_search_string(char *buf, char *leftBound, char *rightBound, char *PreFix)
{
int i,j,k,intTmp, offsetA, offsetB;
char *strTmpA;
char *strTmpB;
char *strTmpC;
char *strTmpD;
char *strPos;
intTmp = strlen(buf) + 1;
if ((strTmpA = (char *)malloc(intTmp * sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpB = (char *)malloc(intTmp * sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpC = (char *)malloc(intTmp * sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpD = (char *)malloc(intTmp * sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
i = 0;
strPos = (char *)strstr(buf, leftBound);
if(strPos != NULL){
offsetA = (int)(strPos - buf);
}
else offsetA = -1;
strPos = (char *)strstr(buf, rightBound);
if(strPos != NULL) offsetB = (int)(strPos - buf);
else offsetB = -1;
if((offsetA < 0)||(offsetB < 0)){
sprintf(strTmpC, "%s_count", PreFix);
lr_save_string ("0", strTmpC);
free(strTmpA);
free(strTmpB);
free(strTmpC);
free(strTmpD);
return;
}
if(offsetA < offsetB){
memcpy(strTmpA, buf + offsetA + strlen(leftBound), offsetB - offsetA - strlen(leftBound));
sprintf(strTmpA + offsetB - offsetA - strlen(leftBound), "\x0");
i++;
sprintf(strTmpC, "%s_%d", PreFix, i);
lr_save_string (strTmpA, strTmpC);
if(strlen(buf) > (offsetB + strlen(rightBound))){
memcpy(strTmpB, buf + offsetB + strlen(rightBound), strlen(buf) - offsetB - strlen(rightBound));
sprintf(strTmpB + strlen(buf) - offsetB - strlen(rightBound), "\x0");
}
else sprintf(strTmpB, "");
}
if(offsetA > offsetB){
memcpy(strTmpB, buf + offsetB + strlen(rightBound), strlen(buf) - offsetB - strlen(rightBound));
sprintf(strTmpB + strlen(buf) - offsetB - strlen(rightBound), "\x0");
}
while(strcmp(strTmpB, "") != 0){
strPos = (char *)strstr(strTmpB, leftBound);
if(strPos != NULL) offsetA = (int)(strPos - strTmpB);
else offsetA = -1;
strPos = (char *)strstr(strTmpB, rightBound);
if(strPos != NULL) offsetB = (int)(strPos - strTmpB);
else offsetB = -1;
if((offsetA < 0)||(offsetB < 0)){
sprintf(strTmpC, "%s_count", PreFix);
sprintf(strTmpD, "%d", i);
lr_save_string (strTmpD, strTmpC);
free(strTmpA);
free(strTmpB);
free(strTmpC);
free(strTmpD);
return;
}
if(offsetA < offsetB){
memcpy(strTmpA, strTmpB + offsetA + strlen(leftBound), offsetB - offsetA - strlen(leftBound));
sprintf(strTmpA + offsetB - offsetA - strlen(leftBound), "\x0");
i++;
sprintf(strTmpC, "%s_%d", PreFix, i);
lr_save_string (strTmpA, strTmpC);
sprintf(strTmpC, "%s_count", PreFix);
sprintf(strTmpD, "%d", i);
lr_save_string (strTmpD, strTmpC);
if(strlen(buf) > (offsetB + strlen(rightBound))){
memcpy(strTmpC, strTmpB + offsetB + strlen(rightBound), strlen(strTmpB) - offsetB - strlen(rightBound));
sprintf(strTmpC + strlen(strTmpB) - offsetB - strlen(rightBound), "\x0");
}
else sprintf(strTmpC, "");
}
if(offsetA > offsetB){
memcpy(strTmpC, strTmpB + offsetB + strlen(rightBound), strlen(strTmpB) - offsetB - strlen(rightBound));
sprintf(strTmpC + strlen(strTmpB) - offsetB - strlen(rightBound), "\x0");
}
sprintf(strTmpB, "%s", strTmpC);
}
free(strTmpA);
free(strTmpB);
free(strTmpC);
free(strTmpD);
return;
}
//==================================
使用方法:
sendBuf 和 recvBuf 等变量定义略...
//save_search_string(返回变量,左边界,右边界,参数名);
save_search_string(recvBuf,"<ORDER_ID type=\"string\">","</ORDER_ID>","Search_Id");
if(strcmp("ok!", lr_eval_string ("{RespondCode_1}")) == 0)
{
lr_message("Pass!");
}
else
{
lr_message("Fail!");
} lr_save_sting("\x012\x013\x012\x012\x048227\x019\x03ONE\x014\x16\x00\x00\f\x012\x013\x012\x012\x048227\x0210\x03TWO\x014\x18\x00\x00","temp");
lr_save_var(lr_eval_string("{temp}")+30,4,“result”); 8#的脚本十分强大 收藏,备用!
页:
[1]