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