library/src/utility/BP-StringUtilities.cc
// This routine will attempt to trim a REASONABLE
// string based around parameters.  Max trim is the
// maximum trim depth this will allow.
This routine will attempt to trim a REASONABLE
string based around parameters.  Max trim is the
maximum trim depth this will allow.
This routine will attempt to trim a REASONABLE string based around parameters . Max trim is the maximum trim depth this will allow .
char * BP_StringTrim
(
		char * str_to_trim,
		BP_STRING_TRIM_DIRECTION trim_direction,
		BP_BOOL trim_newline,
		BP_BOOL trim_tab,
		BP_BOOL trim_space,
		BP_BOOL trim_carriage_return,
		char    trim_arbitrary_char,
		size_t  max_trim
)
{
	if(!str_to_trim)
		return NULL;
	if(!trim_direction)
		return NULL;
	size_t str_to_trim_len = bpstrlen(str_to_trim);
	if(!BP_StringIsReasonablePrintableString(str_to_trim, bpstrlen(str_to_trim), BP_TRUE, BP_TRUE))
		return NULL;
	size_t n = 0;
	char * new_str = NULL;
	size_t new_str_len = 0;
	char * final_string = NULL;
	size_t total_trims = 0;
	if(trim_direction == BP_STRING_TRIM_DIRECTION_RIGHT || trim_direction == BP_STRING_TRIM_DIRECTION_BOTH)
	{
		new_str     = bpstrdup(str_to_trim);
		new_str_len = bpstrlen(new_str);
		for(n = new_str_len; n; n--)
		{
			if(max_trim <= total_trims)
				break;
			if(trim_newline)
			if(new_str[n-1] == '\n')
			{
				new_str[n-1] = '\0';
				total_trims++;
				continue;
			}
			if(trim_carriage_return)
			if(new_str[n-1] == '\r')
			{
				new_str[n-1] = '\0';
				total_trims++;
				continue;
			}
			if(trim_space)
			if(new_str[n-1] == ' ')
			{
				new_str[n-1] = '\0';
				total_trims++;
				continue;
			}
			if(trim_tab)
			if(new_str[n-1] == '\t')
			{
				new_str[n-1] = '\0';
				total_trims++;
				continue;
			}
			if(trim_arbitrary_char)
			if(new_str[n-1] == trim_arbitrary_char)
			{
				new_str[n-1] = '\0';
				total_trims++;
				continue;
			}
			break;
		}
	}
	if(trim_direction == BP_STRING_TRIM_DIRECTION_LEFT || trim_direction == BP_STRING_TRIM_DIRECTION_BOTH)
	{
		 if necessary
		if(!new_str)
		{
			new_str     = bpstrdup(str_to_trim);
			new_str_len = str_to_trim_len;
		}
		else
		{
			new_str_len = bpstrlen(new_str);
		}
		for(n = 0; n < new_str_len; n++)
		{
			if(max_trim <= total_trims)
				break;
			if(trim_newline)
			if(new_str[n] == '\n')
			{
				total_trims++;
				continue;
			}
			if(trim_carriage_return)
			if(new_str[n] == '\r')
			{
				total_trims++;
				continue;
			}
			if(trim_space)
			if(new_str[n] == ' ')
			{
				total_trims++;
				continue;
			}
			if(trim_tab)
			if(new_str[n] == '\t')
			{
				total_trims++;
				continue;
			}
			if(trim_arbitrary_char)
			if(new_str[n] == trim_arbitrary_char)
			{
				total_trims++;
				continue;
			}
			break;
		}
		final_string = bpstrdup((char *) &new_str[n]);
	}
	if(!new_str)
		return NULL;
	if(!bpstrlen(new_str))
	{
		bpfree(new_str);
		return NULL;
	}
	if(!final_string)
		final_string = bpstrdup(new_str);
	bpfree(new_str);
	return final_string;
}
char * BP_StringTrim ( char * str_to_trim , BP_STRING_TRIM_DIRECTION trim_direction , BP_BOOL trim_newline , BP_BOOL trim_tab , BP_BOOL trim_space , BP_BOOL trim_carriage_return , char trim_arbitrary_char , size_t max_trim ) { if ( ! str_to_trim ) return NULL ; if ( ! trim_direction ) return NULL ; size_t str_to_trim_len = bpstrlen ( str_to_trim ) ; if ( ! BP_StringIsReasonablePrintableString ( str_to_trim , bpstrlen ( str_to_trim ) , BP_TRUE , BP_TRUE ) ) return NULL ; size_t n = 0 ; char * new_str = NULL ; size_t new_str_len = 0 ; char * final_string = NULL ; size_t total_trims = 0 ; if ( trim_direction == BP_STRING_TRIM_DIRECTION_RIGHT || trim_direction == BP_STRING_TRIM_DIRECTION_BOTH ) { new_str = bpstrdup ( str_to_trim ) ; new_str_len = bpstrlen ( new_str ) ; for ( n = new_str_len ; n ; n -- ) { if ( max_trim <= total_trims ) break ; if ( trim_newline ) if ( new_str [ n - 1 ] == ' \n ' ) { new_str [ n - 1 ] = ' \0 ' ; total_trims ++ ; continue ; } if ( trim_carriage_return ) if ( new_str [ n - 1 ] == ' \r ' ) { new_str [ n - 1 ] = ' \0 ' ; total_trims ++ ; continue ; } if ( trim_space ) if ( new_str [ n - 1 ] == ' ' ) { new_str [ n - 1 ] = ' \0 ' ; total_trims ++ ; continue ; } if ( trim_tab ) if ( new_str [ n - 1 ] == ' \t ' ) { new_str [ n - 1 ] = ' \0 ' ; total_trims ++ ; continue ; } if ( trim_arbitrary_char ) if ( new_str [ n - 1 ] == trim_arbitrary_char ) { new_str [ n - 1 ] = ' \0 ' ; total_trims ++ ; continue ; } break ; } } if ( trim_direction == BP_STRING_TRIM_DIRECTION_LEFT || trim_direction == BP_STRING_TRIM_DIRECTION_BOTH ) { if ( ! new_str ) { new_str = bpstrdup ( str_to_trim ) ; new_str_len = str_to_trim_len ; } else { new_str_len = bpstrlen ( new_str ) ; } for ( n = 0 ; n < new_str_len ; n ++ ) { if ( max_trim <= total_trims ) break ; if ( trim_newline ) if ( new_str [ n ] == ' \n ' ) { total_trims ++ ; continue ; } if ( trim_carriage_return ) if ( new_str [ n ] == ' \r ' ) { total_trims ++ ; continue ; } if ( trim_space ) if ( new_str [ n ] == ' ' ) { total_trims ++ ; continue ; } if ( trim_tab ) if ( new_str [ n ] == ' \t ' ) { total_trims ++ ; continue ; } if ( trim_arbitrary_char ) if ( new_str [ n ] == trim_arbitrary_char ) { total_trims ++ ; continue ; } break ; } final_string = bpstrdup ( ( char * ) & new_str [ n ] ) ; } if ( ! new_str ) return NULL ; if ( ! bpstrlen ( new_str ) ) { bpfree ( new_str ) ; return NULL ; } if ( ! final_string ) final_string = bpstrdup ( new_str ) ; bpfree ( new_str ) ; return final_string ; }
This routine will attempt to trim a REASONABLE
string based around parameters.
This routine will attempt to trim a REASONABLE string based around parameters .
// trim front // attempt to calculate trim length // string must be deemed reasonable before any processing occurs // string counter // new string pointer // size placeholder for new string // final string which will be returned (trimmed string) // tracks the total number of times a character was trimmed // trim right if user specified (simple nullify) // LOOP NOTE: n indexes must be -1 in the right-end loop due to bpstrlen returning a +1 index. // allocate new string // nullify trailing whitespace // stop loop if we hit our cap on total trims // run newline check // run carriage return check // run space check // run tab check // if no characters match, we can break here // debug verbosity //if(new_str) //	printf("\n [!!] Got after end replace???: \"%s\"", new_str); // trim left if user specified // allocate new string if necessary // recalculate length just in case we // nullified any tail characters above. // nullify trailing whitespace // stop loop if we hit our cap on total trims // run newline check // run carriage return check // run space check // run tab check // if no characters match, we've reached // the end and can break. // set final string // if we haven't created any new strings, no replacements // took place. // dup the final string from newstring if only // the tailpart was fixed up. // free the new string // return the final prepared string (trimmed)