info.h 3.93 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * info.h
 *****************************************************************************
 * Copyright (C) 2010 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25
#ifndef LIBVLC_INPUT_INFO_H
#define LIBVLC_INPUT_INFO_H 1
26 27 28 29 30

#include "vlc_input_item.h"

static inline info_t *info_New(const char *name, const char *value )
{
31 32 33
    info_t *info = malloc(sizeof(*info));
    if (!info)
        return NULL;
34

35 36 37
    info->psz_name = strdup(name);
    info->psz_value = value ? strdup(value) : NULL;
    return info;
38 39 40 41
}

static inline void info_Delete(info_t *i)
{
42 43 44
    free(i->psz_name);
    free(i->psz_value);
    free(i);
45 46 47 48
}

static inline info_category_t *info_category_New(const char *name)
{
49 50 51 52 53 54 55 56
    info_category_t *cat = malloc(sizeof(*cat));
    if (!cat)
        return NULL;
    cat->psz_name = strdup(name);
    cat->i_infos  = 0;
    cat->pp_infos = NULL;

    return cat;
57 58 59 60 61 62 63
}

static inline info_t *info_category_FindInfo(const info_category_t *cat,
                                             int *index, const char *name)
{
    for (int i = 0; i < cat->i_infos; i++) {
        if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
64 65 66
            if (index)
                *index = i;
            return cat->pp_infos[i];
67 68
        }
    }
69
    return NULL;
70 71
}

72 73
static inline void info_category_ReplaceInfo(info_category_t *cat,
                                             info_t *info)
74
{
75
    int index;
76 77
    info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
    if (old) {
78 79
        info_Delete(cat->pp_infos[index]);
        cat->pp_infos[index] = info;
80 81
    } else
        TAB_APPEND(cat->i_infos, cat->pp_infos, info);
82
}
83

84 85 86 87
static inline info_t *info_category_VaAddInfo(info_category_t *cat,
                                              const char *name,
                                              const char *format, va_list args)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88 89 90
    info_t *info = info_category_FindInfo(cat, NULL, name);
    if (!info) {
        info = info_New(name, NULL);
91 92
        if (!info)
            return NULL;
93
        TAB_APPEND(cat->i_infos, cat->pp_infos, info);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94
    } else
95 96 97 98 99 100
        free(info->psz_value);
    if (vasprintf(&info->psz_value, format, args) == -1)
        info->psz_value = NULL;
    return info;
}

101 102 103 104 105 106 107 108 109 110 111 112 113
static inline info_t *info_category_AddInfo(info_category_t *cat,
                                            const char *name,
                                            const char *format, ...)
{
    va_list args;

    va_start(args, format);
    info_t *info = info_category_VaAddInfo(cat, name, format, args);
    va_end(args);

    return info;
}

114 115
static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
{
116 117 118
    int index;
    if (info_category_FindInfo(cat, &index, name)) {
        info_Delete(cat->pp_infos[index]);
119
        TAB_ERASE(cat->i_infos, cat->pp_infos, index);
120 121 122
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
123 124 125 126
}

static inline void info_category_Delete(info_category_t *cat)
{
127 128 129 130 131
    for (int i = 0; i < cat->i_infos; i++)
        info_Delete(cat->pp_infos[i]);
    free(cat->pp_infos);
    free(cat->psz_name);
    free(cat);
132 133 134
}

#endif