src/labeled_int_list.c
author Volker Birk <vb@pep.foundation>
Fri, 05 Oct 2018 15:19:11 +0200
branchsync
changeset 3019 220ebfbe592c
parent 3004 079d0bf653be
child 4792 7056435ab9e7
permissions -rw-r--r--
... ladies and gentlemen, a line ending!
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdbool.h>
     5 #include <stdlib.h>
     6 #include <assert.h>
     7 #include <string.h>
     8 
     9 #include "platform.h"
    10 #include "labeled_int_list.h"
    11 
    12 DYNAMIC_API labeled_int_list_t *new_labeled_int_list(int value, const char* label)
    13 {
    14     assert(label);
    15     if (!label)
    16         return NULL;
    17         
    18     labeled_int_list_t * labeled_int_list = calloc(1, sizeof(labeled_int_list_t));
    19     assert(labeled_int_list);
    20     if (labeled_int_list == NULL)
    21         return NULL;
    22 
    23     labeled_int_list->value = value;
    24     labeled_int_list->label = strdup(label);
    25     if (!labeled_int_list->label) {
    26         free(labeled_int_list);
    27         labeled_int_list = NULL;
    28     }
    29     return labeled_int_list;
    30 }
    31 
    32 DYNAMIC_API void free_labeled_int_list(labeled_int_list_t *labeled_int_list)
    33 {
    34     labeled_int_list_t *curr = labeled_int_list;
    35 
    36     while (curr) {
    37         labeled_int_list_t *next = curr->next;
    38         free(curr->label);
    39         free(curr);
    40         curr = next;
    41     }
    42 }
    43 
    44 DYNAMIC_API labeled_int_list_t *labeled_int_list_dup(const labeled_int_list_t *src)
    45 {
    46     assert(src);
    47     if (src == NULL)
    48         return NULL;
    49     
    50     labeled_int_list_t *labeled_int_list = NULL;
    51 
    52     labeled_int_list = new_labeled_int_list(src->value, src->label);
    53     if (labeled_int_list == NULL)
    54         goto enomem;
    55 
    56     labeled_int_list_t* src_curr = src->next;
    57     labeled_int_list_t** dst_curr_ptr = &labeled_int_list->next;
    58 
    59     // list
    60     while (src_curr) {
    61         *dst_curr_ptr = new_labeled_int_list(src_curr->value, src_curr->label);
    62         if (*dst_curr_ptr == NULL)
    63             goto enomem;
    64 
    65         src_curr = src_curr->next;
    66         dst_curr_ptr = &((*dst_curr_ptr)->next);
    67     }
    68 
    69     return labeled_int_list;
    70 
    71 enomem:
    72     free_labeled_int_list(labeled_int_list);
    73     return NULL;
    74 }
    75 
    76 DYNAMIC_API labeled_int_list_t *labeled_int_list_add(labeled_int_list_t *labeled_int_list, int value, const char* label)
    77 {
    78     if (!label)
    79         return NULL;
    80         
    81     if (!labeled_int_list)
    82         return new_labeled_int_list(value, label);
    83 
    84     if (!labeled_int_list->label) { // empty list
    85         assert(!labeled_int_list->next);
    86         if (labeled_int_list->next)
    87             return NULL; // invalid list
    88 
    89         labeled_int_list->value = value;
    90         labeled_int_list->label = strdup(label);
    91         if (!labeled_int_list->label) {
    92             free(labeled_int_list);
    93             labeled_int_list = NULL;
    94         }
    95 
    96         return labeled_int_list;
    97     }
    98 
    99     labeled_int_list_t* list_curr = labeled_int_list;
   100 
   101     while (list_curr->next)
   102         list_curr = list_curr->next;
   103 
   104     list_curr->next = new_labeled_int_list(value, label);
   105 
   106     assert(list_curr->next);
   107     if (!list_curr->next)
   108         return NULL;
   109 
   110     return list_curr->next;
   111 }
   112 
   113 DYNAMIC_API int labeled_int_list_length(const labeled_int_list_t *labeled_int_list)
   114 {
   115     int len = 0;
   116 
   117     for (const labeled_int_list_t *_li = labeled_int_list; _li && _li->label; _li = _li->next)
   118         len++;
   119 
   120     return len;
   121 }
   122