VLC  3.0.15
info.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * info.h
3  *****************************************************************************
4  * Copyright (C) 2010 Laurent Aimar
5  * $Id: 2752c5bad43f10dbb2aed7c248c86a10aa71358a $
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 
24 #ifndef LIBVLC_INPUT_INFO_H
25 #define LIBVLC_INPUT_INFO_H 1
26 
27 #include "vlc_input_item.h"
28 
29 static inline info_t *info_New(const char *name, const char *value )
30 {
31  info_t *info = malloc(sizeof(*info));
32  if (!info)
33  return NULL;
34 
35  info->psz_name = strdup(name);
36  info->psz_value = value ? strdup(value) : NULL;
37  return info;
38 }
39 
40 static inline void info_Delete(info_t *i)
41 {
42  free(i->psz_name);
43  free(i->psz_value);
44  free(i);
45 }
46 
47 static inline info_category_t *info_category_New(const char *name)
48 {
49  info_category_t *cat = malloc(sizeof(*cat));
50  if (!cat)
51  return NULL;
52  cat->psz_name = strdup(name);
53  cat->i_infos = 0;
54  cat->pp_infos = NULL;
55 
56  return cat;
57 }
58 
59 static inline info_t *info_category_FindInfo(const info_category_t *cat,
60  int *index, const char *name)
61 {
62  for (int i = 0; i < cat->i_infos; i++) {
63  if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
64  if (index)
65  *index = i;
66  return cat->pp_infos[i];
67  }
68  }
69  return NULL;
70 }
71 
72 static inline void info_category_ReplaceInfo(info_category_t *cat,
73  info_t *info)
74 {
75  int index;
76  info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
77  if (old) {
78  info_Delete(cat->pp_infos[index]);
79  cat->pp_infos[index] = info;
80  } else
81  TAB_APPEND(cat->i_infos, cat->pp_infos, info);
82 }
83 
85  const char *name,
86  const char *format, va_list args)
87 {
88  info_t *info = info_category_FindInfo(cat, NULL, name);
89  if (!info) {
90  info = info_New(name, NULL);
91  if (!info)
92  return NULL;
93  TAB_APPEND(cat->i_infos, cat->pp_infos, info);
94  } else
95  free(info->psz_value);
96  if (vasprintf(&info->psz_value, format, args) == -1)
97  info->psz_value = NULL;
98  return info;
99 }
100 
101 static inline info_t *info_category_AddInfo(info_category_t *cat,
102  const char *name,
103  const char *format, ...)
104 {
105  va_list args;
106 
107  va_start(args, format);
108  info_t *info = info_category_VaAddInfo(cat, name, format, args);
109  va_end(args);
110 
111  return info;
112 }
113 
114 static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
115 {
116  int index;
117  if (info_category_FindInfo(cat, &index, name)) {
118  info_Delete(cat->pp_infos[index]);
119  TAB_ERASE(cat->i_infos, cat->pp_infos, index);
120  return VLC_SUCCESS;
121  }
122  return VLC_EGENERIC;
123 }
124 
125 static inline void info_category_Delete(info_category_t *cat)
126 {
127  for (int i = 0; i < cat->i_infos; i++)
128  info_Delete(cat->pp_infos[i]);
129  free(cat->pp_infos);
130  free(cat->psz_name);
131  free(cat);
132 }
133 
134 #endif
info_New
static info_t * info_New(const char *name, const char *value)
Definition: info.h:28
vlc_input_item.h
info_category_t::psz_name
char * psz_name
Name of this category.
Definition: vlc_input_item.h:50
info_category_t
Definition: vlc_input_item.h:48
info_category_DeleteInfo
static int info_category_DeleteInfo(info_category_t *cat, const char *name)
Definition: info.h:113
info_category_AddInfo
static info_t * info_category_AddInfo(info_category_t *cat, const char *name, const char *format,...)
Definition: info.h:100
VLC_EGENERIC
#define VLC_EGENERIC
Unspecified error.
Definition: vlc_common.h:350
info_category_New
static info_category_t * info_category_New(const char *name)
Definition: info.h:46
TAB_ERASE
#define TAB_ERASE(count, tab, index)
Definition: vlc_arrays.h:77
info_category_Delete
static void info_category_Delete(info_category_t *cat)
Definition: info.h:124
TAB_APPEND
#define TAB_APPEND(count, tab, p)
Definition: vlc_arrays.h:64
info_category_t::pp_infos
struct info_t ** pp_infos
Pointer to an array of infos.
Definition: vlc_input_item.h:52
info_t::psz_name
char * psz_name
Name of this info.
Definition: vlc_input_item.h:44
VLC_SUCCESS
#define VLC_SUCCESS
No error.
Definition: vlc_common.h:349
strdup
char * strdup(const char *)
info_category_VaAddInfo
static info_t * info_category_VaAddInfo(info_category_t *cat, const char *name, const char *format, va_list args)
Definition: info.h:83
name
const char name[16]
Definition: httpd.c:1249
info_category_FindInfo
static info_t * info_category_FindInfo(const info_category_t *cat, int *index, const char *name)
Definition: info.h:58
info_t
Definition: vlc_input_item.h:42
info_Delete
static void info_Delete(info_t *i)
Definition: info.h:39
info_category_ReplaceInfo
static void info_category_ReplaceInfo(info_category_t *cat, info_t *info)
Definition: info.h:71
info_category_t::i_infos
int i_infos
Number of infos in the category.
Definition: vlc_input_item.h:51
vasprintf
int vasprintf(char **, const char *, va_list)
info_t::psz_value
char * psz_value
Value of the info.
Definition: vlc_input_item.h:45