-
Notifications
You must be signed in to change notification settings - Fork 7
/
epg.c
1626 lines (1486 loc) · 47.6 KB
/
epg.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* epg.c: Electronic Program Guide
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Original version (as used in VDR before 1.3.0) written by
* Robert Schneider <[email protected]> and Rolf Hakenes <[email protected]>.
*
* $Id: epg.c 5.11 2024/09/26 19:25:41 kls Exp $
*/
#include "epg.h"
#include <ctype.h>
#include <limits.h>
#include <time.h>
#include "libsi/si.h"
#define RUNNINGSTATUSTIMEOUT 30 // seconds before the running status is considered unknown
#define EPGDATAWRITEDELTA 600 // seconds between writing the epg.data file
// --- tComponent ------------------------------------------------------------
cString tComponent::ToString(void)
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%X %02X %s %s", stream, type, language, description ? description : "");
return buffer;
}
bool tComponent::FromString(const char *s)
{
unsigned int Stream, Type;
int n = sscanf(s, "%X %02X %7s %m[^\n]", &Stream, &Type, language, &description); // 7 = MAXLANGCODE2 - 1
if (n != 4 || isempty(description)) {
free(description);
description = NULL;
}
stream = Stream;
type = Type;
return n >= 3;
}
// --- cComponents -----------------------------------------------------------
cComponents::cComponents(void)
{
numComponents = 0;
components = NULL;
}
cComponents::~cComponents(void)
{
for (int i = 0; i < numComponents; i++)
free(components[i].description);
free(components);
}
bool cComponents::Realloc(int Index)
{
if (Index >= numComponents) {
Index++;
if (tComponent *NewBuffer = (tComponent *)realloc(components, Index * sizeof(tComponent))) {
int n = numComponents;
numComponents = Index;
components = NewBuffer;
memset(&components[n], 0, sizeof(tComponent) * (numComponents - n));
}
else {
esyslog("ERROR: out of memory");
return false;
}
}
return true;
}
void cComponents::SetComponent(int Index, const char *s)
{
if (Realloc(Index))
components[Index].FromString(s);
}
void cComponents::SetComponent(int Index, uchar Stream, uchar Type, const char *Language, const char *Description)
{
if (!Realloc(Index))
return;
tComponent *p = &components[Index];
p->stream = Stream;
p->type = Type;
strn0cpy(p->language, Language, sizeof(p->language));
char *q = strchr(p->language, ',');
if (q)
*q = 0; // strips rest of "normalized" language codes
p->description = strcpyrealloc(p->description, !isempty(Description) ? Description : NULL);
}
tComponent *cComponents::GetComponent(int Index, uchar Stream, uchar Type)
{
for (int i = 0; i < numComponents; i++) {
if (components[i].stream == Stream && (
Type == 0 || // don't care about the actual Type
Stream == 2 && (components[i].type < 5) == (Type < 5) // fallback "Dolby" component according to the "Premiere pseudo standard"
)) {
if (!Index--)
return &components[i];
}
}
return NULL;
}
// --- cEvent ----------------------------------------------------------------
cMutex cEvent::numTimersMutex;
cEvent::cEvent(tEventID EventID)
{
schedule = NULL;
numTimers = 0;
eventID = EventID;
tableID = 0xFF; // actual table ids are 0x4E..0x60
version = 0xFF; // actual version numbers are 0..31
runningStatus = SI::RunningStatusUndefined;
title = NULL;
shortText = NULL;
description = NULL;
components = NULL;
memset(contents, 0, sizeof(contents));
parentalRating = 0;
startTime = 0;
duration = 0;
vps = 0;
aux = NULL;
SetSeen();
}
cEvent::~cEvent()
{
free(title);
free(shortText);
free(description);
free(aux);
delete components;
}
int cEvent::Compare(const cListObject &ListObject) const
{
cEvent *e = (cEvent *)&ListObject;
int d = startTime - e->startTime;
if (d == 0)
d = int(tableID) - int(e->tableID);
return d;
}
tChannelID cEvent::ChannelID(void) const
{
return schedule ? schedule->ChannelID() : tChannelID();
}
void cEvent::SetEventID(tEventID EventID)
{
if (eventID != EventID) {
if (schedule)
schedule->UnhashEvent(this);
eventID = EventID;
if (schedule)
schedule->HashEvent(this);
}
}
void cEvent::SetTableID(uchar TableID)
{
tableID = TableID;
}
void cEvent::SetVersion(uchar Version)
{
version = Version;
}
void cEvent::SetRunningStatus(int RunningStatus, const cChannel *Channel)
{
if (Channel && runningStatus != RunningStatus && (RunningStatus > SI::RunningStatusNotRunning || runningStatus > SI::RunningStatusUndefined) && schedule && schedule->HasTimer())
isyslog("channel %d (%s) event %s status %d->%d", Channel->Number(), Channel->Name(), *ToDescr(), runningStatus, RunningStatus);
runningStatus = RunningStatus;
}
void cEvent::SetTitle(const char *Title)
{
title = strcpyrealloc(title, Title);
}
void cEvent::SetShortText(const char *ShortText)
{
shortText = strcpyrealloc(shortText, ShortText);
}
void cEvent::SetDescription(const char *Description)
{
description = strcpyrealloc(description, Description);
}
void cEvent::SetComponents(cComponents *Components)
{
delete components;
components = Components;
}
void cEvent::SetContents(uchar *Contents)
{
for (int i = 0; i < MaxEventContents; i++)
contents[i] = Contents[i];
}
void cEvent::SetParentalRating(int ParentalRating)
{
parentalRating = ParentalRating;
}
void cEvent::SetStartTime(time_t StartTime)
{
if (startTime != StartTime) {
if (schedule)
schedule->UnhashEvent(this);
startTime = StartTime;
if (schedule)
schedule->HashEvent(this);
}
}
void cEvent::SetDuration(int Duration)
{
duration = Duration;
}
void cEvent::SetVps(time_t Vps)
{
vps = Vps;
}
void cEvent::SetSeen(void)
{
seen = time(NULL);
}
void cEvent::SetAux(const char *Aux)
{
free(aux);
aux = Aux ? strdup(Aux) : NULL;
}
cString cEvent::ToDescr(void) const
{
char vpsbuf[64] = "";
if (Vps())
sprintf(vpsbuf, "(VPS: %s) ", *GetVpsString());
return cString::sprintf("%s %s-%s %s'%s'", *GetDateString(), *GetTimeString(), *GetEndTimeString(), vpsbuf, Title());
}
void cEvent::IncNumTimers(void) const
{
numTimersMutex.Lock();
numTimers++;
if (schedule)
schedule->IncNumTimers();
numTimersMutex.Unlock();
}
void cEvent::DecNumTimers(void) const
{
numTimersMutex.Lock();
numTimers--;
if (schedule)
schedule->DecNumTimers();
numTimersMutex.Unlock();
}
bool cEvent::IsRunning(bool OrAboutToStart) const
{
return runningStatus >= (OrAboutToStart ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusPausing);
}
const char *cEvent::ContentToString(uchar Content)
{
switch (Content & 0xF0) {
case ecgMovieDrama:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Movie/Drama");
case 0x01: return tr("Content$Detective/Thriller");
case 0x02: return tr("Content$Adventure/Western/War");
case 0x03: return tr("Content$Science Fiction/Fantasy/Horror");
case 0x04: return tr("Content$Comedy");
case 0x05: return tr("Content$Soap/Melodrama/Folkloric");
case 0x06: return tr("Content$Romance");
case 0x07: return tr("Content$Serious/Classical/Religious/Historical Movie/Drama");
case 0x08: return tr("Content$Adult Movie/Drama");
}
break;
case ecgNewsCurrentAffairs:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$News/Current Affairs");
case 0x01: return tr("Content$News/Weather Report");
case 0x02: return tr("Content$News Magazine");
case 0x03: return tr("Content$Documentary");
case 0x04: return tr("Content$Discussion/Inverview/Debate");
}
break;
case ecgShow:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Show/Game Show");
case 0x01: return tr("Content$Game Show/Quiz/Contest");
case 0x02: return tr("Content$Variety Show");
case 0x03: return tr("Content$Talk Show");
}
break;
case ecgSports:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Sports");
case 0x01: return tr("Content$Special Event");
case 0x02: return tr("Content$Sport Magazine");
case 0x03: return tr("Content$Football/Soccer");
case 0x04: return tr("Content$Tennis/Squash");
case 0x05: return tr("Content$Team Sports");
case 0x06: return tr("Content$Athletics");
case 0x07: return tr("Content$Motor Sport");
case 0x08: return tr("Content$Water Sport");
case 0x09: return tr("Content$Winter Sports");
case 0x0A: return tr("Content$Equestrian");
case 0x0B: return tr("Content$Martial Sports");
}
break;
case ecgChildrenYouth:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Children's/Youth Programme");
case 0x01: return tr("Content$Pre-school Children's Programme");
case 0x02: return tr("Content$Entertainment Programme for 6 to 14");
case 0x03: return tr("Content$Entertainment Programme for 10 to 16");
case 0x04: return tr("Content$Informational/Educational/School Programme");
case 0x05: return tr("Content$Cartoons/Puppets");
}
break;
case ecgMusicBalletDance:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Music/Ballet/Dance");
case 0x01: return tr("Content$Rock/Pop");
case 0x02: return tr("Content$Serious/Classical Music");
case 0x03: return tr("Content$Folk/Tradional Music");
case 0x04: return tr("Content$Jazz");
case 0x05: return tr("Content$Musical/Opera");
case 0x06: return tr("Content$Ballet");
}
break;
case ecgArtsCulture:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Arts/Culture");
case 0x01: return tr("Content$Performing Arts");
case 0x02: return tr("Content$Fine Arts");
case 0x03: return tr("Content$Religion");
case 0x04: return tr("Content$Popular Culture/Traditional Arts");
case 0x05: return tr("Content$Literature");
case 0x06: return tr("Content$Film/Cinema");
case 0x07: return tr("Content$Experimental Film/Video");
case 0x08: return tr("Content$Broadcasting/Press");
case 0x09: return tr("Content$New Media");
case 0x0A: return tr("Content$Arts/Culture Magazine");
case 0x0B: return tr("Content$Fashion");
}
break;
case ecgSocialPoliticalEconomics:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Social/Political/Economics");
case 0x01: return tr("Content$Magazine/Report/Documentary");
case 0x02: return tr("Content$Economics/Social Advisory");
case 0x03: return tr("Content$Remarkable People");
}
break;
case ecgEducationalScience:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Education/Science/Factual");
case 0x01: return tr("Content$Nature/Animals/Environment");
case 0x02: return tr("Content$Technology/Natural Sciences");
case 0x03: return tr("Content$Medicine/Physiology/Psychology");
case 0x04: return tr("Content$Foreign Countries/Expeditions");
case 0x05: return tr("Content$Social/Spiritual Sciences");
case 0x06: return tr("Content$Further Education");
case 0x07: return tr("Content$Languages");
}
break;
case ecgLeisureHobbies:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Leisure/Hobbies");
case 0x01: return tr("Content$Tourism/Travel");
case 0x02: return tr("Content$Handicraft");
case 0x03: return tr("Content$Motoring");
case 0x04: return tr("Content$Fitness & Health");
case 0x05: return tr("Content$Cooking");
case 0x06: return tr("Content$Advertisement/Shopping");
case 0x07: return tr("Content$Gardening");
}
break;
case ecgSpecial:
switch (Content & 0x0F) {
case 0x00: return tr("Content$Original Language");
case 0x01: return tr("Content$Black & White");
case 0x02: return tr("Content$Unpublished");
case 0x03: return tr("Content$Live Broadcast");
default: ;
}
break;
default: ;
}
return "";
}
cString cEvent::GetParentalRatingString(void) const
{
if (parentalRating)
return cString::sprintf(tr("ParentalRating$from %d"), parentalRating);
return NULL;
}
cString cEvent::GetDateString(void) const
{
return DateString(startTime);
}
cString cEvent::GetTimeString(void) const
{
return TimeString(startTime);
}
cString cEvent::GetEndTimeString(void) const
{
return TimeString(startTime + duration);
}
cString cEvent::GetVpsString(void) const
{
char buf[25];
struct tm tm_r;
strftime(buf, sizeof(buf), "%d.%m. %R", localtime_r(&vps, &tm_r));
return buf;
}
void cEvent::Dump(FILE *f, const char *Prefix, bool InfoOnly) const
{
if (InfoOnly || startTime + duration + EPG_LINGER_TIME >= time(NULL)) {
fprintf(f, "%sE %u %jd %d %X %X\n", Prefix, eventID, intmax_t(startTime), duration, tableID, version);
if (!isempty(title))
fprintf(f, "%sT %s\n", Prefix, title);
if (!isempty(shortText))
fprintf(f, "%sS %s\n", Prefix, shortText);
if (!isempty(description)) {
strreplace(description, '\n', '|');
fprintf(f, "%sD %s\n", Prefix, description);
strreplace(description, '|', '\n');
}
if (contents[0]) {
fprintf(f, "%sG", Prefix);
for (int i = 0; Contents(i); i++)
fprintf(f, " %02X", Contents(i));
fprintf(f, "\n");
}
if (parentalRating)
fprintf(f, "%sR %d\n", Prefix, parentalRating);
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
fprintf(f, "%sX %s\n", Prefix, *p->ToString());
}
}
if (vps)
fprintf(f, "%sV %jd\n", Prefix, intmax_t(vps));
if (!InfoOnly && !isempty(aux)) {
strreplace(aux, '\n', '|');
fprintf(f, "%s@ %s\n", Prefix, aux);
strreplace(aux, '|', '\n');
}
if (!InfoOnly)
fprintf(f, "%se\n", Prefix);
}
}
bool cEvent::Parse(char *s)
{
char *t = skipspace(s + 1);
switch (*s) {
case 'T': SetTitle(t);
break;
case 'S': SetShortText(t);
break;
case 'D': strreplace(t, '|', '\n');
SetDescription(t);
break;
case 'G': {
memset(contents, 0, sizeof(contents));
for (int i = 0; i < MaxEventContents; i++) {
char *tail = NULL;
int c = strtol(t, &tail, 16);
if (0x00 < c && c <= 0xFF) {
contents[i] = c;
t = tail;
}
else
break;
}
}
break;
case 'R': SetParentalRating(atoi(t));
break;
case 'X': if (!components)
components = new cComponents;
components->SetComponent(components->NumComponents(), t);
break;
case 'V': SetVps(atol(t));
break;
case '@': strreplace(t, '|', '\n');
SetAux(t);
break;
default: esyslog("ERROR: unexpected tag while reading EPG data: %s", s);
return false;
}
return true;
}
bool cEvent::Read(FILE *f, cSchedule *Schedule, int &Line)
{
if (Schedule) {
cEvent *Event = NULL;
char *s;
cReadLine ReadLine;
while ((s = ReadLine.Read(f)) != NULL) {
Line++;
char *t = skipspace(s + 1);
switch (*s) {
case 'E': if (!Event) {
unsigned int EventID;
intmax_t StartTime; // actually time_t, but intmax_t for scanning with "%jd"
int Duration;
unsigned int TableID = 0;
unsigned int Version = 0xFF; // actual value is ignored
int n = sscanf(t, "%u %jd %d %X %X", &EventID, &StartTime, &Duration, &TableID, &Version);
if (n >= 3 && n <= 5) {
Event = (cEvent *)Schedule->GetEventByTime(StartTime);
cEvent *newEvent = NULL;
if (Event)
DELETENULL(Event->components);
if (!Event) {
Event = newEvent = new cEvent(EventID);
Event->seen = 0;
}
if (Event) {
Event->SetTableID(TableID);
Event->SetStartTime(StartTime);
Event->SetDuration(Duration);
if (newEvent)
Schedule->AddEvent(newEvent);
}
}
}
break;
case 'e': if (Event && !Event->Title())
Event->SetTitle(tr("No title"));
Event = NULL;
break;
case 'c': // to keep things simple we react on 'c' here
return true;
default: if (Event && !Event->Parse(s)) {
esyslog("ERROR: EPG data problem in line %d", Line);
return false;
}
}
}
esyslog("ERROR: unexpected end of file while reading EPG data");
}
return false;
}
#define MAXEPGBUGFIXSTATS 13
#define MAXEPGBUGFIXCHANS 100
struct tEpgBugFixStats {
int hits;
int n;
tChannelID channelIDs[MAXEPGBUGFIXCHANS];
tEpgBugFixStats(void) { hits = n = 0; }
};
tEpgBugFixStats EpgBugFixStats[MAXEPGBUGFIXSTATS];
static void EpgBugFixStat(int Number, tChannelID ChannelID)
{
if (0 <= Number && Number < MAXEPGBUGFIXSTATS) {
tEpgBugFixStats *p = &EpgBugFixStats[Number];
p->hits++;
int i = 0;
for (; i < p->n; i++) {
if (p->channelIDs[i] == ChannelID)
break;
}
if (i == p->n && p->n < MAXEPGBUGFIXCHANS)
p->channelIDs[p->n++] = ChannelID;
}
}
void ReportEpgBugFixStats(bool Force)
{
if (Setup.EPGBugfixLevel > 0) {
static time_t LastReport = 0;
time_t now = time(NULL);
if (now - LastReport > 3600 || Force) {
LastReport = now;
struct tm tm_r;
struct tm *ptm = localtime_r(&now, &tm_r);
if (ptm->tm_hour != 5)
return;
}
else
return;
bool GotHits = false;
char buffer[1024];
for (int i = 0; i < MAXEPGBUGFIXSTATS; i++) {
const char *delim = " ";
tEpgBugFixStats *p = &EpgBugFixStats[i];
if (p->hits) {
bool PrintedStats = false;
char *q = buffer;
*buffer = 0;
LOCK_CHANNELS_READ;
for (int c = 0; c < p->n; c++) {
if (const cChannel *Channel = Channels->GetByChannelID(p->channelIDs[c], true)) {
if (!GotHits) {
dsyslog("=====================");
dsyslog("EPG bugfix statistics");
dsyslog("=====================");
dsyslog("IF SOMEBODY WHO IS IN CHARGE OF THE EPG DATA FOR ONE OF THE LISTED");
dsyslog("CHANNELS READS THIS: PLEASE TAKE A LOOK AT THE FUNCTION cEvent::FixEpgBugs()");
dsyslog("IN VDR/epg.c TO LEARN WHAT'S WRONG WITH YOUR DATA, AND FIX IT!");
dsyslog("=====================");
dsyslog("Fix Hits Channels");
GotHits = true;
}
if (!PrintedStats) {
q += snprintf(q, sizeof(buffer) - (q - buffer), "%-3d %-4d", i, p->hits);
PrintedStats = true;
}
q += snprintf(q, sizeof(buffer) - (q - buffer), "%s%s", delim, Channel->Name());
delim = ", ";
if (q - buffer > 80) {
q += snprintf(q, sizeof(buffer) - (q - buffer), "%s...", delim);
break;
}
}
}
if (*buffer)
dsyslog("%s", buffer);
}
p->hits = p->n = 0;
}
if (GotHits)
dsyslog("=====================");
}
}
static void StripControlCharacters(char *s)
{
if (s) {
int len = strlen(s);
while (len > 0) {
int l = Utf8CharLen(s);
uchar *p = (uchar *)s;
if (l == 2 && *p == 0xC2) // UTF-8 sequence
p++;
if (*p == 0x86 || *p == 0x87 || *p == 0x0D) {
memmove(s, p + 1, len - l + 1); // we also copy the terminating 0!
len -= l;
l = 0;
}
s += l;
len -= l;
}
}
}
void cEvent::FixEpgBugs(void)
{
if (isempty(title)) {
// we don't want any "(null)" titles
title = strcpyrealloc(title, tr("No title"));
EpgBugFixStat(12, ChannelID());
}
if (Setup.EPGBugfixLevel == 0)
goto Final;
// Some TV stations apparently have their own idea about how to fill in the
// EPG data. Let's fix their bugs as good as we can:
// Some channels put the ShortText in quotes and use either the ShortText
// or the Description field, depending on how long the string is:
//
// Title
// "ShortText". Description
//
if ((shortText == NULL) != (description == NULL)) {
char *p = shortText ? shortText : description;
if (*p == '"') {
const char *delim = "\".";
char *e = strstr(p + 1, delim);
if (e) {
*e = 0;
char *s = strdup(p + 1);
char *d = strdup(e + strlen(delim));
free(shortText);
free(description);
shortText = s;
description = d;
EpgBugFixStat(1, ChannelID());
}
}
}
// Some channels put the Description into the ShortText (preceded
// by a blank) if there is no actual ShortText and the Description
// is short enough:
//
// Title
// Description
//
if (shortText && !description) {
if (*shortText == ' ') {
memmove(shortText, shortText + 1, strlen(shortText));
description = shortText;
shortText = NULL;
EpgBugFixStat(2, ChannelID());
}
}
// Sometimes they repeat the Title in the ShortText:
//
// Title
// Title
//
if (shortText && strcmp(title, shortText) == 0) {
free(shortText);
shortText = NULL;
EpgBugFixStat(3, ChannelID());
}
// Some channels put the ShortText between double quotes, which is nothing
// but annoying (some even put a '.' after the closing '"'):
//
// Title
// "ShortText"[.]
//
if (shortText && *shortText == '"') {
int l = strlen(shortText);
if (l > 2 && (shortText[l - 1] == '"' || (shortText[l - 1] == '.' && shortText[l - 2] == '"'))) {
memmove(shortText, shortText + 1, l);
char *p = strrchr(shortText, '"');
if (p)
*p = 0;
EpgBugFixStat(4, ChannelID());
}
}
if (Setup.EPGBugfixLevel <= 1)
goto Final;
// Some channels apparently try to do some formatting in the texts,
// which is a bad idea because they have no way of knowing the width
// of the window that will actually display the text.
// Remove excess whitespace:
title = compactspace(title);
shortText = compactspace(shortText);
description = compactspace(description);
#define MAX_USEFUL_EPISODE_LENGTH 40
// Some channels put a whole lot of information in the ShortText and leave
// the Description totally empty. So if the ShortText length exceeds
// MAX_USEFUL_EPISODE_LENGTH, let's put this into the Description
// instead:
if (!isempty(shortText) && isempty(description)) {
if (strlen(shortText) > MAX_USEFUL_EPISODE_LENGTH) {
free(description);
description = shortText;
shortText = NULL;
EpgBugFixStat(6, ChannelID());
}
}
// Some channels put the same information into ShortText and Description.
// In that case we delete one of them:
if (shortText && description && strcmp(shortText, description) == 0) {
if (strlen(shortText) > MAX_USEFUL_EPISODE_LENGTH) {
free(shortText);
shortText = NULL;
}
else {
free(description);
description = NULL;
}
EpgBugFixStat(7, ChannelID());
}
// Some channels use the ` ("backtick") character, where a ' (single quote)
// would be normally used. Actually, "backticks" in normal text don't make
// much sense, so let's replace them:
strreplace(title, '`', '\'');
strreplace(shortText, '`', '\'');
strreplace(description, '`', '\'');
if (Setup.EPGBugfixLevel <= 2)
goto Final;
// The stream components have a "description" field which some channels
// apparently have no idea of how to set correctly:
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
switch (p->stream) {
case 0x01: { // video
if (p->description) {
if (strcasecmp(p->description, "Video") == 0 ||
strcasecmp(p->description, "Bildformat") == 0) {
// Yes, we know it's video - that's what the 'stream' code
// is for! But _which_ video is it?
free(p->description);
p->description = NULL;
EpgBugFixStat(8, ChannelID());
}
}
if (!p->description) {
switch (p->type) {
case 0x01:
case 0x05: p->description = strdup("4:3"); break;
case 0x02:
case 0x03:
case 0x06:
case 0x07: p->description = strdup("16:9"); break;
case 0x04:
case 0x08: p->description = strdup(">16:9"); break;
case 0x09:
case 0x0D: p->description = strdup("HD 4:3"); break;
case 0x0A:
case 0x0B:
case 0x0E:
case 0x0F: p->description = strdup("HD 16:9"); break;
case 0x0C:
case 0x10: p->description = strdup("HD >16:9"); break;
default: ;
}
EpgBugFixStat(9, ChannelID());
}
}
break;
case 0x02: { // audio
if (p->description) {
if (strcasecmp(p->description, "Audio") == 0) {
// Yes, we know it's audio - that's what the 'stream' code
// is for! But _which_ audio is it?
free(p->description);
p->description = NULL;
EpgBugFixStat(10, ChannelID());
}
}
if (!p->description) {
switch (p->type) {
case 0x05: p->description = strdup("Dolby Digital"); break;
default: ; // all others will just display the language
}
EpgBugFixStat(11, ChannelID());
}
}
break;
default: ;
}
}
}
Final:
// And then there are the specially gifted people who put a literal "\n" string where there should be
// a '\n' character:
if (shortText) {
if (char *p = strstr(shortText, "\\n")) {
*p = 0;
p += 2;
char *s = strdup(shortText);
char *d = strdup(cString::sprintf("%s\n\n%s", p, description));
free(shortText);
free(description);
shortText = s;
description = d;
EpgBugFixStat(12, ChannelID());
}
}
description = strreplace(description, "\\n", " \n");
// VDR can't usefully handle newline characters in the title, shortText or component description of EPG
// data, so let's always convert them to blanks (independent of the setting of EPGBugfixLevel):
strreplace(title, '\n', ' ');
strreplace(shortText, '\n', ' ');
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
if (p->description)
strreplace(p->description, '\n', ' ');
}
}
// Same for control characters:
StripControlCharacters(title);
StripControlCharacters(shortText);
StripControlCharacters(description);
}
// --- cSchedule -------------------------------------------------------------
cMutex cSchedule::numTimersMutex;
cSchedule::cSchedule(tChannelID ChannelID)
{
channelID = ChannelID;
events.SetUseGarbageCollector();
numTimers = 0;
modified = 0;
onActualTp = false;
presentSeen = 0;
}
void cSchedule::IncNumTimers(void) const
{
numTimersMutex.Lock();
numTimers++;
numTimersMutex.Unlock();
}
void cSchedule::DecNumTimers(void) const
{
numTimersMutex.Lock();
numTimers--;
numTimersMutex.Unlock();
}
bool cSchedule::OnActualTp(uchar TableId)
{
if ((TableId & 0xF0) == 0x50)
onActualTp = true;
return onActualTp;
}
cEvent *cSchedule::AddEvent(cEvent *Event)
{
events.Add(Event);
Event->schedule = this;
HashEvent(Event);
return Event;
}
void cSchedule::DelEvent(cEvent *Event)
{
if (Event->schedule == this) {
UnhashEvent(Event);
Event->schedule = NULL;
// Removing the event from its schedule prevents it from decrementing the
// schedule's timer counter, so we do it here:
cEvent::numTimersMutex.Lock();
numTimersMutex.Lock();
numTimers -= Event->numTimers;
numTimersMutex.Unlock();
cEvent::numTimersMutex.Unlock();
events.Del(Event);
}
}
void cSchedule::HashEvent(cEvent *Event)
{
if (cEvent *p = eventsHashID.Get(Event->EventID()))
eventsHashID.Del(p, p->EventID());
eventsHashID.Add(Event, Event->EventID());
if (Event->StartTime() > 0) { // 'StartTime < 0' is apparently used with NVOD channels
if (cEvent *p = eventsHashStartTime.Get(Event->StartTime()))
eventsHashStartTime.Del(p, p->StartTime());
eventsHashStartTime.Add(Event, Event->StartTime());
}
}
void cSchedule::UnhashEvent(cEvent *Event)
{
eventsHashID.Del(Event, Event->EventID());
if (Event->StartTime() > 0) // 'StartTime < 0' is apparently used with NVOD channels
eventsHashStartTime.Del(Event, Event->StartTime());