library/src/utility/BP-StringUtilities.cc
// Generates a heap buffer storing the color prefix data for a color set.  Result
// must be freed using bpfree (or tq_free if linked into a linkl)
Generates a heap buffer storing the color prefix data for a color set.  Result
must be freed using bpfree (or tq_free if linked into a linkl)
Generates a heap buffer storing the color prefix data for a color set . Result must be freed using bpfree ( or tq_free if linked into a linkl )
char * BP_GenerateTerminalColorPrefixFromColorSet(P_BP_COLOR_CODE_SET_T colors)
{
	if(!colors)
		return NULL;
	if(!colors->bg && !colors->fg)
		return NULL;
	char * prefix = (char *)  bpcalloc(255,1);
	if(!prefix)
		return NULL;
	BP_BOOL color_truncated = BP_FALSE;
	if(colors->fg || colors->bg)
	{
		strcat(prefix, BP_COLOR_CODE_PREFIX);
		if(colors->fg)
		{
			strcat(prefix, BP_FOREGROUND_COLOR_CODES[colors->fg] );
			if(!colors->bg)
				strcat(prefix, "m");
		}
		if(colors->bg)
		{
			if(!colors->fg)
			{
				strcat(prefix, BP_FOREGROUND_COLOR_CODES[BP_COLOR_CODE_BG_WHITE]);
				strcat(prefix, ";");
			}
			else
			{
				strcat(prefix, ";");
			}
			strcat(prefix, BP_BACKGROUND_COLOR_CODES[colors->bg]);
			strcat(prefix, "m");
		}
	}
	size_t prefix_len = 0;
	prefix_len = bpstrlen(prefix);
	prefix = (char *) bprealloc((void *) prefix, prefix_len+1);
	if(!prefix)
		return NULL;
	prefix[prefix_len] = (char) 0x00;
	return prefix;
}
char * BP_GenerateTerminalColorPrefixFromColorSet ( P_BP_COLOR_CODE_SET_T colors ) { if ( ! colors ) return NULL ; if ( ! colors -> bg && ! colors -> fg ) return NULL ; char * prefix = ( char * ) bpcalloc ( 255 , 1 ) ; if ( ! prefix ) return NULL ; BP_BOOL color_truncated = BP_FALSE ; if ( colors -> fg || colors -> bg ) { strcat ( prefix , BP_COLOR_CODE_PREFIX ) ; if ( colors -> fg ) { strcat ( prefix , BP_FOREGROUND_COLOR_CODES [ colors -> fg ] ) ; if ( ! colors -> bg ) strcat ( prefix , " " ) ; } if ( colors -> bg ) { if ( ! colors -> fg ) { strcat ( prefix , BP_FOREGROUND_COLOR_CODES [ BP_COLOR_CODE_BG_WHITE ] ) ; strcat ( prefix , " " ) ; } else { strcat ( prefix , " " ) ; } strcat ( prefix , BP_BACKGROUND_COLOR_CODES [ colors -> bg ] ) ; strcat ( prefix , " " ) ; } } size_t prefix_len = 0 ; prefix_len = bpstrlen ( prefix ) ; prefix = ( char * ) bprealloc ( ( void * ) prefix , prefix_len + 1 ) ; if ( ! prefix ) return NULL ; prefix [ prefix_len ] = ( char ) 0x00 ; return prefix ; }
Generates a heap buffer storing the color prefix data for a color set.
Generates a heap buffer storing the color prefix data for a color set .
// ensure we have a valid pointer with fg/bg data. // create return buffer // flag indicating whether or not the sequence has been truncated // add prefix if we have a foreground/background color // add prefix // add foreground color if necessary // declare prefix and format length // calculate prefix and fmt // trim off the remaining bytes (as 255 bytes is likely too long) // ensure the realloc space is null terminated // return the buffer