info.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * info.h
00003  *****************************************************************************
00004  * Copyright (C) 2010 Laurent Aimar
00005  * $Id: 9dcc4ca8f94f4b6a4761c128df2f40d7d7a3c4ef $
00006  *
00007  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef LIBVLC_INPUT_INFO_H
00025 #define LIBVLC_INPUT_INFO_H 1
00026 
00027 #include "vlc_input_item.h"
00028 
00029 static inline info_t *info_New(const char *name, const char *value )
00030 {
00031     info_t *info = malloc(sizeof(*info));
00032     if (!info)
00033         return NULL;
00034 
00035     info->psz_name = strdup(name);
00036     info->psz_value = value ? strdup(value) : NULL;
00037     return info;
00038 }
00039 
00040 static inline void info_Delete(info_t *i)
00041 {
00042     free(i->psz_name);
00043     free(i->psz_value);
00044     free(i);
00045 }
00046 
00047 static inline info_category_t *info_category_New(const char *name)
00048 {
00049     info_category_t *cat = malloc(sizeof(*cat));
00050     if (!cat)
00051         return NULL;
00052     cat->psz_name = strdup(name);
00053     cat->i_infos  = 0;
00054     cat->pp_infos = NULL;
00055 
00056     return cat;
00057 }
00058 
00059 static inline info_t *info_category_FindInfo(const info_category_t *cat,
00060                                              int *index, const char *name)
00061 {
00062     for (int i = 0; i < cat->i_infos; i++) {
00063         if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
00064             if (index)
00065                 *index = i;
00066             return cat->pp_infos[i];
00067         }
00068     }
00069     return NULL;
00070 }
00071 
00072 static inline void info_category_ReplaceInfo(info_category_t *cat,
00073                                              info_t *info)
00074 {
00075     int index;
00076     info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
00077     if (old) {
00078         info_Delete(cat->pp_infos[index]);
00079         cat->pp_infos[index] = info;
00080     } else {
00081         INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
00082     }
00083 }
00084 
00085 static inline info_t *info_category_VaAddInfo(info_category_t *cat,
00086                                               const char *name,
00087                                               const char *format, va_list args)
00088 {
00089     info_t *info = info_category_FindInfo(cat, NULL, name);
00090     if (!info) {
00091         info = info_New(name, NULL);
00092         if (!info)
00093             return NULL;
00094         INSERT_ELEM(cat->pp_infos, cat->i_infos, cat->i_infos, info);
00095     } else
00096         free(info->psz_value);
00097     if (vasprintf(&info->psz_value, format, args) == -1)
00098         info->psz_value = NULL;
00099     return info;
00100 }
00101 
00102 static inline info_t *info_category_AddInfo(info_category_t *cat,
00103                                             const char *name,
00104                                             const char *format, ...)
00105 {
00106     va_list args;
00107 
00108     va_start(args, format);
00109     info_t *info = info_category_VaAddInfo(cat, name, format, args);
00110     va_end(args);
00111 
00112     return info;
00113 }
00114 
00115 static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
00116 {
00117     int index;
00118     if (info_category_FindInfo(cat, &index, name)) {
00119         info_Delete(cat->pp_infos[index]);
00120         REMOVE_ELEM(cat->pp_infos, cat->i_infos, index);
00121         return VLC_SUCCESS;
00122     }
00123     return VLC_EGENERIC;
00124 }
00125 
00126 static inline void info_category_Delete(info_category_t *cat)
00127 {
00128     for (int i = 0; i < cat->i_infos; i++)
00129         info_Delete(cat->pp_infos[i]);
00130     free(cat->pp_infos);
00131     free(cat->psz_name);
00132     free(cat);
00133 }
00134 
00135 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines