a2cabed6e382eae99b663e1426683c735e5c7c15
[trust_router.git] / common / tr_apc.c
1 #include <talloc.h>
2
3 #include <trust_router/tr_name.h>
4 #include <tr_apc.h>
5
6 static int tr_apc_destructor(void *obj)
7 {
8   TR_APC *apc=talloc_get_type_abort(obj, TR_APC);
9   if (apc->id!=NULL)
10     tr_free_name(apc->id);
11   return 0;
12 }
13
14 TR_APC *tr_apc_new(TALLOC_CTX *mem_ctx)
15 {
16   TR_APC *apc=talloc(mem_ctx, TR_APC);
17   if (apc!=NULL) {
18     apc->id=NULL;
19     apc->next=NULL;
20     talloc_set_destructor((void *)apc, tr_apc_destructor);
21   }
22   return apc;
23 }
24
25 void tr_apc_free(TR_APC *apc)
26 {
27   talloc_free(apc);
28 }
29
30 static TR_APC *tr_apc_tail(TR_APC *apc)
31 {
32   if (apc==NULL)
33     return NULL;
34
35   while (apc->next!=NULL)
36     apc=apc->next;
37   return apc;
38 }
39
40 TR_APC *tr_apc_add(TR_APC *head, TR_APC *new)
41 {
42   if (head==NULL)
43     head=new;
44   else {
45     tr_apc_tail(head)->next=new;
46     while (new!=NULL) {
47       talloc_steal(head, new);
48       new=new->next;
49     }
50   }
51 }
52
53 /* does not copy next pointer */
54 TR_APC *tr_apc_dup(TALLOC_CTX *mem_ctx, TR_APC *apc)
55 {
56   TR_APC *new=tr_apc_new(mem_ctx);
57   tr_apc_set_id(new, tr_apc_dup_id(apc));
58   return new;
59 }
60
61 void tr_apc_set_id(TR_APC *apc, TR_NAME *id)
62 {
63   if (apc->id)
64     tr_free_name(apc->id);
65   apc->id=id;
66 }
67
68 TR_NAME *tr_apc_get_id(TR_APC *apc)
69 {
70   return apc->id;
71 }
72
73 TR_NAME *tr_apc_dup_id(TR_APC *apc)
74 {
75   return tr_dup_name(apc->id);;
76 }