library/src/blockclass/subclasses/BP_StringUtils.cc
// Creates a duplicated string array given a csv buffer (\n between rows).  Resultant csv member lists have null termination.
Creates a duplicated string array given a csv buffer (\n between rows).  Resultant csv member lists have null termination.
Creates a duplicated string array given a csv buffer ( \ n between rows ) . Resultant csv member lists have null termination .
bool BP_StringUtils::ParseCsv(bool remove_quotes)
{
	if(!this->string)
	{
		printf("\n [+] No string set.");
		return false;
	}
	char *explode_string = NULL;
	if(!strstr(this->string, "\n"))
	{
		explode_string = (char *) tq_calloc(this->tq, bpstrlen(this->string) + 5, 1, BPLN);
		sprintf(explode_string, "%s%s", this->string, "\n");
	}
	else
	{
		explode_string = this->string;
	}
	BP_StringUtils * str_util = new BP_StringUtils(explode_string);
	str_util->Explode("\n");
	char *new_string = NULL;
	if(!strstr(this->string, "\n"))
	{
		new_string = (char *) tq_calloc(this->tq, bpstrlen(str_util->string) + 5, 1, BPLN);
		sprintf(new_string, "%s%s", str_util->string, "\n");
	}
	size_t i = 0;
	char *** out_array = NULL;
	size_t out_element_count = 0;
	char ** exploded_array      = NULL;
	size_t exploded_array_count = 0;
	for(;
		i < str_util->exploded_count;
		i++
		)
	{
		if(! str_util->exploded[i] )
			continue;
		size_t double_quote_counter = 0;
		size_t quote_counter        = 0;
		bool quote_mismatch        = false;
		bool double_quote_mismatch = false;
		size_t x       = 0;
		size_t saved_x = 0;
		char *tok_string = str_util->exploded[i];
		if(!strnlen(str_util->exploded[i], 10))
		{
			continue;
		}
		size_t replace_size_count = 0;
		calculate_str_offset:
		for(;
			tok_string[x] != ',' && tok_string[x] != '\0';
			x++
			)
		{
			if(tok_string[x] == '"')
			{
				double_quote_counter++;
			}
			if(tok_string[x] == '\'')
			{
				quote_counter++;
			}
		}
		replace_size_count += x;
		if((double_quote_counter % 2) != 0)
		{
			double_quote_mismatch = true;
		}
		if((quote_counter %2) != 0)
		{
			quote_mismatch = true;
		}
		if(double_quote_mismatch)
		{
			double_quote_mismatch = false;
			quote_mismatch        = false;
			x++;
			saved_x = x;
			goto calculate_str_offset;
		}
		exploded_array_count++;
		exploded_array                             = (char **) tq_realloc(this->tq, exploded_array, sizeof(char *) * (exploded_array_count + 1) , BPLN);
		exploded_array[exploded_array_count -1 ]   = tq_strndup(this->tq, tok_string, x , BPLN);
		exploded_array[exploded_array_count]       = NULL;
		quote_counter = 0;
		double_quote_counter = 0;
		tok_string = (char *) &tok_string[x+1];
		if(bpstrlen(tok_string))
		{
			x=0;
			goto calculate_str_offset;
		}
		out_element_count++;
		out_array = (char ***) tq_realloc(this->tq, out_array, ( out_element_count + 2) * sizeof(char *), BPLN);
		size_t l = 0;
		for(;
			l < exploded_array_count;
			l++
			)
		{
			if(!exploded_array[l])
				continue;
		}
		out_array[out_element_count-1] = exploded_array;
		out_array[out_element_count]   = NULL;
		exploded_array       = NULL;
		exploded_array_count = 0;
	}
	if(str_util)
		delete str_util;
	this->csv          = out_array;
	this->csv_num_rows = out_element_count;
	return true;
}
bool BP_StringUtils :: ParseCsv ( bool remove_quotes ) { if ( ! this -> string ) { printf ( " \n " ) ; return false ; } char * explode_string = NULL ; if ( ! strstr ( this -> string , " \n " ) ) { explode_string = ( char * ) tq_calloc ( this -> tq , bpstrlen ( this -> string ) + 5 , 1 , BPLN ) ; sprintf ( explode_string , " " , this -> string , " \n " ) ; } else { explode_string = this -> string ; } BP_StringUtils * str_util = new BP_StringUtils ( explode_string ) ; str_util -> Explode ( " \n " ) ; char * new_string = NULL ; if ( ! strstr ( this -> string , " \n " ) ) { new_string = ( char * ) tq_calloc ( this -> tq , bpstrlen ( str_util -> string ) + 5 , 1 , BPLN ) ; sprintf ( new_string , " " , str_util -> string , " \n " ) ; } size_t i = 0 ; char * * * out_array = NULL ; size_t out_element_count = 0 ; char * * exploded_array = NULL ; size_t exploded_array_count = 0 ; for ( ; i < str_util -> exploded_count ; i ++ ) { if ( ! str_util -> exploded [ i ] ) continue ; size_t double_quote_counter = 0 ; size_t quote_counter = 0 ; bool quote_mismatch = false ; bool double_quote_mismatch = false ; size_t x = 0 ; size_t saved_x = 0 ; char * tok_string = str_util -> exploded [ i ] ; if ( ! strnlen ( str_util -> exploded [ i ] , 10 ) ) { continue ; } size_t replace_size_count = 0 ; calculate_str_offset : for ( ; tok_string [ x ] != ' ' && tok_string [ x ] != ' \0 ' ; x ++ ) { if ( tok_string [ x ] == ' ' ) { double_quote_counter ++ ; } if ( tok_string [ x ] == ' \' ' ) { quote_counter ++ ; } } replace_size_count += x ; if ( ( double_quote_counter % 2 ) != 0 ) { double_quote_mismatch = true ; } if ( ( quote_counter % 2 ) != 0 ) { quote_mismatch = true ; } if ( double_quote_mismatch ) { double_quote_mismatch = false ; quote_mismatch = false ; x ++ ; saved_x = x ; goto calculate_str_offset ; } exploded_array_count ++ ; exploded_array = ( char * * ) tq_realloc ( this -> tq , exploded_array , sizeof ( char * ) * ( exploded_array_count + 1 ) , BPLN ) ; exploded_array [ exploded_array_count - 1 ] = tq_strndup ( this -> tq , tok_string , x , BPLN ) ; exploded_array [ exploded_array_count ] = NULL ; quote_counter = 0 ; double_quote_counter = 0 ; tok_string = ( char * ) & tok_string [ x + 1 ] ; if ( bpstrlen ( tok_string ) ) { x = 0 ; goto calculate_str_offset ; } out_element_count ++ ; out_array = ( char * * * ) tq_realloc ( this -> tq , out_array , ( out_element_count + 2 ) * sizeof ( char * ) , BPLN ) ; size_t l = 0 ; for ( ; l < exploded_array_count ; l ++ ) { if ( ! exploded_array [ l ] ) continue ; } out_array [ out_element_count - 1 ] = exploded_array ; out_array [ out_element_count ] = NULL ; exploded_array = NULL ; exploded_array_count = 0 ; } if ( str_util ) delete str_util ; this -> csv = out_array ; this -> csv_num_rows = out_element_count ; return true ; }
Creates a duplicated string array given a csv buffer (\n between rows).
Creates a duplicated string array given a csv buffer ( \ n between rows ) .
// ensure we have a string before trying to parse it // string to use for exploding // add newline if needed // create new string utility object to manipulate string // break the buffer up into newlines // add newline if needed // walk each line and parse individual csv items // array returned outbound // exploded array since csv is a special case and cannot be exploded // Walk the exploded data.  Each element is a line of csv. // only process non-null lines // counters must be even in order to signify matching "" // these are set to true while calculating mismatches // for detecting string lengths // set token string before working on object // ensure the string has an actual length // printf("\n HERE? str_util->exploded[i]: %u , %s",i, str_util->exploded[i]); // printf("\n str_util->exploded[i]: %u , %s",i, str_util->exploded[i]); // printf("\n test string: %s", tok_string); // replacement count // JMP loop label, used if string comma count mismatch (i is eventually the length of string till real comma) // save x before loop // increase the size of the replacement counter the size of x // check the quote counters to determine if we have a mismatch // if there is a mismatch, the comma matched is not the correct one, and you must loop // reset indicator flags before looping // increase x counter // save x // JMP loop // add element to the exploded array // reset quote counter // reset double quote counter // printf("\n Before: tok_string -- %s  -- %u", tok_string, x ); // printf("\n Saved String: %s",                exploded_array[exploded_array_count -1 ]); // reassign token string // printf("\n After: tok_string -- %s", tok_string); // printf("\n"); // if there is more data in the string, go ahead and loop to populate next exploded array element // increase position // %%%%%%%%%%% DEV NOTE %%%%%%%%%%%%%%%%%%%%%%%%% // at this point the exploded array is populated // line_utils->exploded now contains array of csv values // loop the exploded array now // exploded string // add out array element // delete str utils if necessary // set class members // return indicating success