src/identity_list.c
author Krista Bennett <krista@pep-project.org>
Tue, 01 Aug 2017 16:40:24 +0200
branchmessage-2.0
changeset 1979 3d0a778d035a
parent 1559 c6506bc6a0df
child 2524 aa720ce48018
permissions -rw-r--r--
Changed name to ENGINE-214 for JIRA tracking - please use that branch instead.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     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
vb@274
    30
    pEp_identity *_ident = identity_dup(src->ident);
vb@274
    31
    if (_ident == NULL)
vb@274
    32
        return NULL;
vb@274
    33
vb@274
    34
    identity_list *id_list = new_identity_list(_ident);
krista@914
    35
    if (id_list == NULL) {
krista@914
    36
        free_identity(_ident);
vb@98
    37
        return NULL;
krista@914
    38
    }
vb@98
    39
krista@893
    40
    identity_list* src_curr = src->next;
krista@892
    41
    identity_list** dst_curr_ptr = &id_list->next;
krista@892
    42
    
krista@892
    43
    while (src_curr) {
krista@892
    44
        _ident = identity_dup(src_curr->ident);
krista@894
    45
        if (_ident == NULL) {
krista@894
    46
            free_identity_list(id_list);
krista@894
    47
            return NULL;
krista@894
    48
        }
krista@894
    49
        
krista@892
    50
        *dst_curr_ptr = new_identity_list(_ident);
krista@894
    51
        if (*dst_curr_ptr == NULL) {
krista@894
    52
            free_identity(_ident);
krista@894
    53
            free_identity_list(id_list);
krista@894
    54
            return NULL;
krista@894
    55
        }
krista@894
    56
        
krista@892
    57
        src_curr = src_curr->next;
krista@892
    58
        dst_curr_ptr = &((*dst_curr_ptr)->next);
vb@98
    59
    }
krista@892
    60
    
vb@98
    61
    return id_list;
krista@892
    62
    
vb@98
    63
}
vb@98
    64
vb@98
    65
DYNAMIC_API void free_identity_list(identity_list *id_list)
vb@98
    66
{
roker@1559
    67
    identity_list *curr = id_list;
krista@893
    68
    
krista@893
    69
    while (curr) {
roker@1559
    70
        identity_list *next = curr->next;
krista@893
    71
        free_identity(curr->ident);
krista@893
    72
        free(curr);
krista@893
    73
        curr = next;
vb@98
    74
    }
vb@98
    75
}
vb@98
    76
vb@98
    77
DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
vb@98
    78
{
vb@98
    79
    assert(ident);
krista@849
    80
    if (ident == NULL)
krista@849
    81
        return NULL;
krista@849
    82
    
vb@98
    83
    if (id_list == NULL)
vb@98
    84
        return new_identity_list(ident);
vb@98
    85
krista@891
    86
    // empty list
vb@98
    87
    if (id_list->ident == NULL) {
krista@891
    88
        if (id_list->next)
krista@891
    89
            return NULL; // invalid list
krista@891
    90
            
vb@98
    91
        id_list->ident = ident;
krista@891
    92
        
krista@891
    93
        if (id_list->ident == NULL)
krista@891
    94
            return NULL;
krista@891
    95
        
vb@98
    96
        return id_list;
vb@98
    97
    }
krista@891
    98
 
krista@891
    99
    identity_list* list_curr = id_list;
krista@891
   100
    while (list_curr->next)
krista@891
   101
        list_curr = list_curr->next;
krista@891
   102
    
krista@891
   103
    list_curr->next = new_identity_list(ident);
krista@891
   104
    
krista@891
   105
    return list_curr->next;
vb@98
   106
}
vb@98
   107
vb@230
   108
DYNAMIC_API int identity_list_length(const identity_list *id_list)
vb@230
   109
{
vb@230
   110
    int len = 0;
vb@230
   111
vb@230
   112
    for (; id_list && id_list->ident; id_list = id_list->next)
vb@230
   113
        ++len;
vb@230
   114
vb@230
   115
    return len;
vb@230
   116
}