From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/strrepl.c | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/strrepl.c (limited to 'reference/C/CONTRIB/SNIP/strrepl.c') diff --git a/reference/C/CONTRIB/SNIP/strrepl.c b/reference/C/CONTRIB/SNIP/strrepl.c new file mode 100755 index 0000000..bca07c4 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/strrepl.c @@ -0,0 +1,74 @@ +/* + -------------------------------------------------------------------- + Module: REPLACE.C + Author: Gilles Kohl + Started: 09.06.1992 12:16:47 + Modified: 09.06.1992 12:41:41 + Subject: Replace one string by another in a given buffer. + This code is public domain. Use freely. + -------------------------------------------------------------------- +*/ + +#include +#include +#include + +/* + * StrReplace: Replace OldStr by NewStr in string Str. + * + * Str should have enough allocated space for the replacement, no check + * is made for this. Str and OldStr/NewStr should not overlap. + * The empty string ("") is found at the beginning of every string. + * + * Returns: pointer to first location behind where NewStr was inserted + * or NULL if OldStr was not found. + * This is useful for multiple replacements, see example in main() below + * (be careful not to replace the empty string this way !) + */ + +char *StrReplace(char *Str, char *OldStr, char *NewStr) +{ + int OldLen, NewLen; + char *p, *q; + + if(NULL == (p = strstr(Str, OldStr))) + return p; + OldLen = strlen(OldStr); + NewLen = strlen(NewStr); + memmove(q = p+NewLen, p+OldLen, strlen(p+OldLen)+1); + memcpy(p, NewStr, NewLen); + return q; +} + +#ifdef TEST + +/* + * Test main(). + * Given two arguments, replaces the first arg. in the lines read from + * stdin by the second one. + * Example invocation: + * replace printf puts log msg
diff options
context:
space:
mode: