src/identity_list.c
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 914 08a0252d82c3
child 1513 e7f7e42385b5
permissions -rw-r--r--
merge "default" into my branch
vb@125
     1
#include "pEp_internal.h"
vb@125
     2
vb@98
     3
#include <stdlib.h>
vb@98
     4
#include <assert.h>
vb@98
     5
vb@98
     6
#include "pEpEngine.h"
vb@98
     7
#include "identity_list.h"
vb@98
     8
vb@98
     9
DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
vb@98
    10
{
vb@98
    11
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@98
    12
    assert(id_list);
vb@98
    13
    if (id_list == NULL)
vb@98
    14
        return NULL;
vb@98
    15
vb@98
    16
    id_list->ident = ident;
vb@98
    17
vb@98
    18
    return id_list;
vb@98
    19
}
vb@98
    20
vb@98
    21
DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
vb@98
    22
{
vb@98
    23
    assert(src);
krista@849
    24
    if (src == NULL)
krista@849
    25
        return NULL;
vb@98
    26
vb@274
    27
    pEp_identity *_ident = identity_dup(src->ident);
vb@274
    28
    if (_ident == NULL)
vb@274
    29
        return NULL;
vb@274
    30
vb@274
    31
    identity_list *id_list = new_identity_list(_ident);
krista@914
    32
    if (id_list == NULL) {
krista@914
    33
        free_identity(_ident);
vb@98
    34
        return NULL;
krista@914
    35
    }
vb@98
    36
krista@893
    37
    identity_list* src_curr = src->next;
krista@892
    38
    identity_list** dst_curr_ptr = &id_list->next;
krista@892
    39
    
krista@892
    40
    while (src_curr) {
krista@892
    41
        _ident = identity_dup(src_curr->ident);
krista@894
    42
        if (_ident == NULL) {
krista@894
    43
            free_identity_list(id_list);
krista@894
    44
            return NULL;
krista@894
    45
        }
krista@894
    46
        
krista@892
    47
        *dst_curr_ptr = new_identity_list(_ident);
krista@894
    48
        if (*dst_curr_ptr == NULL) {
krista@894
    49
            free_identity(_ident);
krista@894
    50
            free_identity_list(id_list);
krista@894
    51
            return NULL;
krista@894
    52
        }
krista@894
    53
        
krista@892
    54
        src_curr = src_curr->next;
krista@892
    55
        dst_curr_ptr = &((*dst_curr_ptr)->next);
vb@98
    56
    }
krista@892
    57
    
vb@98
    58
    return id_list;
krista@892
    59
    
vb@98
    60
}
vb@98
    61
vb@98
    62
DYNAMIC_API void free_identity_list(identity_list *id_list)
vb@98
    63
{
krista@893
    64
    identity_list *curr;
krista@893
    65
    identity_list *next;
krista@893
    66
    
krista@893
    67
    curr = id_list;
krista@893
    68
    
krista@893
    69
    while (curr) {
krista@893
    70
        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
}