遇到一个需求,需要在没有数据库的情况下,实现后端返回码多对一映射。请大家指出不足
代码如下:
#include <stdio.h>
#include <string.h>
/*
* 函数名称 BackMapp
* 入 参 pBuff:待解析字符串 caInBuff:需要映射的返回码 caFirstSeparator:第一分割符 caSecondlySeparator:第二分割符
* 出 参 pRetBuff:返回结果
* 返 回 值 0:成功 -1:失败
* 功 能 根据配置文件解析出对应的返回码
*/
int BackMapp( char* const pcBuff,
char* const pcInBuff,
char* pcRetBuff ,
char* const pcFirstSeparator ,
char* const pcSecondlySeparator)
{
char caBigBuff[1024] = {0x00};
char caOldBuff[1024] = {0x00};
char caNewBuff[1024] = {0x00};
char caInBuff[1024] = {0x00};
char caOutbuff[1024] = {0x00};
char caFirstSeparator[1024] = {0x00};
char caSecondlySeparator[1024] = {0x00};
char *pcTempBuff = NULL;
char *pStrtok = NULL;
char *pStrstr = NULL;
if( !strlen(pcBuff)||
!strlen(pcInBuff)||
!strlen(pcFirstSeparator)||
!strlen(pcSecondlySeparator)
){
return -1;
}
strncpy(caBigBuff , pcBuff , strlen(pcBuff));
strncpy(caInBuff , pcInBuff, strlen(pcInBuff));
strncpy(caFirstSeparator , pcFirstSeparator ,strlen(pcFirstSeparator));
strncpy(caSecondlySeparator , pcSecondlySeparator , strlen(pcSecondlySeparator));
pStrtok = strtok_s( caBigBuff , caFirstSeparator , &pcTempBuff );
while( pStrtok != NULL )
{
pStrstr = strstr(pStrtok , caSecondlySeparator );
if( pStrstr != NULL )
{
*pStrstr = ‘\0’;
printf(“%s\n”,pStrtok);
if( strstr(pStrtok , caInBuff) != NULL )
{
strncpy(caOutbuff, pStrstr + strlen(caSecondlySeparator),strlen(pStrstr + strlen(caSecondlySeparator)));
strncpy(pcRetBuff, caOutbuff , strlen(caOutbuff));
return 0;
}
}
pStrtok = strtok_s( NULL , caFirstSeparator , &pcTempBuff);
}
return -1;
}
int main()
{
char caOutBuff[1024] = {0x00};
int iRet = BackMapp(“1,2,3:1@|4,5,6:2@|7:3@|8:-1@|”, “8” , caOutBuff , “@|” , “:”);
if(iRet == 0)
{
printf(“%s\n” , caOutBuff);
}
while(1){}
}