summaryrefslogtreecommitdiff
path: root/lib/item.c
blob: e6ccc7ac61a88585006b8b85ff9bb856052bebf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "internal.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>

/**
 * Allocate a new item.
 *
 * @param text Pointer to null terminated C "string", can be **NULL** for empty text.
 * @return bmItem for new item instance, **NULL** if creation failed.
 */
bmItem* bmItemNew(const char *text)
{
    bmItem *item = calloc(1, sizeof(bmItem));

    if (!item)
        return NULL;

    bmItemSetText(item, text);
    return item;
}

/**
 * Release bmItem instance.
 *
 * @param item bmItem instance to be freed from memory.
 */
void bmItemFree(bmItem *item)
{
    assert(item);

    if (item->text)
        free(item->text);

    free(item);
}

/**
 * Set text to bmItem instance.
 *
 * @param item bmItem instance where to set text.
 * @param text C "string" to set as text, can be **NULL** for empty text.
 */
int bmItemSetText(bmItem *item, const char *text)
{
    assert(item);

    char *copy = NULL;
    if (text && !(copy = _bmStrdup(text)))
        return 0;

    if (item->text)
        free(item->text);

    item->text = copy;
    return 1;
}

/**
 * Get text from bmItem instance.
 *
 * @param item bmItem instance where to get text from.
 * @return Pointer to null terminated C "string", can be **NULL** for empty text.
 */
const char* bmItemGetText(const bmItem *item)
{
    assert(item);
    return item->text;
}

/* vim: set ts=8 sw=4 tw=0 :*/