library/src/utility/BP-GeneralPurposeTemplateSubstitution.cc
// This function finds and replaces all items in a buffer, creating a working
// filled out template.  The filled out template is allocated and returned in
// a pointer (which must later after use, be bpfreed).  This function is literal
// and does not replace regular expressions.
This function finds and replaces all items in a buffer, creating a working
filled out template.  The filled out template is allocated and returned in
a pointer (which must later after use, be bpfreed).  This function is literal
and does not replace regular expressions.
This function finds and replaces all items in a buffer creating a working filled out template . The filled out template is allocated and returned in a pointer ( which must later after use be bpfreed ) . This function is literal and does not replace regular expressions .
char * BP_GenerateSubstitutedTemplate(char *template_data, char **find_strs, char **replace_strs)
{
	if(!template_data || !find_strs || !replace_strs)
		return NULL;
	if(!find_strs[0] || !replace_strs[0])
		return NULL;
	size_t find_item_count = 0;
	size_t replace_count   = 0;
	size_t n = 0;
	for(n = 0; find_strs[n];    n++){ find_item_count++; }
	for(n = 0; replace_strs[n]; n++){ replace_count++; }
	if(find_item_count != replace_count)
	{
		printf("\n Item count  mismatch?!");
		return NULL;
	}
	size_t j = 0;
	char *tmp_str = NULL;
	if(!template_data)
	{
		return NULL;
	}
	char * mod_string     = NULL;
	char * replace_string = NULL;
	char * replace_string_last = NULL;
	char *replace_this    = NULL;
	char *replace_with    = NULL;
	mod_string = bpstrdup(template_data);
	replace_string = mod_string;
	for(; j < replace_count; j++)
	{
		replace_this = find_strs[j];
		replace_with = replace_strs[j];
		if(!replace_this || !replace_with)
			break;
		size_t replace_len = 0;
		if(replace_with)
			replace_len = bpstrlen(replace_with);
		if(!replace_string)
			replace_string = mod_string;
		replace_string_last  = BP_FindReplace
		(
			replace_string,
			replace_this,
			replace_with,
			replace_len,  	
			1
		);
		if(!replace_string_last)
		{
			if(!replace_string)
				replace_string = mod_string;
		}
		else
		{
			if(mod_string != replace_string)
				bpfree(replace_string);
			replace_string = replace_string_last;
		}
	}
	if(mod_string)
	if(mod_string != replace_string)
	{
		bpfree(mod_string);
	}
	return replace_string;
}
char * BP_GenerateSubstitutedTemplate ( char * template_data , char * * find_strs , char * * replace_strs ) { if ( ! template_data || ! find_strs || ! replace_strs ) return NULL ; if ( ! find_strs [ 0 ] || ! replace_strs [ 0 ] ) return NULL ; size_t find_item_count = 0 ; size_t replace_count = 0 ; size_t n = 0 ; for ( n = 0 ; find_strs [ n ] ; n ++ ) { find_item_count ++ ; } for ( n = 0 ; replace_strs [ n ] ; n ++ ) { replace_count ++ ; } if ( find_item_count != replace_count ) { printf ( " \n " ) ; return NULL ; } size_t j = 0 ; char * tmp_str = NULL ; if ( ! template_data ) { return NULL ; } char * mod_string = NULL ; char * replace_string = NULL ; char * replace_string_last = NULL ; char * replace_this = NULL ; char * replace_with = NULL ; mod_string = bpstrdup ( template_data ) ; replace_string = mod_string ; for ( ; j < replace_count ; j ++ ) { replace_this = find_strs [ j ] ; replace_with = replace_strs [ j ] ; if ( ! replace_this || ! replace_with ) break ; size_t replace_len = 0 ; if ( replace_with ) replace_len = bpstrlen ( replace_with ) ; if ( ! replace_string ) replace_string = mod_string ; replace_string_last = BP_FindReplace ( replace_string , replace_this , replace_with , replace_len , 1 ) ; if ( ! replace_string_last ) { if ( ! replace_string ) replace_string = mod_string ; } else { if ( mod_string != replace_string ) bpfree ( replace_string ) ; replace_string = replace_string_last ; } } if ( mod_string ) if ( mod_string != replace_string ) { bpfree ( mod_string ) ; } return replace_string ; }
This function finds and replaces all items in a buffer, creating a working
filled out template.
This function finds and replaces all items in a buffer creating a working filled out template .
// check template_data parameters first // make sure we have first elements // counts the elements in the find and replace columns // loop counter // check find item counts // temporary loop iteration string // ensure we have a string to replace in // duplicate string before loop // duplicate string here // run replacement loop // setup find and replace items // ensure we have replace pointers // replace length used for updating // printf("\n Got: %p", replace_string); // replace the string (no regexp) // replace size // set replace string as mod string // check replace/mod string before freeing // set pointers here // check mod string // return indicating success