info.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * info.h
00003  *****************************************************************************
00004  * Copyright (C) 2010 Laurent Aimar
00005  * $Id: 2beaafe87d27f0b0a546eeda3c2683d600b55b9a $
00006  *
00007  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
00025 # error This header file can only be included from LibVLC.
00026 #endif
00027 
00028 #ifndef _INPUT_INFO_H
00029 #define _INPUT_INFO_H 1
00030 
00031 #include "vlc_input_item.h"
00032 
00033 static inline info_t *info_New(const char *name, const char *value )
00034 {
00035     info_t *info = malloc(sizeof(*info));
00036     if (!info)
00037         return NULL;
00038 
00039     info->psz_name = strdup(name);
00040     info->psz_value = value ? strdup(value) : NULL;
00041     return info;
00042 }
00043 
00044 static inline void info_Delete(info_t *i)
00045 {
00046     free(i->psz_name);
00047     free(i->psz_value);
00048     free(i);
00049 }
00050 
00051 static inline info_category_t *info_category_New(const char *name)
00052 {
00053     info_category_t *cat = malloc(sizeof(*cat));
00054     if (!cat)
00055         return NULL;
00056     cat->psz_name = strdup(name);
00057     cat->i_infos  = 0;
00058     cat->pp_infos = NULL;
00059 
00060     return cat;
00061 }
00062 
00063 static inline info_t *info_category_FindInfo(const info_category_t *cat,
00064                                              int *index, const char *name)
00065 {
00066     for (int i = 0; i < cat->i_infos; i++) {
00067         if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
00068             if (index)
00069                 *index = i;
00070             return cat->pp_infos[i];
00071         }
00072     }
00073     return NULL;
00074 }
00075 
00076 static inline void info_category_ReplaceInfo(info_category_t *cat,
00077                                              info_t *info)
00078 {
00079     int index;
00080     info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
00081     if (old) {
00082         info_Delete(cat->pp_infos[index]);
00083         cat->pp_infos[index] = info;
00084     } else {
00085         INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
00086     }
00087 }
00088 
00089 static inline info_t *info_category_VaAddInfo(info_category_t *cat,
00090                                               const char *name,
00091                                               const char *format, va_list args)
00092 {
00093     info_t *info = info_category_FindInfo(cat, NULL, name);
00094     if (!info) {
00095         info = info_New(name, NULL);
00096         if (!info)
00097             return NULL;
00098         INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
00099     } else {
00100         free(info->psz_value);
00101     }
00102     if (vasprintf(&info->psz_value, format, args) == -1)
00103         info->psz_value = NULL;
00104     return info;
00105 }
00106 
00107 static inline info_t *info_category_AddInfo(info_category_t *cat,
00108                                             const char *name,
00109                                             const char *format, ...)
00110 {
00111     va_list args;
00112 
00113     va_start(args, format);
00114     info_t *info = info_category_VaAddInfo(cat, name, format, args);
00115     va_end(args);
00116 
00117     return info;
00118 }
00119 
00120 static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
00121 {
00122     int index;
00123     if (info_category_FindInfo(cat, &index, name)) {
00124         info_Delete(cat->pp_infos[index]);
00125         REMOVE_ELEM(cat->pp_infos, cat->i_infos, index);
00126         return VLC_SUCCESS;
00127     }
00128     return VLC_EGENERIC;
00129 }
00130 
00131 static inline void info_category_Delete(info_category_t *cat)
00132 {
00133     for (int i = 0; i < cat->i_infos; i++)
00134         info_Delete(cat->pp_infos[i]);
00135     free(cat->pp_infos);
00136     free(cat->psz_name);
00137     free(cat);
00138 }
00139 
00140 #endif

Generated on Tue May 25 08:05:01 2010 for VLC by  doxygen 1.5.6