Umesh wrote:
Do you have any answer to it? thx.
/************************************************** ********************/
/* File Name: replace.c. */
/* Author: Stan Milam. */
/* Date Written: 13-Jan-2003. */
/* Description: */
/* Implement a function replace all occurances of a pattern. */
/* */
/* (c) Copyright 2007 by Stan Milam. */
/* All rights reserved. */
/* */
/************************************************** ********************/
#include <string.h>
#include <stddef.h>
#include "booldefs.h"
/* $Revision: 1.3 $ */
extern char tb_empty[];
extern char tb_copyright[];
static char *copyright = tb_copyright;
/************************************************** ********************/
/* Name: */
/* replace(). */
/* */
/* Synopsis: */
/* #include "strtools.h" */
/* char *replace(char *target, const char *from, char *to */
/* */
/* Description: */
/* The replace function will replace all instances of the from */
/* pattern with that of the to pattern. If the to pattern is */
/* an empty string or NULL all instances of the from pattern will */
/* be removed from the target string. */
/* */
/* Note: */
/* Since a replacement pattern can be longer the the original */
/* pattern the programmer must allow for enough memory to */
/* accomodate a larger target string to allow successful */
/* completion of the operation. */
/* */
/* Arguments: */
/* char *target - The string in which patterns will be */
/* replaced. Cannot be NULL */
/* */
/* const char *from - The pattern to be replaced. Cannot be NULL */
/* or an empty string (i.e. ""). */
/* */
/* const char *to - The replacement pattern. Using a NULL */
/* pointer will be interpreted the same as an */
/* empty string (i.e. ""). */
/* */
/* Return Value: */
/* In all cases the address of the target string. Should either */
/* the target string or the from pattern be NULL the global errno */
/* variable will be set to EINVAL. */
/* */
/************************************************** ********************/
char *
replace ( char *p_target, const char *p_from, const char *p_to )
{
size_t to_len, from_len;
char *l_to, *ptr, *rv = p_target;
/************************************************** ************/
/* I should not have to do this but some compilers complain */
/* when I try to assign the empty pointer to p_to. p_to */
/* points to a string of const characters. I should be able */
/* to change the pointer! */
/************************************************** ************/
l_to = p_to == NULL ? tb_empty : (char *) p_to;
/************************************************** ************/
/* Get the lengths before we enter the loop. */
/************************************************** ************/
to_len = strlen( l_to );
from_len = strlen( p_from );
for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {
/************************************************** ********/
/* We must either shrink out the replaced pattern or make */
/* room for the new pattern. If the two patterns are of */
/* equal length we don't care. */
/************************************************** ********/
if ( to_len != from_len )
memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);
/************************************************** ********/
/* Once everthing is adjusted all we have to do is */
/* replace :-). */
/************************************************** ********/
if ( to_len ) {
memmove( ptr, l_to, to_len );
ptr = ptr + to_len;
}
}
return rv;
}
--
Regards,
Stan Milam
================================================== ===========
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
================================================== ===========