Handle ampersand
Replaced with & in messages
This commit is contained in:
38
util.c
38
util.c
@@ -23,6 +23,7 @@
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void get_time(char *thetime)
|
||||
{
|
||||
@@ -54,3 +55,40 @@ char *trim(char *str)
|
||||
return str;
|
||||
}
|
||||
|
||||
char * str_replace (const char *string, const char *substr,
|
||||
const char *replacement) {
|
||||
char *tok = NULL;
|
||||
char *newstr = NULL;
|
||||
char *oldstr = NULL;
|
||||
char *head = NULL;
|
||||
|
||||
if ( substr == NULL || replacement == NULL )
|
||||
return strdup (string);
|
||||
|
||||
newstr = strdup (string);
|
||||
head = newstr;
|
||||
|
||||
while ( (tok = strstr ( head, substr ))) {
|
||||
oldstr = newstr;
|
||||
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) +
|
||||
strlen ( replacement ) + 1 );
|
||||
|
||||
if ( newstr == NULL ) {
|
||||
free (oldstr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy ( newstr, oldstr, tok - oldstr );
|
||||
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
|
||||
memcpy ( newstr + (tok - oldstr) + strlen( replacement ),
|
||||
tok + strlen ( substr ),
|
||||
strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
|
||||
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) +
|
||||
strlen ( replacement ) , 0, 1 );
|
||||
|
||||
head = newstr + (tok - oldstr) + strlen( replacement );
|
||||
free (oldstr);
|
||||
}
|
||||
|
||||
return newstr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user