Example #1
0
/*----------------------------------------------------------------------------*/
int MoveData(ring_buffer *dest_buff, ring_buffer *src_buff, int len)
{
	int to_move, ret;
	u_char* ip;

	// getting length to move
	to_move = GetDataSizeRBuffer(src_buff);

	if (len > 0)
		to_move = MIN(to_move, len);

	to_move = MIN(GetDataSizeRBuffer(src_buff), to_move);
	if(to_move <= 0)
		return 0;

	// copy from src_buffer to dest_buffer
	ip = GetInputPoint(dest_buff);
	memcpy(ip, GetDataPoint(src_buff), to_move);
	AddDataLen(dest_buff, to_move);

	// remove from src_buff
	ret = RemoveDataFromBuffer(src_buff, to_move);
	assert(to_move == ret);
	
	return ret;
}
Example #2
0
/*----------------------------------------------------------------------------*/
int MoveToREPData(ring_buffer *dest_buff, ring_buffer *src_buff, int len)
{
	int to_move, ret, sum = 0;
	u_char* ip;
	
	int data_size = GetDataSizeRBuffer(src_buff);
	int remain_size = GetRemainBufferSize(dest_buff);

	if (len > 0)
		data_size = MIN(data_size, len);

	while (data_size > 0 && remain_size > sizeof(rephdr)) {
		// getting length to move		
		to_move = MIN(data_size, remain_size - sizeof(rephdr));
		to_move = MIN(to_move, MAX_REP_LEN);
		if (to_move <= 0)
			return 0;
		
		remain_size -= sizeof(rephdr);
		remain_size -= to_move;
		data_size -= to_move;

		ip = GetInputPoint(dest_buff);
		
		// create rep header  
		rephdr rep;
		rep.msg_type = 0x01;
		rep.command = 0x00;
		rep.msg_len = to_move;
		memcpy(ip, &rep, sizeof(rephdr));
		ip += sizeof(rephdr);
		AddDataLen(dest_buff, sizeof(rephdr));
		
		// copy from src_buffer to dest_buffer
		memcpy(ip, GetDataPoint(src_buff), to_move);
		AddDataLen(dest_buff, to_move);
		sum += to_move;

		// remove from src_buff
		ret = RemoveDataFromBuffer(src_buff, to_move);
		assert(to_move == ret);
	}
	
	return sum;
}
Example #3
0
/*----------------------------------------------------------------------------*/
int MtcpWriteFromBuffer(mctx_t mctx, int fid, ring_buffer *r_buff)
{
	int to_send, wr, ret;
	to_send = GetDataSizeRBuffer(r_buff);
	if (to_send <= 0)
		return 0;

	wr = mtcp_write(mctx, fid, GetDataPoint(r_buff), to_send);
	if (wr < 0) {
		TRACE_APP("RE_MAIN: Write failed. reason: %d\n", wr);
		return wr;
	}

	ret = RemoveDataFromBuffer(r_buff, wr);
	assert (wr == ret);

	return wr;
}
Example #4
0
/*----------------------------------------------------------------------------*/
int CopyData(ring_buffer *dest_buff, ring_buffer *src_buff, int len) 
{
	int to_cpy;
	u_char* ip;

	// getting length to copy
	to_cpy = GetRemainBufferSize(dest_buff);
	if (to_cpy <= 0)
		return 0;

	if (to_cpy > 0)
		to_cpy = MIN(to_cpy, len);
	to_cpy = MIN(GetDataSizeRBuffer(src_buff), to_cpy);

	// copy from src_buffer to dest_buffer
	ip = GetInputPoint(dest_buff);
	memcpy(ip, GetDataPoint(src_buff), to_cpy);
	AddDataLen(dest_buff, to_cpy);

	return to_cpy;
}
Example #5
0
void
Snap(Graph *graph, int sx, int sy, float *wx, float *wy)
{
    float   x, y;
    float   wx2, wy2;
    int     i;
    int     npoints;
    Plot   *p;
    Plot   *plot;
    FCoord *data;

    /*
     * get the world coordinates of the cursor
     */

    WorldTransform(graph, sx, sy, &x, &y);

    /*
     * get the currently selected plot
     * use the first plot as the default
     */

    plot = graph->plot;

    for (p = graph->plot; p; p = p->next)
    {
        if (p->selected)
        {
            plot = p;
        }
    }

    /*
     * locate the data point with the closest x coord
     * from the currently selected plot
     */

    data = plot->data;
    npoints = plot->npoints;

    /*
     * check the endpoints
     */

    if (npoints > 0)
    {
        /*
         * get the first data point
         */

        GetDataPoint(plot, data, wx, wy);

        /*
         * if there is only one point then just return
         * return with wx, wy set to the data point
         */

        if (npoints < 2)
            return;

        /*
         * otherwise get the last data point
         */

        GetDataPoint(plot, data + npoints - 1, &wx2, &wy2);

        /*
         * is it beyond the most positive data point?
         */

        if (x >= *wx && x >= wx2)
        {
            /*
             * return with wx, wy set to the most positive data point
             */

            if (wx2 > *wx)
            {
                *wx = wx2;
                *wy = wy2;
            }

            return;
        }

        /*
         * is it beyond the most negative data point?
         */

        if (x <= *wx && x <= wx2)
        {
            /*
             * return with wx, wy set to the most positive data point
             */

            if (wx2 < *wx)
            {
                *wx = wx2;
                *wy = wy2;
            }

            return;
        }

        GetDataPoint(plot, data, wx, wy);

        for (i = 1; i < plot->npoints; i++)
        {
            GetDataPoint(plot, data + i, &wx2, &wy2);

            /*
             * is x between this data point and the last?
             */

            if (wx2 >= *wx)
            {
                if (x < wx2 && x >= *wx)
                {
                    return;
                }
            }
            else
            {
                if (x < *wx && x >= wx2)
                {
                    *wx = wx2;
                    *wy = wy2;
                    return;
                }
            }

            *wx = wx2;
            *wy = wy2;
        }
    }
}