Debian Codegraph @

src/GenreTable.cpp

raw
1 /*
2 * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSE.md for more information.
6 */
7
8 #include <algorithm>
9 #include "GenreTable.h"
10 #include "tinyxml.h"
11
12 #include <kodi/General.h>
13 #include <kodi/addon-instance/pvr/EPG.h>
14
15 using namespace std;
16
17 bool CGenreTable::LoadGenreXML(const std::string &filename)
18 {
19 TiXmlDocument xmlDoc;
20 if (!xmlDoc.LoadFile(filename))
21 {
22 kodi::Log(ADDON_LOG_ERROR, "Unable to load %s: %s at line %d", filename.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
23 return false;
24 }
25
26 kodi::Log(ADDON_LOG_INFO, "Opened %s to read genre string to type/subtype translation table", filename.c_str());
27
28 TiXmlHandle hDoc(&xmlDoc);
29 TiXmlElement* pElem;
30 TiXmlHandle hRoot(0);
31 const char* sGenreType = NULL;
32 const char* sGenreSubType = NULL;
33 genre_t genre;
34
35 // block: genrestrings
36 pElem = hDoc.FirstChildElement("genrestrings").Element();
37 // should always have a valid root but handle gracefully if it does
38 if (!pElem)
39 {
40 kodi::Log(ADDON_LOG_ERROR, "Could not find <genrestrings> element");
41 return false;
42 }
43
44 //This should hold: pElem->Value() == "genrestrings"
45
46 // save this for later
47 hRoot=TiXmlHandle(pElem);
48
49 // iterate through all genre elements
50 TiXmlElement* pGenreNode = hRoot.FirstChildElement("genre").Element();
51 //This should hold: pGenreNode->Value() == "genre"
52
53 if (!pGenreNode)
54 {
55 kodi::Log(ADDON_LOG_ERROR, "Could not find <genre> element");
56 return false;
57 }
58
59 for (; pGenreNode != NULL; pGenreNode = pGenreNode->NextSiblingElement("genre"))
60 {
61 const char* sGenreString = pGenreNode->GetText();
62
63 if (sGenreString)
64 {
65 sGenreType = pGenreNode->Attribute("type");
66 sGenreSubType = pGenreNode->Attribute("subtype");
67
68 if ((sGenreType) && (strlen(sGenreType) > 2))
69 {
70 if(sscanf(sGenreType + 2, "%5x", &genre.type) != 1)
71 genre.type = 0;
72 }
73 else
74 {
75 genre.type = 0;
76 }
77
78 if ((sGenreSubType) && (strlen(sGenreSubType) > 2 ))
79 {
80 if(sscanf(sGenreSubType + 2, "%5x", &genre.subtype) != 1)
81 genre.subtype = 0;
82 }
83 else
84 {
85 genre.subtype = 0;
86 }
87
88 if (genre.type > 0)
89 {
90 kodi::Log(ADDON_LOG_DEBUG, "Genre '%s' => 0x%x, 0x%x", sGenreString, genre.type, genre.subtype);
91 m_genremap.insert(std::pair<std::string, genre_t>(sGenreString, genre));
92 }
93 }
94 }
95
96 return true;
97 }
98
99 void CGenreTable::GenreToTypes(string& strGenre, int& genreType, int& genreSubType)
100 {
101 // The xmltv plugin from the MediaPortal TV Server can return genre
102 // strings in local language (depending on the external TV guide source).
103 // The only way to solve this at the XMBC side is to transfer the
104 // genre string to XBMC or to let this plugin (or the TVServerKodi
105 // plugin) translate it into XBMC compatible (numbered) genre types
106 string m_genre = strGenre;
107
108 if(!m_genremap.empty() && !m_genre.empty())
109 {
110 GenreMap::iterator it;
111
112 std::transform(m_genre.begin(), m_genre.end(), m_genre.begin(), ::tolower);
113
114 it = m_genremap.find(m_genre);
115 if (it != m_genremap.end())
116 {
117 genreType = it->second.type;
118 genreSubType = it->second.subtype;
119 }
120 else
121 {
122 kodi::Log(ADDON_LOG_DEBUG, "EPG: No mapping of '%s' to genre type/subtype found.", strGenre.c_str());
123 genreType = EPG_GENRE_USE_STRING;
124 genreSubType = 0;
125 }
126 }
127 else
128 {
129 genreType = 0;
130 genreSubType = 0;
131 }
132 }