src/identity_list.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 04 May 2017 16:57:23 +0200
changeset 1750 33b2fa2368e6
parent 1559 c6506bc6a0df
child 2524 aa720ce48018
permissions -rw-r--r--
ENGINE-206 disable XER content in Sync message body in DEBUG mode. Seems to be triggering unicode transcoding problem in JNI adapter leading to NULL char in message.
     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     pEp_identity *_ident = identity_dup(src->ident);
    31     if (_ident == NULL)
    32         return NULL;
    33 
    34     identity_list *id_list = new_identity_list(_ident);
    35     if (id_list == NULL) {
    36         free_identity(_ident);
    37         return NULL;
    38     }
    39 
    40     identity_list* src_curr = src->next;
    41     identity_list** dst_curr_ptr = &id_list->next;
    42     
    43     while (src_curr) {
    44         _ident = identity_dup(src_curr->ident);
    45         if (_ident == NULL) {
    46             free_identity_list(id_list);
    47             return NULL;
    48         }
    49         
    50         *dst_curr_ptr = new_identity_list(_ident);
    51         if (*dst_curr_ptr == NULL) {
    52             free_identity(_ident);
    53             free_identity_list(id_list);
    54             return NULL;
    55         }
    56         
    57         src_curr = src_curr->next;
    58         dst_curr_ptr = &((*dst_curr_ptr)->next);
    59     }
    60     
    61     return id_list;
    62     
    63 }
    64 
    65 DYNAMIC_API void free_identity_list(identity_list *id_list)
    66 {
    67     identity_list *curr = id_list;
    68     
    69     while (curr) {
    70         identity_list *next = curr->next;
    71         free_identity(curr->ident);
    72         free(curr);
    73         curr = next;
    74     }
    75 }
    76 
    77 DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    78 {
    79     assert(ident);
    80     if (ident == NULL)
    81         return NULL;
    82     
    83     if (id_list == NULL)
    84         return new_identity_list(ident);
    85 
    86     // empty list
    87     if (id_list->ident == NULL) {
    88         if (id_list->next)
    89             return NULL; // invalid list
    90             
    91         id_list->ident = ident;
    92         
    93         if (id_list->ident == NULL)
    94             return NULL;
    95         
    96         return id_list;
    97     }
    98  
    99     identity_list* list_curr = id_list;
   100     while (list_curr->next)
   101         list_curr = list_curr->next;
   102     
   103     list_curr->next = new_identity_list(ident);
   104     
   105     return list_curr->next;
   106 }
   107 
   108 DYNAMIC_API int identity_list_length(const identity_list *id_list)
   109 {
   110     int len = 0;
   111 
   112     for (; id_list && id_list->ident; id_list = id_list->next)
   113         ++len;
   114 
   115     return len;
   116 }