src/bloblist.c
author Volker Birk <vb@pep-project.org>
Wed, 29 Apr 2015 14:51:33 +0200
changeset 229 313d152239bf
parent 125 5119178815d8
child 230 5b36a2663539
permissions -rw-r--r--
fixing preprocessor
     1 #include "pEp_internal.h"
     2 
     3 #include <stdlib.h>
     4 #include <assert.h>
     5 #include <string.h>
     6 
     7 #include "bloblist.h"
     8 
     9 DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    10         const char *filename)
    11 {
    12     bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    13     assert(bloblist);
    14     if (bloblist == NULL)
    15         return NULL;
    16 
    17     if (mime_type) {
    18         bloblist->mime_type = strdup(mime_type);
    19         if (bloblist->mime_type == NULL) {
    20             free(bloblist);
    21             return NULL;
    22         }
    23     }
    24 
    25     if (filename) {
    26         bloblist->filename = strdup(filename);
    27         if (bloblist->filename == NULL) {
    28             free(bloblist->mime_type);
    29             free(bloblist);
    30             return NULL;
    31         }
    32     }
    33 
    34     bloblist->data = blob;
    35     bloblist->size = size;
    36 
    37     return bloblist;
    38 }
    39 
    40 DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
    41 {
    42     if (bloblist) {
    43         if (bloblist->next)
    44             free_bloblist(bloblist->next);
    45         free(bloblist->data);
    46         free(bloblist->mime_type);
    47         free(bloblist->filename);
    48         free(bloblist);
    49     }
    50 }
    51 
    52 DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
    53 {
    54     bloblist_t *bloblist = NULL;
    55 
    56     assert(src);
    57 
    58     bloblist = new_bloblist(src->data, src->size, src->mime_type, src->filename);
    59     if (bloblist == NULL)
    60         goto enomem;
    61 
    62     if (src->next) {
    63         bloblist->next = bloblist_dup(src->next);
    64         if (bloblist->next == NULL)
    65             goto enomem;
    66     }
    67 
    68     return bloblist;
    69 
    70 enomem:
    71     free_bloblist(bloblist);
    72     return NULL;
    73 }
    74 
    75 DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
    76         const char *mime_type, const char *filename)
    77 {
    78     assert(blob);
    79 
    80     if (bloblist == NULL)
    81         return new_bloblist(blob, size, mime_type, filename);
    82 
    83     if (bloblist->data == NULL) {
    84         if (mime_type) {
    85             bloblist->mime_type = strdup(mime_type);
    86             if (bloblist->mime_type == NULL) {
    87                 free(bloblist);
    88                 return NULL;
    89             }
    90         }
    91         if (filename) {
    92             bloblist->filename = strdup(filename);
    93             if (bloblist->filename == NULL) {
    94                 free(bloblist->mime_type);
    95                 free(bloblist);
    96                 return NULL;
    97             }
    98         }
    99         bloblist->data = blob;
   100         bloblist->size = size;
   101         return bloblist;
   102     }
   103 
   104     if (bloblist->next == NULL) {
   105         bloblist->next = new_bloblist(blob, size, mime_type, filename);
   106         return bloblist->next;
   107     }
   108 
   109     return bloblist_add(bloblist->next, blob, size, mime_type, filename);
   110 }
   111