]> git.sesse.net Git - vlc/blob - src/input/info.h
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / input / info.h
1 /*****************************************************************************
2  * info.h
3  *****************************************************************************
4  * Copyright (C) 2010 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
25 # error This header file can only be included from LibVLC.
26 #endif
27
28 #ifndef _INPUT_INFO_H
29 #define _INPUT_INFO_H 1
30
31 #include "vlc_input_item.h"
32
33 static inline info_t *info_New(const char *name, const char *value )
34 {
35         info_t *info = malloc(sizeof(*info));
36         if (!info)
37                 return NULL;
38
39         info->psz_name = strdup(name);
40         info->psz_value = value ? strdup(value) : NULL;
41         return info;
42 }
43
44 static inline void info_Delete(info_t *i)
45 {
46         free(i->psz_name);
47         free(i->psz_value);
48         free(i);
49 }
50
51 static inline info_category_t *info_category_New(const char *name)
52 {
53         info_category_t *cat = malloc(sizeof(*cat));
54         if (!cat)
55                 return NULL;
56         cat->psz_name = strdup(name);
57         cat->i_infos  = 0;
58         cat->pp_infos = NULL;
59
60         return cat;
61 }
62
63 static inline info_t *info_category_FindInfo(const info_category_t *cat,
64                                              int *index, const char *name)
65 {
66     for (int i = 0; i < cat->i_infos; i++) {
67         if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
68                         if (index)
69                                 *index = i;
70                         return cat->pp_infos[i];
71         }
72     }
73         return NULL;
74 }
75
76 static inline void info_category_ReplaceInfo(info_category_t *cat,
77                                              info_t *info)
78 {
79     int index;
80         info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
81         if (old) {
82         info_Delete(cat->pp_infos[index]);
83         cat->pp_infos[index] = info;
84     } else {
85             INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
86     }
87 }
88
89 static inline info_t *info_category_VaAddInfo(info_category_t *cat,
90                                               const char *name,
91                                               const char *format, va_list args)
92 {
93         info_t *info = info_category_FindInfo(cat, NULL, name);
94         if (!info) {
95                 info = info_New(name, NULL);
96         if (!info)
97             return NULL;
98             INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
99     } else {
100         free(info->psz_value);
101     }
102     if (vasprintf(&info->psz_value, format, args) == -1)
103         info->psz_value = NULL;
104     return info;
105 }
106
107 static inline info_t *info_category_AddInfo(info_category_t *cat,
108                                             const char *name,
109                                             const char *format, ...)
110 {
111     va_list args;
112
113     va_start(args, format);
114     info_t *info = info_category_VaAddInfo(cat, name, format, args);
115     va_end(args);
116
117     return info;
118 }
119
120 static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
121 {
122         int index;
123         if (info_category_FindInfo(cat, &index, name)) {
124                 info_Delete(cat->pp_infos[index]);
125                 REMOVE_ELEM(cat->pp_infos, cat->i_infos, index);
126                 return VLC_SUCCESS;
127         }
128         return VLC_EGENERIC;
129 }
130
131 static inline void info_category_Delete(info_category_t *cat)
132 {
133         for (int i = 0; i < cat->i_infos; i++)
134                 info_Delete(cat->pp_infos[i]);
135         free(cat->pp_infos);
136         free(cat->psz_name);
137         free(cat);
138 }
139
140 #endif