#include <stdio.h>
#include "string.h"
#include "stdlib.h"
#include "lib_ringbuf.h"
#define VALID_LEN (Object->push_size - Object->pop_size)    //缓冲区有效长度
int RingBuf_discard(RingBuf Object, int size)   ;

RingBuf RingBuf_Init(RingBufOBJ *data, void *buffer, int buff_size)
{
    RingBuf result = NULL;
    if (data != NULL && buffer != NULL && buff_size > 0)
    {
        data->pHead = buffer;
        data->size = buff_size;
        data->pTail = data->pHead + buff_size;
        data->pValid = data->pHead;
        data->pValidTail = data->pHead;
        data->push_size = 0;
        data->pop_size = 0;
        result = data;
    }
    return result;
}

void RingBuf_Reset(RingBuf Object)
{

    if (Object != NULL)
    {
        Object->pValid = Object->pHead;
        Object->pValidTail = Object->pHead;
        Object->push_size = 0;
        Object->pop_size = 0;
    }
    return ;
}

void RingBuf_Fini(RingBufOBJ *data)
{
    if (data != NULL)
    {
        data->pValid = data->pValidTail = data->pHead;
        data->pop_size = data->push_size;
    }    
}

int RingBuf_Push(RingBuf Object, const void *data, int size)
{
    int result = 0;
    int _ValidLength = Object->push_size - Object->pop_size;
    int _Size = Object->size - _ValidLength;    //实际可用 为0？	
	
		if( _Size <= size )
		{
			RingBuf_discard(Object,size);
		}
		
		if(Object->pop_size >= Object->push_size )
		{
			RingBuf_Reset(Object);
		}
		
    if (data == NULL)
		{
			result = -1;
			return result;
		}		
        
    if (size > 0 && _Size > 0) //保留原本不能丢弃
    {
        if (_Size < size)
            size = _Size;
        result = size;
        if (Object->pValidTail + size > Object->pTail)//写入之后超出缓冲区 分两段
        {
            int rest = Object->pTail - Object->pValidTail;
            memcpy(Object->pValidTail, data, rest);
            memcpy(Object->pHead, (char *)data + rest, size - rest);
            Object->pValidTail = Object->pHead + size - rest;
        }
        else
        {
            memcpy(Object->pValidTail, data, size);
            Object->pValidTail += size;
            if (Object->pValidTail == Object->pTail)
                Object->pValidTail = Object->pHead;
        }
        Object->push_size += result;
    }
    // printf(" push = %s\n", Object->pHead);  
    return result;
}

int RingBuf_Peek(RingBuf Object, void *data, int size)//返回实际尺寸
{
    int result = 0;
    if (data == NULL)
		{
			result = -1;
			return result;
		}
        
    
		if (size > 0 && Object->pop_size != Object->push_size)
    {
        int _ValidLength = Object->push_size - Object->pop_size;
        if (size > _ValidLength)//可能会push VALID_LEN变大 导致size变大越界
            size = _ValidLength;
        if (Object->pValid + size > Object->pTail)//需要分成两段copy
        {
            int rest = Object->pTail - Object->pValid;
            memcpy(data, Object->pValid, rest);
            memcpy((char*)data + rest, Object->pHead, size - rest);//第二段，绕到整个存储区的开头
        }
        else
        {
            memcpy(data, Object->pValid, size);
        }
        result = size;
    }
    return result;
}

int RingBuf_Pop(RingBuf Object, void *data, int size)
{
    int result = 0;
    if (data == NULL)
        result = -1;
    else if (size > 0 && Object->pop_size != Object->push_size)
    {
        int _ValidLength = Object->push_size - Object->pop_size;
        if (size > _ValidLength)//可能会push VALID_LEN变大 导致size变大越界
            size = _ValidLength;
				
				if((Object->pValidTail - size) >= Object->pHead)		
				{
					//数据在缓冲区中间，一次性读取；
					Object->pValidTail = Object->pValidTail - size;
					memcpy(data, Object->pValidTail, size);
					memset(Object->pValidTail, 0x00, size);
				}
				else
				{					
					//数据在缓冲区首尾，需要分两段读取
					Object->pValidTail = Object->pValidTail + Object->size - size;	
					
					int RestLength = Object->pTail - Object->pValidTail;
					memcpy(data, Object->pValidTail, RestLength);
					memset(Object->pValidTail, 0x00, RestLength);
					
					memcpy((char*)data+RestLength, Object->pHead, size - RestLength);	
					memset(Object->pHead, 0x00, size - RestLength);
				}

				result = size;
        Object->pop_size += result;
    }
    // printf("pop = %s\n",(char*)data);   
    return result;
}


int RingBuf_Pop_head(RingBuf Object, void *data, int size)                                                                        
{
    int result = 0;
    if (data == NULL)
        result = -1;
    else if (size > 0 && Object->pop_size != Object->push_size)
    {
        int _ValidLength = Object->push_size - Object->pop_size;
        if (size > _ValidLength)//可能会push VALID_LEN变大 导致size变大越界
            size = _ValidLength;
        if (Object->pValid + size > Object->pTail)//需要分成两段copy
        {
            int rest = Object->pTail - Object->pValid;
            memcpy(data, Object->pValid, rest);
            memcpy((char*)data + rest, Object->pHead, size - rest);//第二段，绕到整个存储区的开头
            Object->pValid = Object->pHead + size - rest;//更新已使用缓冲区的起始
        }
        else
        {
            memcpy(data, Object->pValid, size);
            Object->pValid = Object->pValid + size;
            if (Object->pValid == Object->pTail)
                Object->pValid = Object->pHead;
        }
        result = size;
        Object->pop_size += result;
    }
    // printf("pop = %s\n",(char*)data);   
    return result;
}


int RingBuf_discard(RingBuf Object, int size)                                                                        
{
    int result = 0;

    if (size > 0 && Object->pop_size != Object->push_size)
    {
        int _ValidLength = Object->push_size - Object->pop_size;
        if (size > _ValidLength)//可能会push VALID_LEN变大 导致size变大越界
            size = _ValidLength;
        if (Object->pValid + size > Object->pTail)//需要分成两段copy
        {
            int rest = Object->pTail - Object->pValid;
            memset(Object->pValid, 0x00,  rest);
            memset(Object->pHead, 0x00,  size - rest);//第二段，绕到整个存储区的开头
            Object->pValid = Object->pHead + size - rest;//更新已使用缓冲区的起始
        }
        else
        {
            memset(Object->pValid, 0x00, size);
            Object->pValid = Object->pValid + size;
            if (Object->pValid == Object->pTail)
                Object->pValid = Object->pHead;
        }
        result = size;
        Object->pop_size += result;
    }
    // printf("pop = %s\n",(char*)data);   
    return result;
}
uint32_t RingBuf_obj_num(RingBuf Object,int obj_size)
{
		uint32_t obj_num = 0;
	
		obj_num = 0;
		if(Object->push_size > Object->pop_size)
		{
			obj_num = Object->push_size - Object->pop_size;
			obj_num = obj_num/obj_size;			
		}
		
		return obj_num;
}




