Ticket #11525: program-category.patch

File program-category.patch, 3.1 KB (added by rwscott@…, 7 years ago)

Go one step farther to prevent mistakes such as invalid values being stored and used

  • mythtv/libs/libmyth/programinfo.cpp

    commit 1f06b62d1edd48c61d5d5636cd75b9d80ea967e3
    Author: Rick Scott <rwscott@users.sourceforge.net>
    Date:   Sun Apr 14 13:27:47 2013 -0400
    
        Go one step farther to prevent mistakes such as invalid values being stored and used.
        
        The original patch, bff155970773, changed the category type from a string
        to an enum to help avoid mistakes in the future. It however meant that a
        couple of string arrays, and some static int's (added later to fix the fix),
         _must_ be kept in sync. This just changes the type of future mistakes.
        This defines the category type in one place only.
    
    diff --git a/mythtv/libs/libmyth/programinfo.cpp b/mythtv/libs/libmyth/programinfo.cpp
    index 968a85c..d834bb6 100644
    a b static void set_flag(uint32_t &flags, int flag_to_set, bool is_set) 
    8383
    8484QString myth_category_type_to_string(ProgramInfo::CategoryType category_type)
    8585{
    86     static int NUM_CAT_TYPES = 5;
    87     static const char *cattype[] =
    88         { "", "movie", "series", "sports", "tvshow", };
     86#define CATEGORY_TYPE_ITEM(name, ...) \
     87    case ProgramInfo::kCategory##name: return(QString(#name).toLower()); break;
    8988
    90     if ((category_type > ProgramInfo::kCategoryNone) &&
    91         ((int)category_type < NUM_CAT_TYPES))
    92         return QString(cattype[category_type]);
    93 
    94     return "";
     89    switch (category_type)
     90    {
     91    CATEGORY_TYPE_LIST
     92    default:
     93        return(QString(""));
     94        break;
     95    }
     96#undef CATEGORY_TYPE_ITEM
    9597}
    9698
    9799ProgramInfo::CategoryType string_to_myth_category_type(const QString &category_type)
    98100{
    99     static int NUM_CAT_TYPES = 5;
    100     static const char *cattype[] =
    101         { "", "movie", "series", "sports", "tvshow", };
     101#define CATEGORY_TYPE_ITEM(name, ...) \
     102    if (category_type == QString(#name).toLower()) \
     103        return(ProgramInfo::kCategory##name); \
     104    else
    102105
    103     for (uint i = 1; i < NUM_CAT_TYPES; i++)
    104         if (category_type == cattype[i])
    105             return (ProgramInfo::CategoryType) i;
    106     return ProgramInfo::kCategoryNone;
     106    CATEGORY_TYPE_LIST
     107    {
     108        return(ProgramInfo::kCategoryNone);
     109    }
     110#undef CATEGORY_TYPE_ITEM
    107111}
    108112
    109113/** \fn ProgramInfo::ProgramInfo(void)
  • mythtv/libs/libmyth/programinfo.h

    diff --git a/mythtv/libs/libmyth/programinfo.h b/mythtv/libs/libmyth/programinfo.h
    index 6e8e263..cb1d827 100644
    a b class MSqlQuery; 
    6969class ProgramInfoUpdater;
    7070class PMapDBReplacement;
    7171
     72#define CATEGORY_TYPE_LIST \
     73    CATEGORY_TYPE_ITEM(None) \
     74    CATEGORY_TYPE_ITEM(Movie) \
     75    CATEGORY_TYPE_ITEM(Series) \
     76    CATEGORY_TYPE_ITEM(Sports) \
     77    CATEGORY_TYPE_ITEM(TVShow)
     78
    7279class MPUBLIC ProgramInfo
    7380{
    7481    friend int pginfo_init_statics(void);
    7582  public:
    76     enum CategoryType { kCategoryNone, kCategoryMovie, kCategorySeries,
    77                         kCategorySports, kCategoryTVShow };
     83#define CATEGORY_TYPE_ITEM(name, ...) kCategory##name,
     84    enum CategoryType {
     85        CATEGORY_TYPE_LIST
     86    };
     87#undef CATEGORY_TYPE_ITEM
    7888                       
    7989    /// Null constructor
    8090    ProgramInfo(void);