#include "ArrayList.h" #define test_object(object) if(object == NULL)return NULL #define new_string(str) (char*)malloc(strlen(str)+1) #define new_object(object) (object*)malloc(sizeof(object)) #define reset_object(object,p) memset((void*)p,NULL,sizeof(object)) void *new_byte(int size){ void *mem = malloc(size); if(mem != NULL){ memset(mem,NULL,size); return mem; } return NULL; } /**************************************************************** func: new_array_list note: 创建一个new_array_list对象,并返回 @head: 集合 @value: value @size: value的长度 return: 返回创建的 ArrayList ****************************************************************/ ArrayList *new_array_list(ArrayList *head,void *value,int size){ ArrayList *obj = new_object(ArrayList); if(obj != NULL){ reset_object(ArrayList,obj); obj->sValue = size; obj->value = new_byte(size); if(obj->value != NULL){ if(value != NULL){ memcpy(obj->value,value,size); } return obj; } free(obj); } return NULL; } /**************************************************************** func: add_array_list note: 添加value @head: 集合 @value: value @size: value的长度 return: 返回head ****************************************************************/ ArrayList *add_array_list(ArrayList *head,void *value,int size){ ArrayList *obj = head; if(head == NULL){ return new_array_list(NULL,value,size); }else{ obj = get_array_list_last(head); obj->next = new_array_list(NULL,value,size); return head; } } /**************************************************************** func: put_string_map note: 修改指定的key的值 @head: 集合 @key: key @value: value return: 返回head ****************************************************************/ ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){ ArrayList *obj = get_array_list(head,indexof); if(obj != NULL){ free(obj->value); obj->sValue = size; obj->value = new_byte(size); if(value != NULL){ memcpy(obj->value,value,size); } return head; } return NULL; } /**************************************************************** func: get_array_list note: 获得indexof位置的对象 @head: 集合 @indexof: 坐标位置 return: 返回获得indexof的对象,如果indexof不存在返回NULL ****************************************************************/ ArrayList *get_array_list(ArrayList *head,int indexof){ ArrayList *obj = head; test_object(head); for(int i=0;obj != NULL;i++){ if(i == indexof){ return obj; } obj = obj->next; } return NULL; } /**************************************************************** func: get_array_list_previous note: 获得indexof的上一个对象 @head: 集合 @indexof: indexof return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL ****************************************************************/ ArrayList *get_array_list_previous(ArrayList *head,int indexof){ ArrayList *obj = head; ArrayList *last = NULL; test_object(head); for(int i=0;obj != NULL;i++){ if(i == indexof){ return last; } last = obj; obj = obj->next; } return NULL; } /**************************************************************** func: get_array_list_last note: 获得集合最后的对象 @head: 集合 return: 返回获得集合最后的对象 ****************************************************************/ ArrayList *get_array_list_last(ArrayList *head){ ArrayList *obj = head; test_object(head); while(obj->next != NULL){ obj = obj->next; } return obj; } /**************************************************************** func: get_array_list_size note: 获得集合的对象数量 @head: 集合 return: 返回集合的对象数量 ****************************************************************/ int get_array_list_size(ArrayList *head){ int i = 0; ArrayList *obj = head; test_object(head); for(i=0;obj != NULL;i++){ obj = obj->next; } return i; } /**************************************************************** func: remove_string_map note: 移除集合中的对象 @head: 集合 return: 移除集合中的对象后一个StringMap对象 ****************************************************************/ ArrayList *remove_array_list(ArrayList *head){ ArrayList *obj = head->next; test_object(head); free(head->value); free(head); return obj; } /**************************************************************** func: close_array_list note: 移除所有集合中的对象 @head: 集合 return: NULL ****************************************************************/ ArrayList *close_array_list(ArrayList *head){ ArrayList *obj = head; test_object(head); for(int i=0;obj != NULL;i++){ obj = remove_array_list(obj); } return NULL; } /**************************************************************** func: remove_array_list_index note: 移除坐标的对象 @head: 集合 @index: 坐标 return: 返回移除后的集合,如果没有返回NULL ****************************************************************/ ArrayList *remove_array_list_index(ArrayList *head,int index){ ArrayList *obj = NULL; ArrayList *previous = NULL;; test_object(head); if(index < get_array_list_size(head)){ previous = get_array_list_previous(head,index); if(previous == NULL || index == 0){ return remove_array_list(head); }else{ obj = get_array_list(head,index); previous->next = remove_array_list(obj); return head; } } return NULL; } /**************************************************************** func: get_array_list_memsize note: 获得集合字符串内存块大小 @head: 集合 return: 字符串的内存块大小 ****************************************************************/ int get_array_list_memsize(ArrayList *head){ int memsize = 0; ArrayList *obj = head; for(int i=0;obj != NULL;i++){ memsize += obj->sValue; obj = obj->next; } return memsize; } /**************************************************************** func: get_array_list_disksize note: 获得当前对象占用磁盘空间大小 @head: 集合 return: 占用磁盘空间大小 ****************************************************************/ int get_array_list_disksize(ArrayList *head){ int memsize = get_array_list_memsize(head); int objsize = get_array_list_size(head) * (sizeof(int)*1); int sum = sizeof(int) + objsize + memsize; return sum; } uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){ if(mBuf == NULL) return NULL; memcpy(mBuf,&head->sValue,sizeof(int)); mBuf = mBuf + sizeof(int); memcpy(mBuf,head->value,head->sValue); mBuf = mBuf + head->sValue; return mBuf; } uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){ if(mBuf == NULL) return NULL; memcpy(&head->sValue,mBuf,sizeof(int)); mBuf = mBuf + sizeof(int); head->value = new_byte(head->sValue); if(head->value != NULL ){ memcpy(head->value,mBuf,head->sValue); mBuf = mBuf + head->sValue; return mBuf; } return NULL; } /**************************************************************** func: pack_array_list note: 打包集合数据,打包方式 int(总的长度) + ( (int)sValue + (data)value ... @head: 集合 return: 返回打包好的PackMap数据对象,失败返回NULL ****************************************************************/ PackMap *pack_array_list(ArrayList *head){ PackMap *pack = NULL; ArrayList *obj = head; int sum = get_array_list_disksize(head); uint8_t *buf = new_byte(sum); uint8_t *mBuf = buf; if(buf != NULL){ memset(buf,0,sum); memcpy(mBuf,&sum,sizeof(int)); mBuf = mBuf + sizeof(int); while(obj != NULL){ mBuf = write_buff_array_list(obj,mBuf); obj = obj->next; } pack = new_object(PackMap); pack->sum = sum; pack->buff = buf; return pack; } return NULL; } /**************************************************************** func: unpack_array_list note: 解包ArrayList集合数据 @pack: PackMap对象 return: 返回ArrayList集合,失败返回NULL ****************************************************************/ ArrayList *unpack_array_list(PackMap *pack){ ArrayList *head = NULL; ArrayList *obj = NULL; ArrayList *temp = NULL; uint8_t *mBuf = NULL; int sum = 0; if(pack != NULL){ mBuf = pack->buff; memcpy(&sum,mBuf,sizeof(int)); mBuf = mBuf + sizeof(int); head = new_object(ArrayList); mBuf = read_buff_array_list(head,mBuf); obj = head; if(pack->sum == sum){ while((pack->buff + sum) > mBuf && mBuf != NULL){ temp = new_object(ArrayList); mBuf = read_buff_array_list(temp,mBuf); obj->next = temp; obj->next->next = NULL; obj = obj->next; } return head; } } return NULL; } #if 0 void test_ArrayList(void){ uint8_t buf[50]; ArrayList *temp; ArrayList *object = new_array_list(NULL,"0123456789",10); printf("\rArrayList --> test\r"); add_array_list(object,"A123456\0",8); add_array_list(object,"B123456\0",8); add_array_list(object,"C123456\0",8); add_array_list(object,"D123456\0",8); #if 1 //string 类型 add_array_list(object,"123",3); temp = get_array_list(object,1); char *stp = temp->value; printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]); //int 类型 int in = 1024; add_array_list(object,&in,sizeof(int)); int *inp = get_array_list(object,2)->value; printf("int type p:%d\r",*inp); //double 类型 double dou = 1024.5555; add_array_list(object,&dou,sizeof(double)); double *pdo = get_array_list(object,3)->value; printf("double type p:%f\r",*pdo); #endif /* for(int i=0;i<1000;i++){ snprintf(buf,50,"Guihui->%d \0",i); add_array_list(object,buf,strlen(buf)); }*/ PackMap *pack = pack_array_list(object); if(pack != NULL){ printf("ArrayList PackMap Test\r"); object = close_array_list(object); for(int i=0; isum; i++){ printf("%x ",pack->buff[i]); } printf("\r"); /*解包*/ object = unpack_array_list(pack); } temp = remove_array_list_index(object,0); if(temp == NULL){ printf("ERROR remove index\r"); }else{ object = temp; } temp = remove_array_list_index(object,3); if(temp == NULL){ printf("ERROR remove index\r"); }else{ object = temp; } //object = remove_array_list_index(object,3); temp = object; for(int i=0; temp != NULL; i++){ snprintf((char *)buf,temp->sValue,"%s",(char *)temp->value); printf("%0.2d -> %s\r",i,buf); temp = temp->next; } close_array_list(object); } #endif