b0a5c34671fba302eb06ab3dd3cead8abebc49a1
[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   return head;
52 }
53
54 /* does not copy next pointer */
55 TR_APC *tr_apc_dup(TALLOC_CTX *mem_ctx, TR_APC *apc)
56 {
57   TR_APC *new=tr_apc_new(mem_ctx);
58   tr_apc_set_id(new, tr_apc_dup_id(apc));
59   return new;
60 }
61
62 void tr_apc_set_id(TR_APC *apc, TR_NAME *id)
63 {
64   if (apc->id)
65     tr_free_name(apc->id);
66   apc->id=id;
67 }
68
69 TR_NAME *tr_apc_get_id(TR_APC *apc)
70 {
71   return apc->id;
72 }
73
74 TR_NAME *tr_apc_dup_id(TR_APC *apc)
75 {
76   return tr_dup_name(apc->id);;
77 }
78
79
80 char *tr_apc_to_str(TALLOC_CTX *mem_ctx, TR_APC *apc)
81 {
82   return talloc_strndup(mem_ctx, apc->id->buf, apc->id->len);
83 }