src/identity_list.c
author Volker Birk <vb@pep-project.org>
Fri, 17 May 2019 17:59:06 +0200
branchsync
changeset 3720 9ed76a79d784
parent 2524 aa720ce48018
child 3889 20e17006f286
permissions -rw-r--r--
...
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 
     6 #include <stdlib.h>
     7 #include <assert.h>
     8 
     9 #include "pEpEngine.h"
    10 #include "identity_list.h"
    11 
    12 DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
    13 {
    14     identity_list *id_list = calloc(1, sizeof(identity_list));
    15     assert(id_list);
    16     if (id_list == NULL)
    17         return NULL;
    18 
    19     id_list->ident = ident;
    20 
    21     return id_list;
    22 }
    23 
    24 DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
    25 {
    26     assert(src);
    27     if (src == NULL)
    28         return NULL;
    29 
    30     if (src->ident == NULL)
    31         return new_identity_list(NULL);
    32         
    33     pEp_identity *_ident = identity_dup(src->ident);
    34     if (_ident == NULL)
    35         return NULL;
    36 
    37     identity_list *id_list = new_identity_list(_ident);
    38     if (id_list == NULL) {
    39         free_identity(_ident);
    40         return NULL;
    41     }
    42 
    43     identity_list* src_curr = src->next;
    44     identity_list** dst_curr_ptr = &id_list->next;
    45     
    46     while (src_curr) {
    47         _ident = identity_dup(src_curr->ident);
    48         if (_ident == NULL) {
    49             free_identity_list(id_list);
    50             return NULL;
    51         }
    52         
    53         *dst_curr_ptr = new_identity_list(_ident);
    54         if (*dst_curr_ptr == NULL) {
    55             free_identity(_ident);
    56             free_identity_list(id_list);
    57             return NULL;
    58         }
    59         
    60         src_curr = src_curr->next;
    61         dst_curr_ptr = &((*dst_curr_ptr)->next);
    62     }
    63     
    64     return id_list;
    65     
    66 }
    67 
    68 DYNAMIC_API void free_identity_list(identity_list *id_list)
    69 {
    70     identity_list *curr = id_list;
    71     
    72     while (curr) {
    73         identity_list *next = curr->next;
    74         free_identity(curr->ident);
    75         free(curr);
    76         curr = next;
    77     }
    78 }
    79 
    80 DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    81 {
    82     assert(ident);
    83     if (ident == NULL)
    84         return NULL;
    85     
    86     if (id_list == NULL)
    87         return new_identity_list(ident);
    88 
    89     // empty list
    90     if (id_list->ident == NULL) {
    91         if (id_list->next)
    92             return NULL; // invalid list
    93             
    94         id_list->ident = ident;
    95         
    96         if (id_list->ident == NULL)
    97             return NULL;
    98         
    99         return id_list;
   100     }
   101  
   102     identity_list* list_curr = id_list;
   103     while (list_curr->next)
   104         list_curr = list_curr->next;
   105     
   106     list_curr->next = new_identity_list(ident);
   107     
   108     return list_curr->next;
   109 }
   110 
   111 DYNAMIC_API int identity_list_length(const identity_list *id_list)
   112 {
   113     int len = 0;
   114 
   115     for (; id_list && id_list->ident; id_list = id_list->next)
   116         ++len;
   117 
   118     return len;
   119 }