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 uses
// Posix 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 uses
Posix 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 uses Posix Regular Expressions .
char * BP_GenerateSubstitutedTemplateREGEXP(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 = bpstrdup(template_data);
	char * replaced_str = NULL;
	char *replace_this = NULL;
	char *replace_with = NULL;
	replaced_str = 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);
		tmp_str  = BP_RegFindReplace
		(
			replaced_str,
			replace_this,
			replace_with,
			replace_len,    
			1, 				
			0
		);
		if(tmp_str)
		{
			if(replaced_str != mod_string)
				bpfree(replaced_str);
			replaced_str = tmp_str;
		}
		if(!replaced_str )
		{
			replaced_str = mod_string;
		}
	}
	if(mod_string)
		bpfree(mod_string);
	return replaced_str;
}
char * BP_GenerateSubstitutedTemplateREGEXP ( 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 = bpstrdup ( template_data ) ; char * replaced_str = NULL ; char * replace_this = NULL ; char * replace_with = NULL ; replaced_str = 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 ) ; tmp_str = BP_RegFindReplace ( replaced_str , replace_this , replace_with , replace_len , 1 , 0 ) ; if ( tmp_str ) { if ( replaced_str != mod_string ) bpfree ( replaced_str ) ; replaced_str = tmp_str ; } if ( ! replaced_str ) { replaced_str = mod_string ; } } if ( mod_string ) bpfree ( mod_string ) ; return replaced_str ; }
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 // set loop iter // temporary loop iteration string // ensure we have a string to replace in // duplicate string before loop // set tmp string as mod string // run replacement loop // setup find and replace items // ensure we have pointers to find/replace strings // replace length used for updating // attempt to run find and replace // replace size // max replacements // check to see that we have a temporary string // free only if the replace string and mod string aren't the same // set the replaced string here // if the data couldn't be acted on, restore the old ptr on loop // free string if necessary (should always be necessary) // return the replaced value