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