const int MAX_STRLEN = 256;
//---------------------------------------------------------------------------
// Name : strrep
// Desc : Replace all matched substring.
// Param :
// str : Null-terminated string to replace.
// match : Null-terminated string to match for.
// rep : string to replace for.
// return : 0 Success,-1 Fail.
int strrep(char *str, const char *match, const char *rep)
{
if (!str || !match)
return -1;
char tmpstr[MAX_STRLEN];
char *pos = strstr(str, match);
while (pos)
{
memset(tmpstr, 0, sizeof(tmpstr));
strncpy(tmpstr, str, pos - str);
strcat(tmpstr, rep);
strcat(tmpstr, pos + strlen(match));
strcpy(str, tmpstr);
pos = strstr(str, match);
}
return 0;
}
//---------------------------------------------------------------------------
// Name : wcsrep
// Desc : Replace all matched substring.
// Param :
// str : Null-terminated string to replace.
// match : Null-terminated string to match for.
// rep : string to replace for.
// return : 0 Success,-1 Fail.
int wcsrep(wchar_t *str, const wchar_t *match, const wchar_t *rep)
{
if (!str || !match)
return -1;
wchar_t tmpstr[MAX_STRLEN];
wchar_t *pos = wcsstr(str, match);
while (pos)
{
memset(tmpstr, 0, sizeof(tmpstr));
wcsncpy(tmpstr, str, pos - str);
wcscat(tmpstr, rep);
wcscat(tmpstr, pos + wcslen(match));
wcscpy(str, tmpstr);
pos = wcsstr(str, match);
}
return 0;
}
//---------------------------------------------------------------------------