library/src/utility/BP-StringUtilities.cc
// Colorizes a string based around the colors passed in.  Each character must have a
// matching colors pointer entry.  This means that the string length must equal the number
// of pointers available.  Don't mess with this routine, it's perfect.
Colorizes a string based around the colors passed in.  Each character must have a
matching colors pointer entry.  This means that the string length must equal the number
of pointers available.  Don't mess with this routine, it's perfect.
Colorizes a string based around the colors passed in . Each character must have a matching colors pointer entry . This means that the string length must equal the number of pointers available . Don ' t mess with this routine it ' s perfect .
char * BP_ColorizeString(char * string, size_t string_len, P_BP_COLOR_CODE_SET_T * colors, BP_BOOL terminate_with_color_reset)
{
	if(!string || !string_len || !colors)
		return NULL;
	char * out_str = NULL;
	P_BP_LINKL tq = BP_CreateLinkL(".", BP_FALSE);
	char *color_prefix = NULL;
	char *last_color_prefix = NULL;
	size_t n = 0;
	for(; n < string_len; n++)
	{
		if(colors[n])
		{
			color_prefix = BP_GenerateTerminalColorPrefixFromColorSet(colors[n]);
			if(color_prefix)
			{
				tq_strdup(tq, color_prefix, BPLN);
			}
		}
		tq_strndup(tq, &string[n], 1, BPLN);
		if(color_prefix)
		{
			bpfree(color_prefix);
			color_prefix = NULL;
			if(terminate_with_color_reset == BP_TRUE)
				tq_strdup(tq, BP_COLOR_CODE_RESET_DEFAULT_COLORS, BPLN);
		}
	}
	BP_LinkLFlattenJoinStringElements(tq);
	out_str = (char *) tq->flat_buff;
	if(out_str)
		out_str = bpstrdup(out_str);
	BP_DestroyLinkL(tq, BP_TRUE);
	return out_str;
}
char * BP_ColorizeString ( char * string , size_t string_len , P_BP_COLOR_CODE_SET_T * colors , BP_BOOL terminate_with_color_reset ) { if ( ! string || ! string_len || ! colors ) return NULL ; char * out_str = NULL ; P_BP_LINKL tq = BP_CreateLinkL ( " " , BP_FALSE ) ; char * color_prefix = NULL ; char * last_color_prefix = NULL ; size_t n = 0 ; for ( ; n < string_len ; n ++ ) { if ( colors [ n ] ) { color_prefix = BP_GenerateTerminalColorPrefixFromColorSet ( colors [ n ] ) ; if ( color_prefix ) { tq_strdup ( tq , color_prefix , BPLN ) ; } } tq_strndup ( tq , & string [ n ] , 1 , BPLN ) ; if ( color_prefix ) { bpfree ( color_prefix ) ; color_prefix = NULL ; if ( terminate_with_color_reset == BP_TRUE ) tq_strdup ( tq , BP_COLOR_CODE_RESET_DEFAULT_COLORS , BPLN ) ; } } BP_LinkLFlattenJoinStringElements ( tq ) ; out_str = ( char * ) tq -> flat_buff ; if ( out_str ) out_str = bpstrdup ( out_str ) ; BP_DestroyLinkL ( tq , BP_TRUE ) ; return out_str ; }
Colorizes a string based around the colors passed in.
Colorizes a string based around the colors passed in .
// string to be returned // LinkL used for pushing entries. // this is generated once per loop // this stores the last generated color prefix, it's compared // with the current color prefix to determine if we need to re-colorize. // string length must match colors // generate color prefix // color must be set // link the color in // link in the character // destroy the color prefix on loop // reset terminal data if set // flatten the tq (creates the final colorized string) // set the buffer to return as the new flat buffer // generate the return buffer here on the normal heap, aka // copy it from the tq. // destroy the linkl // return the string