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