forked from vdr-projects/vdr-plugin-tvscraper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchannelmap.c
185 lines (146 loc) · 4.32 KB
/
channelmap.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <fstream>
#include <string.h>
void removeChars(std::string& str, const char* ignore);
//***************************************************************************
// Load Channelmap
//***************************************************************************
int loadChannelmap(vector<sChannelMapEpg> &channelMap, const vector<std::shared_ptr<iExtEpg>> &extEpgs) {
std::ifstream cmfile;
std::string s;
size_t p;
int line = 0;
int count = 0;
int status = 1;
enum ConfIndex
{
ciSource,
ciExtid,
ciMerge,
ciVps,
ciCount
};
CONCATENATE(path, cPlugin::ConfigDirectory(PLUGIN_NAME_I18N), "/channelmap.conf");
cmfile.open(path);
if (cmfile.fail())
{
esyslog("tvscraper, '%s' does not exist, external EPG disabled (see README.md). Message: '%s'", path, strerror(errno));
return 0;
}
esyslog("tvscraper, loading '%s'", path);
while (!cmfile.eof())
{
char* left = 0;
char* right = 0;
char* extid = 0;
char* source = 0;
char* pc;
int index;
int vps = 0;
int merge = 1;
getline(cmfile, s);
line++;
removeChars(s, " \f\n\r\t\v");
// remove comments
p = s.find_first_of("//");
if (p != string::npos)
s.erase(p);
if (s.empty())
continue;
// split line at '='
p = s.find_first_of("=");
if ((p == string::npos) || !s.substr(p+1).length())
{
esyslog("tvscraper: ERROR parsing '%s' at line %d!", path, line);
status = 0;
break;
}
left = strdup(s.substr(0, p).c_str());
right = strdup(s.substr(p+1).c_str());
for (index = 0, pc = strtok(left, ":"); pc; pc = strtok(0, ":"), index++)
{
switch (index)
{
case ciSource: source = strdup(pc); break;
case ciExtid: extid = strdup(pc); break;
case ciMerge: merge = atoi(pc); break;
case ciVps: vps = strchr("1yY", *pc) != 0; break;
}
}
free(left);
if (!right || !source || !extid)
{
esyslog("tvscraper: ERROR: Syntax error in '%s' at line %d!", path, line);
free(right);
free(source);
free(extid);
status = 0;
break;
}
sChannelMapEpg channelMapEpg;
// find plugin that can handle source
for (auto &extEpg: extEpgs) {
if (!extEpg->getSource() ) continue;
if (strcmp(extEpg->getSource(), source) != 0) continue;
channelMapEpg.extEpg = extEpg;
break;
}
if (!channelMapEpg.extEpg) {
esyslog("tvscraper: ERROR parsing '%s' at line %d, no plugin for source %s found", path, line, source);
} else {
channelMapEpg.extid = extid; // channel ID of external EPG provider
channelMapEpg.source = source; // id of external EPG provider
channelMapEpg.merge = merge;
channelMapEpg.vps = vps;
// read channels separated by commas
for (index = 0, pc = strtok(right, ","); pc; pc = strtok(0, ","), index++)
{
channelMapEpg.channelID = tChannelID::FromString(pc); // VDR channel ID
if (!channelMapEpg.channelID.Valid() ) {
esyslog("tvscraper: ERROR parsing '%s' at line %d, channel %s not valid", path, line, pc);
} else {
channelMap.push_back(channelMapEpg);
count++;
}
} // for read channels separated by commas
} // plugin for source is available
free(right);
free(source);
free(extid);
}
cmfile.close();
std::sort(channelMap.begin(), channelMap.end());
esyslog("tvscraper: %d channel mappings read.", count);
return status;
}
void removeChars(std::string& str, const char* ignore)
{
const char* s = str.c_str();
int lenSrc = str.length();
int lenIgn = strlen(ignore);
char* dest = (char*)malloc(lenSrc+1); *dest = 0;
char* d = dest;
int csSrc; // size of character
int csIgn; //
for (int ps = 0; ps < lenSrc; ps += csSrc)
{
int skip = 0;
csSrc = std::max(mblen(&s[ps], lenSrc-ps), 1);
for (int pi = 0; pi < lenIgn; pi += csIgn)
{
csIgn = std::max(mblen(&ignore[pi], lenIgn-pi), 1);
if (csSrc == csIgn && strncmp(&s[ps], &ignore[pi], csSrc) == 0)
{
skip = 1;
break;
}
}
if (!skip)
{
for (int i = 0; i < csSrc; i++)
*d++ = s[ps+i];
}
}
*d = 0;
str = dest;
free(dest);
}