Debian Codegraph @

src/categories.cpp

raw
1 /*
2 * Copyright (C) 2005-2012 Team XBMC
3 * http://www.xbmc.org
4 *
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 */
22 // Code taken from pvr.zattoo
23
24 #include "categories.h"
25
26 #include <cstring>
27 #include <iostream>
28 #include <regex>
29
30 #include <kodi/Filesystem.h>
31
32 #define CATEGORIES_MAXLINESIZE 255
33
34 #if defined(_WIN32) || defined(_WIN64)
35 /* We are on Windows */
36 #define strtok_r strtok_s
37 #define strdup _strdup
38 #endif
39
40 Categories::Categories() : m_categoriesById()
41 {
42 char* saveptr;
43 LoadEITCategories();
44 // Copy over
45 CategoryByIdMap::const_iterator it;
46 for (it = m_categoriesById.begin(); it != m_categoriesById.end(); ++it)
47 {
48 m_categoriesByName[it->second] = it->first;
49 if (it->second.find("/") != std::string::npos)
50 {
51 char* categories = strdup(it->second.c_str());
52 char* p = strtok_r(categories, "/", &saveptr);
53 while (p != nullptr)
54 {
55 std::string category = p;
56 m_categoriesByName[category] = it->first;
57 p = strtok_r(nullptr, "/", &saveptr);
58 }
59 free(categories);
60 }
61 }
62 }
63
64 std::string Categories::Category(int category) const
65 {
66 auto it = m_categoriesById.find(category);
67 if (it != m_categoriesById.end())
68 return it->second;
69 return "";
70 }
71
72 int Categories::Category(const std::string& category)
73 {
74 if (category.empty())
75 {
76 return 0;
77 }
78 auto it = m_categoriesByName.find(category);
79 if (it != m_categoriesByName.end())
80 return it->second;
81 kodi::Log(ADDON_LOG_INFO, "Missing category: %s", category.c_str());
82 m_categoriesByName[category] = 0;
83 return 0;
84 }
85
86 void Categories::LoadEITCategories()
87 {
88 const char* filePath = "special://home/addons/pvr.waipu/resources/eit_categories.txt";
89 if (!kodi::vfs::FileExists(filePath, false))
90 {
91 filePath = "special://xbmc/addons/pvr.waipu/resources/eit_categories.txt";
92 }
93
94 if (kodi::vfs::FileExists(filePath, false))
95 {
96 kodi::Log(ADDON_LOG_DEBUG, "%s: Loading EIT categories from file '%s'", __FUNCTION__, filePath);
97 kodi::vfs::CFile file;
98 if (!file.OpenFile(filePath, 0))
99 {
100 kodi::Log(ADDON_LOG_ERROR, "%s: File '%s' failed to open", __FUNCTION__, filePath);
101 return;
102 }
103
104 std::string line;
105 std::regex rgx("^ *(0x.*)*; *\"(.*)\"");
106 while (file.ReadLine(line))
107 {
108 std::smatch matches;
109 if (std::regex_search(line, matches, rgx) && matches.size() == 3)
110 {
111 int catId = std::stoi(matches[1].str(), nullptr, 16);
112 std::string name = matches[2].str();
113
114 m_categoriesById.insert(std::pair<int, std::string>(catId, name));
115 kodi::Log(ADDON_LOG_DEBUG, "%s: Add name [%s] for category %.2X", __FUNCTION__,
116 name.c_str(), catId);
117 }
118 }
119 }
120 else
121 {
122 kodi::Log(ADDON_LOG_INFO, "%s: File '%s' not found", __FUNCTION__, filePath);
123 }
124 }