函数功能:区号比较
入参说明:
exten:需要比较的区号,如028
code:区号字符串,如0731,0732,021,010,028,0838
心得:
1. 不要修改入参的内容==》可以malloc内存,将内容复制到新的内存,注意释放内存;
2. malloc的内存,千万不要使用'/0',否则会造成内存释放不完全,即内存泄露。
错误代码:
static int avx_res_cmpAreaCode(char *exten, char *code)
{
char *p, *q;
int i;
p = (char*)malloc(strlen(code) +1);
if (!p) {
ast_log(LOG_WARNING, "mallor error!/n");
return -1;
}
strcpy(p, code);
while (p)
{
q = p;
p = strstr(p,",");
if (p)
{
*p = '/0';
p ++;
}
if(!(i=strncmp(exten,q,strlen(q))))
break;
}
if (p)
free(p);
return i;
}
修正代码:
tatic int avx_res_cmpAreaCode(char *exten, char *code)
{
char *prev, *tail;
char tmp[10];
int i;
int ret =-1;
tail = code;
while (tail)
{
prev = tail;
tail = strstr(tail,",");
for (i=0; prev != tail && *prev != '/0'; prev++,i++)
tmp[i] = *prev;
tmp[i] = '/0';
if (tail)
tail++;
if(!(ret=strncmp(exten,tmp,i)))
break;
}
return ret;
}