library/src/utility/BP-StringUtilities.cc
// Wrapper for printf using VT colors
Wrapper for printf using VT colors
Wrapper for printf using VT colors
BP_ERROR_T BP_printfVTColor(const char * fmt,  BP_FOREGROUND_COLOR_CODE_T fgc, BP_BACKGROUND_COLOR_CODE_T bgc, ...)
{
	if(!fmt)
		return ERR_FAILURE;
	BP_RESET_TERMINAL_COLOR;
	va_list args;
	va_start(args, fmt);
	char prefix[255];
	BP_ZERO_STACK(prefix);
	BP_BOOL color_truncated = BP_FALSE;
	if(fgc || bgc)
	{
		strcat(prefix, BP_COLOR_CODE_PREFIX);
		if(fgc)
		{
			strcat(prefix, BP_FOREGROUND_COLOR_CODES[fgc] );
			if(!bgc)
				strcat(prefix, "m");
		}
		if(bgc)
		{
			if(!fgc)
			{
				strcat(prefix, BP_FOREGROUND_COLOR_CODES[BP_COLOR_CODE_BG_WHITE]);
				strcat(prefix, ";");
			}
			else
			{
				strcat(prefix, ";");
			}
			strcat(prefix, BP_BACKGROUND_COLOR_CODES[bgc]);
			strcat(prefix, "m");
		}
	}
	size_t prefix_len = 0;
	size_t fmt_len    = 0;
	prefix_len = bpstrlen(prefix);
	fmt_len    = bpstrlen(fmt);
	char *out_fmt = (char *) bpcalloc(prefix_len+fmt_len+1, 1);
	if(prefix_len)
		strcat(out_fmt, prefix);
	if(fmt_len)
		strcat(out_fmt, fmt);
	vfprintf(stdout, out_fmt, args);
	va_end(args);
	BP_RESET_TERMINAL_COLOR;
	if(out_fmt)
		bpfree(out_fmt);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_printfVTColor ( const char * fmt , BP_FOREGROUND_COLOR_CODE_T fgc , BP_BACKGROUND_COLOR_CODE_T bgc , ... ) { if ( ! fmt ) return ERR_FAILURE ; BP_RESET_TERMINAL_COLOR ; va_list args ; va_start ( args , fmt ) ; char prefix [ 255 ] ; BP_ZERO_STACK ( prefix ) ; BP_BOOL color_truncated = BP_FALSE ; if ( fgc || bgc ) { strcat ( prefix , BP_COLOR_CODE_PREFIX ) ; if ( fgc ) { strcat ( prefix , BP_FOREGROUND_COLOR_CODES [ fgc ] ) ; if ( ! bgc ) strcat ( prefix , " " ) ; } if ( bgc ) { if ( ! fgc ) { strcat ( prefix , BP_FOREGROUND_COLOR_CODES [ BP_COLOR_CODE_BG_WHITE ] ) ; strcat ( prefix , " " ) ; } else { strcat ( prefix , " " ) ; } strcat ( prefix , BP_BACKGROUND_COLOR_CODES [ bgc ] ) ; strcat ( prefix , " " ) ; } } size_t prefix_len = 0 ; size_t fmt_len = 0 ; prefix_len = bpstrlen ( prefix ) ; fmt_len = bpstrlen ( fmt ) ; char * out_fmt = ( char * ) bpcalloc ( prefix_len + fmt_len + 1 , 1 ) ; if ( prefix_len ) strcat ( out_fmt , prefix ) ; if ( fmt_len ) strcat ( out_fmt , fmt ) ; vfprintf ( stdout , out_fmt , args ) ; va_end ( args ) ; BP_RESET_TERMINAL_COLOR ; if ( out_fmt ) bpfree ( out_fmt ) ; return ERR_SUCCESS ; }
Wrapper for printf using VT colors
Wrapper for printf using VT colors
// exit here if no format is supplied // reset terminal color before print // variable argument list // create and zero out the stack prefix // 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 // allocate memory // copy in the data // restore the normal colors // end vargs // now reset terminal colors // destroy the buffer before exit // return indicating success