ppa stats graph
[ppastats.git] / src / list.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "list.h"
24
25 int list_length(void **list)
26 {
27         int n;
28
29         n = 0;
30         while (list && *list) {
31                 list++;
32                 n++;
33         }
34
35         return n;
36 }
37
38 void **list_add(void **list, void *new_item)
39 {
40         int n;
41         void **new_list;
42
43         n = list_length(list);
44
45         new_list = malloc(sizeof(void *)*(n+2));
46
47         if (n) {
48                 memcpy(new_list, list, sizeof(void *)*n);
49                 free(list);
50         }
51
52         new_list[n] = new_item;
53         new_list[n+1] = NULL;
54
55         return new_list;
56 }
57
58 void **list_add_list(void **list1, void **list2)
59 {
60         int n1, n2, n;
61         void **list;
62
63         n1 = list_length(list1);
64         n2 = list_length(list2);
65
66         n = n1 + n2 + 1;
67
68         list = malloc(sizeof(void *)*(n+1));
69
70         memcpy(list, list1, n1*sizeof(void *));
71         memcpy(list+n1, list2, n2*sizeof(void *));
72
73         list[n1+n2] = NULL;
74
75         free(list1);
76
77         return list;
78 }
79