library/src/utility/BP-StringUtilities.cc
// This routine joins an array of buffers (simple structure containing a pointer and a size), and stores the resulting
// joined buffer in the out_buff parameter.  It's done this way so we don't have to allocate the out_buff
// for the user, and they can just supply a stack pointer etc.  If this routine returns ERR_SUCCESS, it means
// that the data and len members of out_buff have been set to the joined data, and can then be used.  The
// data member is allocated on the heap directly if no tq_in is provided.  If one is provided, it is allocated
// via that tailqueue.
This routine joins an array of buffers (simple structure containing a pointer and a size), and stores the resulting
joined buffer in the out_buff parameter.  It's done this way so we don't have to allocate the out_buff
for the user, and they can just supply a stack pointer etc.  If this routine returns ERR_SUCCESS, it means
that the data and len members of out_buff have been set to the joined data, and can then be used.  The
data member is allocated on the heap directly if no tq_in is provided.  If one is provided, it is allocated
via that tailqueue.
This routine joins an array of buffers ( simple structure containing a pointer and a size ) and stores the resulting joined buffer in the out_buff parameter . It ' s done this way so we don ' t have to allocate the out_buff for the user and they can just supply a stack pointer etc . If this routine returns ERR_SUCCESS it means that the data and len members of out_buff have been set to the joined data and can then be used . The data member is allocated on the heap directly if no tq_in is provided . If one is provided it is allocated via that tailqueue .
BP_ERROR_T BP_JoinMemArray(P_BP_BUFFER_T mem_array, size_t entry_count, P_BP_BUFFER_T out_buff, P_BP_LINKL tq_in)
{
	if(!mem_array || !entry_count || !out_buff)
		return ERR_FAILURE;
	if(entry_count < 2)
		return ERR_FAILURE;
	BP_BUFFER_T final_buffer;
	BP_ZERO_STACK(final_buffer);
	BP_BUFFER_T last_buffer;
	BP_ZERO_STACK(last_buffer);
	BP_BOOL is_first_iter = BP_TRUE;
	size_t n = 0;
	for(; n < entry_count; n++)
	{
		if(!mem_array[n].data || !mem_array[n].len)
			continue;
		if(is_first_iter == BP_TRUE)
		{
			final_buffer.data = BP_JoinMem
			(
				mem_array[0].data, mem_array[0].len,
				mem_array[1].data, mem_array[1].len,
				tq_in
			);
			final_buffer.len = mem_array[0].len + mem_array[1].len;
			if(!final_buffer.data || !final_buffer.len)
				return ERR_FAILURE;
			last_buffer.data = final_buffer.data;
			last_buffer.len  = final_buffer.len;
			is_first_iter = BP_FALSE;
			n++;
			continue;
		}
		final_buffer.data = BP_JoinMem
		(
			last_buffer.data,  last_buffer.len,
			mem_array[n].data, mem_array[n].len,
			tq_in
		);
		final_buffer.len += mem_array[n].len;
		if(tq_in)
		{
			tq_free(tq_in, last_buffer.data);
			last_buffer.len = 0;
		}
		else
		{
			bpfree(last_buffer.data);
			last_buffer.len = 0;
		}
		last_buffer.data = final_buffer.data;
		last_buffer.len  = final_buffer.len;
	}
	out_buff->data = final_buffer.data;
	out_buff->len  = final_buffer.len;
	return ERR_SUCCESS;
}
BP_ERROR_T BP_JoinMemArray ( P_BP_BUFFER_T mem_array , size_t entry_count , P_BP_BUFFER_T out_buff , P_BP_LINKL tq_in ) { if ( ! mem_array || ! entry_count || ! out_buff ) return ERR_FAILURE ; if ( entry_count < 2 ) return ERR_FAILURE ; BP_BUFFER_T final_buffer ; BP_ZERO_STACK ( final_buffer ) ; BP_BUFFER_T last_buffer ; BP_ZERO_STACK ( last_buffer ) ; BP_BOOL is_first_iter = BP_TRUE ; size_t n = 0 ; for ( ; n < entry_count ; n ++ ) { if ( ! mem_array [ n ] . data || ! mem_array [ n ] . len ) continue ; if ( is_first_iter == BP_TRUE ) { final_buffer . data = BP_JoinMem ( mem_array [ 0 ] . data , mem_array [ 0 ] . len , mem_array [ 1 ] . data , mem_array [ 1 ] . len , tq_in ) ; final_buffer . len = mem_array [ 0 ] . len + mem_array [ 1 ] . len ; if ( ! final_buffer . data || ! final_buffer . len ) return ERR_FAILURE ; last_buffer . data = final_buffer . data ; last_buffer . len = final_buffer . len ; is_first_iter = BP_FALSE ; n ++ ; continue ; } final_buffer . data = BP_JoinMem ( last_buffer . data , last_buffer . len , mem_array [ n ] . data , mem_array [ n ] . len , tq_in ) ; final_buffer . len += mem_array [ n ] . len ; if ( tq_in ) { tq_free ( tq_in , last_buffer . data ) ; last_buffer . len = 0 ; } else { bpfree ( last_buffer . data ) ; last_buffer . len = 0 ; } last_buffer . data = final_buffer . data ; last_buffer . len = final_buffer . len ; } out_buff -> data = final_buffer . data ; out_buff -> len = final_buffer . len ; return ERR_SUCCESS ; }
This routine joins an array of buffers (simple structure containing a pointer and a size), and stores the resulting
joined buffer in the out_buff parameter.
This routine joins an array of buffers ( simple structure containing a pointer and a size ) and stores the resulting joined buffer in the out_buff parameter .
// ensure we have a string array and an entry count // minimum elements is 2 (front and tail) // final buffer for returning to the caller // set initial buffer // notes if we're in the first buffer // walk each entry and join things up // ensure we have data to store // the first iteration requires a few different steps // attempt to join memory // set length // ensure we could create the buffer // store the last buffer as the final buffer, so it can be free'd on loop // set flag indicating we're no longer on the first iteration, and continue // the loop. // join the element into our buffer // increase the join length by the element length // free last_buffer in tailqueue if set, if not, normal free // free and reset length (tailqueue) // free and reset length (natural free) // set data and length // set output members // return indicating success