Handle ampersand

Replaced with & in messages
This commit is contained in:
James Booth
2012-04-19 22:26:12 +01:00
parent 9805b2b2b2
commit 366eecc195
7 changed files with 118 additions and 3 deletions

38
util.c
View File

@@ -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;
}