src/bloblist.c
changeset 98 9e3d28932e7b
child 113 4bee48270834
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/bloblist.c	Wed Mar 11 11:52:14 2015 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <assert.h>
     1.6 +#include <string.h>
     1.7 +
     1.8 +#include "bloblist.h"
     1.9 +
    1.10 +DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    1.11 +        const char *file_name)
    1.12 +{
    1.13 +    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    1.14 +    assert(bloblist);
    1.15 +    if (bloblist == NULL)
    1.16 +        return NULL;
    1.17 +
    1.18 +    if (mime_type) {
    1.19 +        bloblist->mime_type = strdup(mime_type);
    1.20 +        if (bloblist->mime_type == NULL) {
    1.21 +            free(bloblist);
    1.22 +            return NULL;
    1.23 +        }
    1.24 +    }
    1.25 +
    1.26 +    if (file_name) {
    1.27 +        bloblist->file_name = strdup(file_name);
    1.28 +        if (bloblist->file_name == NULL) {
    1.29 +            free(bloblist->mime_type);
    1.30 +            free(bloblist);
    1.31 +            return NULL;
    1.32 +        }
    1.33 +    }
    1.34 +
    1.35 +    bloblist->data = blob;
    1.36 +    bloblist->size = size;
    1.37 +
    1.38 +    return bloblist;
    1.39 +}
    1.40 +
    1.41 +DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
    1.42 +{
    1.43 +    if (bloblist) {
    1.44 +        if (bloblist->next)
    1.45 +            free_bloblist(bloblist->next);
    1.46 +        free(bloblist->data);
    1.47 +        free(bloblist->mime_type);
    1.48 +        free(bloblist->file_name);
    1.49 +        free(bloblist);
    1.50 +    }
    1.51 +}
    1.52 +
    1.53 +DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
    1.54 +{
    1.55 +    bloblist_t *bloblist = NULL;
    1.56 +
    1.57 +    assert(src);
    1.58 +
    1.59 +    bloblist = new_bloblist(src->data, src->size, src->mime_type, src->file_name);
    1.60 +    if (bloblist == NULL)
    1.61 +        goto enomem;
    1.62 +
    1.63 +    if (src->next) {
    1.64 +        bloblist->next = bloblist_dup(src->next);
    1.65 +        if (bloblist->next == NULL)
    1.66 +            goto enomem;
    1.67 +    }
    1.68 +
    1.69 +    return bloblist;
    1.70 +
    1.71 +enomem:
    1.72 +    free_bloblist(bloblist);
    1.73 +    return NULL;
    1.74 +}
    1.75 +
    1.76 +DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
    1.77 +        const char *mime_type, const char *file_name)
    1.78 +{
    1.79 +    assert(blob);
    1.80 +
    1.81 +    if (bloblist == NULL)
    1.82 +        return new_bloblist(blob, size, mime_type, file_name);
    1.83 +
    1.84 +    if (bloblist->data == NULL) {
    1.85 +        if (mime_type) {
    1.86 +            bloblist->mime_type = strdup(mime_type);
    1.87 +            if (bloblist->mime_type == NULL) {
    1.88 +                free(bloblist);
    1.89 +                return NULL;
    1.90 +            }
    1.91 +        }
    1.92 +        if (file_name) {
    1.93 +            bloblist->file_name = strdup(file_name);
    1.94 +            if (bloblist->file_name == NULL) {
    1.95 +                free(bloblist->mime_type);
    1.96 +                free(bloblist);
    1.97 +                return NULL;
    1.98 +            }
    1.99 +        }
   1.100 +        bloblist->data = blob;
   1.101 +        bloblist->size = size;
   1.102 +        return bloblist;
   1.103 +    }
   1.104 +
   1.105 +    if (bloblist->next == NULL) {
   1.106 +        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
   1.107 +        return bloblist->next;
   1.108 +    }
   1.109 +
   1.110 +    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
   1.111 +}
   1.112 +