ArrayList.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "ArrayList.h"
  2. #define test_object(object) if(object == NULL)return NULL
  3. #define new_string(str) (char*)malloc(strlen(str)+1)
  4. #define new_object(object) (object*)malloc(sizeof(object))
  5. #define reset_object(object,p) memset((void*)p,NULL,sizeof(object))
  6. void *new_byte(int size){
  7. void *mem = malloc(size);
  8. if(mem != NULL){
  9. memset(mem,NULL,size);
  10. return mem;
  11. }
  12. return NULL;
  13. }
  14. /****************************************************************
  15. func: new_array_list
  16. note: 创建一个new_array_list对象,并返回
  17. @head: 集合
  18. @value: value
  19. @size: value的长度
  20. return: 返回创建的 ArrayList
  21. ****************************************************************/
  22. ArrayList *new_array_list(ArrayList *head,void *value,int size){
  23. ArrayList *obj = new_object(ArrayList);
  24. if(obj != NULL){
  25. reset_object(ArrayList,obj);
  26. obj->sValue = size;
  27. obj->value = new_byte(size);
  28. if(obj->value != NULL){
  29. if(value != NULL){
  30. memcpy(obj->value,value,size);
  31. }
  32. return obj;
  33. }
  34. free(obj);
  35. }
  36. return NULL;
  37. }
  38. /****************************************************************
  39. func: add_array_list
  40. note: 添加value
  41. @head: 集合
  42. @value: value
  43. @size: value的长度
  44. return: 返回head
  45. ****************************************************************/
  46. ArrayList *add_array_list(ArrayList *head,void *value,int size){
  47. ArrayList *obj = head;
  48. if(head == NULL){
  49. return new_array_list(NULL,value,size);
  50. }else{
  51. obj = get_array_list_last(head);
  52. obj->next = new_array_list(NULL,value,size);
  53. return head;
  54. }
  55. }
  56. /****************************************************************
  57. func: put_string_map
  58. note: 修改指定的key的值
  59. @head: 集合
  60. @key: key
  61. @value: value
  62. return: 返回head
  63. ****************************************************************/
  64. ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
  65. ArrayList *obj = get_array_list(head,indexof);
  66. if(obj != NULL){
  67. free(obj->value);
  68. obj->sValue = size;
  69. obj->value = new_byte(size);
  70. if(value != NULL){
  71. memcpy(obj->value,value,size);
  72. }
  73. return head;
  74. }
  75. return NULL;
  76. }
  77. /****************************************************************
  78. func: get_array_list
  79. note: 获得indexof位置的对象
  80. @head: 集合
  81. @indexof: 坐标位置
  82. return: 返回获得indexof的对象,如果indexof不存在返回NULL
  83. ****************************************************************/
  84. ArrayList *get_array_list(ArrayList *head,int indexof){
  85. ArrayList *obj = head;
  86. test_object(head);
  87. for(int i=0;obj != NULL;i++){
  88. if(i == indexof){
  89. return obj;
  90. }
  91. obj = obj->next;
  92. }
  93. return NULL;
  94. }
  95. /****************************************************************
  96. func: get_array_list_previous
  97. note: 获得indexof的上一个对象
  98. @head: 集合
  99. @indexof: indexof
  100. return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
  101. ****************************************************************/
  102. ArrayList *get_array_list_previous(ArrayList *head,int indexof){
  103. ArrayList *obj = head;
  104. ArrayList *last = NULL;
  105. test_object(head);
  106. for(int i=0;obj != NULL;i++){
  107. if(i == indexof){
  108. return last;
  109. }
  110. last = obj;
  111. obj = obj->next;
  112. }
  113. return NULL;
  114. }
  115. /****************************************************************
  116. func: get_array_list_last
  117. note: 获得集合最后的对象
  118. @head: 集合
  119. return: 返回获得集合最后的对象
  120. ****************************************************************/
  121. ArrayList *get_array_list_last(ArrayList *head){
  122. ArrayList *obj = head;
  123. test_object(head);
  124. while(obj->next != NULL){
  125. obj = obj->next;
  126. }
  127. return obj;
  128. }
  129. /****************************************************************
  130. func: get_array_list_size
  131. note: 获得集合的对象数量
  132. @head: 集合
  133. return: 返回集合的对象数量
  134. ****************************************************************/
  135. int get_array_list_size(ArrayList *head){
  136. int i = 0;
  137. ArrayList *obj = head;
  138. test_object(head);
  139. for(i=0;obj != NULL;i++){
  140. obj = obj->next;
  141. }
  142. return i;
  143. }
  144. /****************************************************************
  145. func: remove_string_map
  146. note: 移除集合中的对象
  147. @head: 集合
  148. return: 移除集合中的对象后一个StringMap对象
  149. ****************************************************************/
  150. ArrayList *remove_array_list(ArrayList *head){
  151. ArrayList *obj = head->next;
  152. test_object(head);
  153. free(head->value);
  154. free(head);
  155. return obj;
  156. }
  157. /****************************************************************
  158. func: close_array_list
  159. note: 移除所有集合中的对象
  160. @head: 集合
  161. return: NULL
  162. ****************************************************************/
  163. ArrayList *close_array_list(ArrayList *head){
  164. ArrayList *obj = head;
  165. test_object(head);
  166. for(int i=0;obj != NULL;i++){
  167. obj = remove_array_list(obj);
  168. }
  169. return NULL;
  170. }
  171. /****************************************************************
  172. func: remove_array_list_index
  173. note: 移除坐标的对象
  174. @head: 集合
  175. @index: 坐标
  176. return: 返回移除后的集合,如果没有返回NULL
  177. ****************************************************************/
  178. ArrayList *remove_array_list_index(ArrayList *head,int index){
  179. ArrayList *obj = NULL;
  180. ArrayList *previous = NULL;;
  181. test_object(head);
  182. if(index < get_array_list_size(head)){
  183. previous = get_array_list_previous(head,index);
  184. if(previous == NULL || index == 0){
  185. return remove_array_list(head);
  186. }else{
  187. obj = get_array_list(head,index);
  188. previous->next = remove_array_list(obj);
  189. return head;
  190. }
  191. }
  192. return NULL;
  193. }
  194. /****************************************************************
  195. func: get_array_list_memsize
  196. note: 获得集合字符串内存块大小
  197. @head: 集合
  198. return: 字符串的内存块大小
  199. ****************************************************************/
  200. int get_array_list_memsize(ArrayList *head){
  201. int memsize = 0;
  202. ArrayList *obj = head;
  203. for(int i=0;obj != NULL;i++){
  204. memsize += obj->sValue;
  205. obj = obj->next;
  206. }
  207. return memsize;
  208. }
  209. /****************************************************************
  210. func: get_array_list_disksize
  211. note: 获得当前对象占用磁盘空间大小
  212. @head: 集合
  213. return: 占用磁盘空间大小
  214. ****************************************************************/
  215. int get_array_list_disksize(ArrayList *head){
  216. int memsize = get_array_list_memsize(head);
  217. int objsize = get_array_list_size(head) * (sizeof(int)*1);
  218. int sum = sizeof(int) + objsize + memsize;
  219. return sum;
  220. }
  221. uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
  222. if(mBuf == NULL) return NULL;
  223. memcpy(mBuf,&head->sValue,sizeof(int));
  224. mBuf = mBuf + sizeof(int);
  225. memcpy(mBuf,head->value,head->sValue);
  226. mBuf = mBuf + head->sValue;
  227. return mBuf;
  228. }
  229. uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
  230. if(mBuf == NULL) return NULL;
  231. memcpy(&head->sValue,mBuf,sizeof(int));
  232. mBuf = mBuf + sizeof(int);
  233. head->value = new_byte(head->sValue);
  234. if(head->value != NULL ){
  235. memcpy(head->value,mBuf,head->sValue);
  236. mBuf = mBuf + head->sValue;
  237. return mBuf;
  238. }
  239. return NULL;
  240. }
  241. /****************************************************************
  242. func: pack_array_list
  243. note: 打包集合数据,打包方式
  244. int(总的长度) + ( (int)sValue + (data)value ...
  245. @head: 集合
  246. return: 返回打包好的PackMap数据对象,失败返回NULL
  247. ****************************************************************/
  248. PackMap *pack_array_list(ArrayList *head){
  249. PackMap *pack = NULL;
  250. ArrayList *obj = head;
  251. int sum = get_array_list_disksize(head);
  252. uint8_t *buf = new_byte(sum);
  253. uint8_t *mBuf = buf;
  254. if(buf != NULL){
  255. memset(buf,0,sum);
  256. memcpy(mBuf,&sum,sizeof(int));
  257. mBuf = mBuf + sizeof(int);
  258. while(obj != NULL){
  259. mBuf = write_buff_array_list(obj,mBuf);
  260. obj = obj->next;
  261. }
  262. pack = new_object(PackMap);
  263. pack->sum = sum;
  264. pack->buff = buf;
  265. return pack;
  266. }
  267. return NULL;
  268. }
  269. /****************************************************************
  270. func: unpack_array_list
  271. note: 解包ArrayList集合数据
  272. @pack: PackMap对象
  273. return: 返回ArrayList集合,失败返回NULL
  274. ****************************************************************/
  275. ArrayList *unpack_array_list(PackMap *pack){
  276. ArrayList *head = NULL;
  277. ArrayList *obj = NULL;
  278. ArrayList *temp = NULL;
  279. uint8_t *mBuf = NULL;
  280. int sum = 0;
  281. if(pack != NULL){
  282. mBuf = pack->buff;
  283. memcpy(&sum,mBuf,sizeof(int));
  284. mBuf = mBuf + sizeof(int);
  285. head = new_object(ArrayList);
  286. mBuf = read_buff_array_list(head,mBuf);
  287. obj = head;
  288. if(pack->sum == sum){
  289. while((pack->buff + sum) > mBuf && mBuf != NULL){
  290. temp = new_object(ArrayList);
  291. mBuf = read_buff_array_list(temp,mBuf);
  292. obj->next = temp;
  293. obj->next->next = NULL;
  294. obj = obj->next;
  295. }
  296. return head;
  297. }
  298. }
  299. return NULL;
  300. }
  301. #if 0
  302. void test_ArrayList(void){
  303. uint8_t buf[50];
  304. ArrayList *temp;
  305. ArrayList *object = new_array_list(NULL,"0123456789",10);
  306. printf("\rArrayList --> test\r");
  307. add_array_list(object,"A123456\0",8);
  308. add_array_list(object,"B123456\0",8);
  309. add_array_list(object,"C123456\0",8);
  310. add_array_list(object,"D123456\0",8);
  311. #if 1
  312. //string 类型
  313. add_array_list(object,"123",3);
  314. temp = get_array_list(object,1);
  315. char *stp = temp->value;
  316. printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
  317. //int 类型
  318. int in = 1024;
  319. add_array_list(object,&in,sizeof(int));
  320. int *inp = get_array_list(object,2)->value;
  321. printf("int type p:%d\r",*inp);
  322. //double 类型
  323. double dou = 1024.5555;
  324. add_array_list(object,&dou,sizeof(double));
  325. double *pdo = get_array_list(object,3)->value;
  326. printf("double type p:%f\r",*pdo);
  327. #endif
  328. /* for(int i=0;i<1000;i++){
  329. snprintf(buf,50,"Guihui->%d \0",i);
  330. add_array_list(object,buf,strlen(buf));
  331. }*/
  332. PackMap *pack = pack_array_list(object);
  333. if(pack != NULL){
  334. printf("ArrayList PackMap Test\r");
  335. object = close_array_list(object);
  336. for(int i=0; i<pack->sum; i++){
  337. printf("%x ",pack->buff[i]);
  338. }
  339. printf("\r");
  340. /*解包*/
  341. object = unpack_array_list(pack);
  342. }
  343. temp = remove_array_list_index(object,0);
  344. if(temp == NULL){
  345. printf("ERROR remove index\r");
  346. }else{
  347. object = temp;
  348. }
  349. temp = remove_array_list_index(object,3);
  350. if(temp == NULL){
  351. printf("ERROR remove index\r");
  352. }else{
  353. object = temp;
  354. }
  355. //object = remove_array_list_index(object,3);
  356. temp = object;
  357. for(int i=0; temp != NULL; i++){
  358. snprintf((char *)buf,temp->sValue,"%s",(char *)temp->value);
  359. printf("%0.2d -> %s\r",i,buf);
  360. temp = temp->next;
  361. }
  362. close_array_list(object);
  363. }
  364. #endif