library/src/utility/BP-StringUtilities.cc
// joins two pieces of memory in a newly allocated buffer.
joins two pieces of memory in a newly allocated buffer.
joins two pieces of memory in a newly allocated buffer .
void * BP_JoinMem(void *mem_front, size_t front_len, void *mem_tail, size_t tail_len, P_BP_LINKL tq_in)
{
	if(!mem_front || !mem_tail || !front_len || !tail_len)
		return NULL;
	P_BP_LINKL tq = BP_CreateLinkL(".", BP_FALSE);
	if(!tq)
		return NULL;
	void *front_part = tq_calloc(tq, front_len, 1, BPLN);
	void *tail_part  = tq_calloc(tq, tail_len, 1, BPLN);
	if(!front_part || !tail_part)
	{
		BP_DestroyLinkL(tq, BP_TRUE);
		return NULL;
	}
	memcpy(front_part, mem_front, front_len);
	memcpy(tail_part,  mem_tail,  tail_len);
	if(BP_LinkLFlattenRaw(tq) != ERR_SUCCESS)
	{
		BP_DestroyLinkL(tq, BP_TRUE);
		return NULL;
	}
	void * final_buffer = NULL;
	if(tq->flat_buff)
	{
		final_buffer = bpcalloc(front_len+tail_len, 1);
		if(!final_buffer)
		{
			BP_DestroyLinkL(tq, BP_TRUE);
			return NULL;
		}
		memcpy(final_buffer, tq->flat_buff, front_len+tail_len);
	}
	BP_DestroyLinkL(tq, BP_TRUE);
	if(tq_in)
	{
		tq_link(tq_in, final_buffer, front_len + tail_len, BPLN);
	}
	return final_buffer;
}
void * BP_JoinMem ( void * mem_front , size_t front_len , void * mem_tail , size_t tail_len , P_BP_LINKL tq_in ) { if ( ! mem_front || ! mem_tail || ! front_len || ! tail_len ) return NULL ; P_BP_LINKL tq = BP_CreateLinkL ( " " , BP_FALSE ) ; if ( ! tq ) return NULL ; void * front_part = tq_calloc ( tq , front_len , 1 , BPLN ) ; void * tail_part = tq_calloc ( tq , tail_len , 1 , BPLN ) ; if ( ! front_part || ! tail_part ) { BP_DestroyLinkL ( tq , BP_TRUE ) ; return NULL ; } memcpy ( front_part , mem_front , front_len ) ; memcpy ( tail_part , mem_tail , tail_len ) ; if ( BP_LinkLFlattenRaw ( tq ) != ERR_SUCCESS ) { BP_DestroyLinkL ( tq , BP_TRUE ) ; return NULL ; } void * final_buffer = NULL ; if ( tq -> flat_buff ) { final_buffer = bpcalloc ( front_len + tail_len , 1 ) ; if ( ! final_buffer ) { BP_DestroyLinkL ( tq , BP_TRUE ) ; return NULL ; } memcpy ( final_buffer , tq -> flat_buff , front_len + tail_len ) ; } BP_DestroyLinkL ( tq , BP_TRUE ) ; if ( tq_in ) { tq_link ( tq_in , final_buffer , front_len + tail_len , BPLN ) ; } return final_buffer ; }
joins two pieces of memory in a newly allocated buffer.
joins two pieces of memory in a newly allocated buffer .
// ensure all join parts are available // create linkl or exit if we cant // attempt to allocate pointers // ensure both were created ok // now copy in data // flatten the tq // this is the final buffer which gets returned // set final buffer if we've flattened things ok // allocate space for the final buffer // if it couldn't be created, exit here // finally copy in the joined data // destroy the linkl and return with the final generated buffer // check if we have an output tailueue // link the final buffer if we have a tailqueue to link it in // return the pointer