src/identity_list.c
author Volker Birk <vb@pep-project.org>
Thu, 30 Apr 2015 19:01:58 +0200
changeset 230 5b36a2663539
parent 125 5119178815d8
child 274 892d07b825f6
permissions -rw-r--r--
adding _length() functions
     1 #include "pEp_internal.h"
     2 
     3 #include <stdlib.h>
     4 #include <assert.h>
     5 
     6 #include "pEpEngine.h"
     7 #include "identity_list.h"
     8 
     9 DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
    10 {
    11     identity_list *id_list = calloc(1, sizeof(identity_list));
    12     assert(id_list);
    13     if (id_list == NULL)
    14         return NULL;
    15 
    16     id_list->ident = ident;
    17 
    18     return id_list;
    19 }
    20 
    21 DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
    22 {
    23     assert(src);
    24 
    25     identity_list *id_list = new_identity_list(identity_dup(src->ident));
    26     assert(id_list);
    27     if (id_list == NULL)
    28         return NULL;
    29 
    30     if (src->next) {
    31         id_list->next = identity_list_dup(src->next);
    32         if (id_list->next == NULL) {
    33             free_identity_list(id_list);
    34             return NULL;
    35         }
    36     }
    37 
    38     return id_list;
    39 }
    40 
    41 DYNAMIC_API void free_identity_list(identity_list *id_list)
    42 {
    43     if (id_list) {
    44         free_identity_list(id_list->next);
    45         free_identity(id_list->ident);
    46         free(id_list);
    47     }
    48 }
    49 
    50 DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    51 {
    52     assert(ident);
    53 
    54     if (id_list == NULL)
    55         return new_identity_list(ident);
    56 
    57     if (id_list->ident == NULL) {
    58         id_list->ident = ident;
    59         return id_list;
    60     }
    61     else if (id_list->next == NULL) {
    62         id_list->next = new_identity_list(ident);
    63         return id_list->next;
    64     }
    65     else {
    66         return identity_list_add(id_list->next, ident);
    67     }
    68 }
    69 
    70 DYNAMIC_API int identity_list_length(const identity_list *id_list)
    71 {
    72     int len = 0;
    73 
    74     for (; id_list && id_list->ident; id_list = id_list->next)
    75         ++len;
    76 
    77     return len;
    78 }