77 lines
1.3 KiB
C
77 lines
1.3 KiB
C
|
|
#include <common.h>
|
|
|
|
/*
|
|
* name : sunxi_str_replace
|
|
* fucntion: replace a word in string which separated by a space
|
|
* note : sunxi_str_replace("abc def gh", "def", "replace") get "abc replace gh"
|
|
*/
|
|
int sunxi_str_replace(char *dest_buf, char *goal, char *replace)
|
|
{
|
|
char tmp[128];
|
|
char tmp_str[16];
|
|
int goal_len, rep_len, dest_len;
|
|
int i, j, k;
|
|
|
|
if( (goal == NULL) || (dest_buf == NULL))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
memset(tmp, 0, 128);
|
|
strcpy(tmp, dest_buf);
|
|
|
|
goal_len = strlen(goal);
|
|
dest_len = strlen(dest_buf);
|
|
|
|
if(replace != NULL)
|
|
{
|
|
rep_len = strlen(replace);
|
|
}
|
|
else
|
|
{
|
|
rep_len = 0;
|
|
}
|
|
j = 0;
|
|
for(i=0;tmp[i];)
|
|
{
|
|
//找出空格字符
|
|
k = 0;
|
|
while(((tmp[i] != ' ') && (tmp[i] != 0) )|| (tmp[i+1] == ' '))
|
|
{
|
|
tmp_str[k++] = tmp[i];
|
|
i ++;
|
|
if(i >= dest_len)
|
|
break;
|
|
}
|
|
i ++;
|
|
//开始找出一个完整的字符串
|
|
tmp_str[k] = 0;
|
|
if(!strcmp(tmp_str, goal))
|
|
{
|
|
if(rep_len)
|
|
{
|
|
strcpy(dest_buf + j, replace);
|
|
if(tmp[j + goal_len])
|
|
{
|
|
memcpy(dest_buf + j + rep_len, tmp + j + goal_len, dest_len - j - goal_len);
|
|
dest_buf[dest_len - goal_len + rep_len] = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(tmp[j + goal_len])
|
|
{
|
|
memcpy(dest_buf + j, tmp + j + goal_len, dest_len - j - goal_len);
|
|
dest_buf[dest_len - goal_len + rep_len] = 0;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
j = i;
|
|
}
|
|
|
|
return 0;
|
|
|
|
} |