#include "bytearray.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "board.h"
#include "mystdlib.h"
#include "debug.h"
#include "rtthread.h"



#define ARR_MAX_PRINT_LEN   50

#define ARRAY_APPEND_SKIP   50



// 使用时保证不要在不同线程同时修改，可以不用保护以提高速度
#define rt_mutex_create(...) 0
#define rt_mutex_delete(...)
#define rt_mutex_take(...)
#define rt_mutex_release(...)





struct _array_def{
  int32_t all;
  int32_t used;
  rt_mutex_t mutex;
  uint8_t data[0];
};





array_def *arr_creat(void)
{
  array_def *a;
  static uint16_t count=0;
	char s1[16]={0};
	sprintf(s1,"arr_mut#%d",count);
  int size=0;
  size+=sizeof(array_def);
  size+=ARRAY_APPEND_SKIP;
  a=malloc(size);
  param_check(a);
  a->all=ARRAY_APPEND_SKIP;
  a->used=0;
  a->mutex=rt_mutex_create(s1,RT_IPC_FLAG_FIFO);
  count++;
  return a;
}



static array_def *arr_expend(array_def *a)
{
  array_def *r;
  int size=0;
  int cpysize=0;
  size+=sizeof(array_def);
  size+=a->all+ARRAY_APPEND_SKIP;
  r=malloc(size);
  param_check(r);
  cpysize=sizeof(array_def)+a->used;
  memcpy(r,a,cpysize);
  r->all+=ARRAY_APPEND_SKIP;
  free(a);
  return r;
}




array_def *_arr_append(array_def **a,uint8_t d)
{
  param_check(a);
  param_check(*a);
  array_def *r=*a;
  rt_mutex_take(r->mutex,RT_WAITING_FOREVER);
  if((*a)->used>=(*a)->all)
  {
    r=arr_expend(*a);
  }
  r->data[r->used]=d;
  r->used++;
  rt_mutex_release(r->mutex);
  *a=r;
  return r;
}


uint8_t arr_get(array_def *a,int index)
{
  uint8_t ret=0;
  param_check(a);
  rt_mutex_take(a->mutex,RT_WAITING_FOREVER);
  if(index<0) index=a->used+index;
  if((index>=0&&index<a->used)==0) ret=0;
  else ret=a->data[index];
  rt_mutex_release(a->mutex);
  return ret;
}


void arr_reset(array_def *a,uint8_t d,int index)
{
  param_check(a);
  rt_mutex_take(a->mutex,RT_WAITING_FOREVER);
  if(index<0) index=a->used+index;
  if((index>=0&&index<a->used)==0){}
  else a->data[index]=d;
  rt_mutex_release(a->mutex);
}


uint8_t *arr_data(array_def *a)
{
  param_check(a);
  return a->data;
}


int arr_length(array_def *a)
{
  param_check(a);
  return a->used;
}


// 截取数组
array_def *arr_mid(array_def *a,int start,int len)
{
  param_check(a);
  array_def *r;
  r=arr_creat();
  if(start<0) start=a->used+start;
  if((start>=0&&start<a->used)==0)
  {
    return r;
  }
  if(start+len>a->used)
  {
    len=a->used-start;
  }
  for(int i=0;i<len;i++)
  {
    //_arr_append(&r,arr_get(a,i+start));
    _arr_append(&r,a->data[i+start]);
  }
  //return tappend(r,0);
  return r;
}

// 移除一些数据,返回实际移除的数据长度
int arr_remove(array_def *a,int start,int len)
{
  param_check(a);
  if(start<0) start=a->used+start;
  if((start>=0&&start<a->used)==0)
  {
    return 0;
  }
  if(start+len>a->used)
  {
    len=a->used-start;
  }
  int move_len=a->used-(start+len);
  memcpy(&a->data[start],&a->data[start+len],move_len);
  a->used-=len;
  return len;
}

// 输出打印字符串
char *arr_string(array_def *a)
{
  param_check(a);
  array_def *d=arr_creat();
  param_check(d);
  // DBG_LOG("d=%08x,a=%08x",d,a);
  // DBG_LOG("%s:length(a)==%d.",__func__,arr_length(a));
  int len=0;
  char s[20];
  int index=0;
  arr_append(d,'[');
  for(int i=0;i<arr_length(a);i++)
  {
    sprintf(s,"%02x",arr_get(a,i));
    // sprintf(s,"%02x",(uint8_t)i);
    len=strlen(s);
    arr_appends(d,s,len);
    arr_append(d,',');
    index++;
    if(index>=ARR_MAX_PRINT_LEN){
      // 超过20字节的 只打印前20字节
      sprintf(s,"...(%d)",arr_length(a));
      len=strlen(s);
      arr_appends(d,s,len);
      break;
    }
  }
  arr_append(d,']');
  len=arr_length(d);
  char *ptr=malloc(len+1);
  param_check(ptr);
  memcpy(ptr,arr_data(d),len);
  arr_delete(d);
  ptr[len]=0;
  return ptr;
}






