diff options
-rw-r--r-- | HISTORY | 7 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | README | 13 | ||||
-rw-r--r-- | charset.c | 34 | ||||
-rw-r--r-- | charset.h | 8 | ||||
-rw-r--r-- | config.c | 2 | ||||
-rw-r--r-- | config.h | 6 | ||||
-rw-r--r-- | epgfixer.c | 2 | ||||
-rw-r--r-- | epgfixer/charset.conf | 5 | ||||
-rw-r--r-- | epghandler.c | 51 | ||||
-rw-r--r-- | regexp.c | 133 | ||||
-rw-r--r-- | regexp.h | 8 | ||||
-rw-r--r-- | setup_menu.c | 10 | ||||
-rw-r--r-- | tools.c | 104 | ||||
-rw-r--r-- | tools.h | 29 |
15 files changed, 197 insertions, 219 deletions
@@ -1,9 +1,14 @@ VDR Plugin 'epgfixer' Revision History -------------------------------------- -2012-xx-xx: Version 0.0.6 +2012-xx-xx: Version 0.0.7 - Support for character set conversion for selected channels. - Support for stripping HTML entities. +- Cleaning up code (some cleanup by Rolf Ahrenberg). + +2012-04-13: Version 0.0.6 + +- Bug fix release. 2012-04-13: Version 0.0.5 @@ -124,3 +124,7 @@ dist: $(I18Npo) clean clean: @-rm -f $(OBJS) $(DEPFILE) *.so *.tgz core* *~ $(PODIR)/*.mo $(PODIR)/*.pot + +cppcheck: $(OBJS) + @cppcheck --enable=information,style,unusedFunction -v -f $(OBJS:%.o=%.c) + @@ -48,7 +48,7 @@ Syntax of regexp.conf line is "Channel_list:Parsed_epg_field=Regexp" with: All character set conversions are placed to VDRPLUGINCONFDIR/epgfixer/charset.conf. -Syntax of charset.conf line is "Channel_list=OriginalCharSet" with: +Syntax of charset.conf line is "Channel_list:OriginalCharSet" with: - OriginalCharSet is a name of a character set (e.g. iso8859-1). Examples of regexp.conf: @@ -61,6 +61,17 @@ Examples of regexp.conf: 1,3,5:title=^(?:Movie: |Document: )(?<title>.*)$ # Move parental rating from end of title to correct EPG field: +# Program title (12) title=^(?<title>.*)[ ][(](?<rating>[0-9S]{1,2})[)][ ]*$ +Examples of charset.conf: + +# Convert character set of channels 1, 2 and 3 from iso8859-1 to character set +# used by VDR +1,2,3:iso8859-1 + +# Convert character set of all channels from iso8859-1 to character set +# used by VDR +iso8859-1 + @@ -21,23 +21,9 @@ cCharSet::~cCharSet(void) free(charset); } -bool cCharSet::ConvertCharSet(cEvent *Event) +bool cCharSet::Apply(cEvent *Event) { - bool active = true; - if (numchannels > 0) { - bool found = false; - int i = 0; - while (i < numchannels && !found) { - if (Channels.GetByChannelID(Event->ChannelID())->Number() == GetChannelNum(i)) - found = true; - if (GetChannelID(i) && strcmp(*(Event->ChannelID().ToString()), GetChannelID(i)) == 0) - found = true; - i++; - } - if (!found) - active = false; - } - if (active && enabled) { + if (enabled && IsActive(Event->ChannelID())) { cCharSetConv conv(charset, cCharSetConv::SystemCharacterTable()); Event->SetTitle(conv.Convert(Event->Title())); Event->SetShortText(conv.Convert(Event->ShortText())); @@ -48,6 +34,7 @@ bool cCharSet::ConvertCharSet(cEvent *Event) void cCharSet::SetFromString(char *s, bool Enabled) { + FREE(charset); Free(); enabled = Enabled; if (s[0] == '!') @@ -56,14 +43,17 @@ void cCharSet::SetFromString(char *s, bool Enabled) string = strdup(s); if (s[0] == '!' || s[0] == '#') enabled = false; - char *p = (s[0] == '#') ? s : strchr(s, '='); + char *p = (s[0] == '#') ? NULL : s; if (p) { - if (p[0] != '#') { - *p = 0; - charset = strdup(p + 1); - char *chans = (s[0] == '!') ? s+1 : s; - numchannels = loadChannelsFromString(chans, &channels_num, &channels_str); + char *p = (s[0] == '!') ? s+1 : s; + char *f = strchr(p, ':'); + if (f) { + *f = 0; + charset = strdup(f + 1); + numchannels = LoadChannelsFromString(p); } + else + charset = strdup(p); } } @@ -5,8 +5,8 @@ * */ -#ifndef _CHARSET_H_ -#define _CHARSET_H_ +#ifndef __EPGFIXER_CHARSET_H_ +#define __EPGFIXER_CHARSET_H_ #include "tools.h" #include <vdr/epg.h> @@ -20,7 +20,7 @@ private: public: cCharSet(); virtual ~cCharSet(); - bool ConvertCharSet(cEvent *Event); + virtual bool Apply(cEvent *Event); void SetFromString(char *string, bool Enabled); virtual void PrintConfigLineToFile(FILE *f); }; @@ -28,4 +28,4 @@ public: // Global instance extern cEpgfixerList<cCharSet> EpgfixerCharSets; -#endif //_REGEXP_H_ +#endif //__EPGFIXER_CHARSET_H_ @@ -60,7 +60,7 @@ bool cEpgfixerSetup::SetupParse(const char *Name, const char *Value) else if (!strcasecmp(Name, "PreventEqualShortTextAndDescription")) equalshorttextanddescription = atoi(Value); else if (!strcasecmp(Name, "ReplaceBackticksWithSingleQuotes")) nobackticks = atoi(Value); else if (!strcasecmp(Name, "FixStreamComponentDescriptions")) components = atoi(Value); - else if (!strcasecmp(Name, "StripHTMLentities")) striphtml = atoi(Value); + else if (!strcasecmp(Name, "StripHTMLEntities")) striphtml = atoi(Value); else return false; @@ -5,8 +5,8 @@ * */ -#ifndef _EPGFIXER_CONFIG_H_ -#define _EPGFIXER_CONFIG_H_ +#ifndef __EPGFIXER_CONFIG_H_ +#define __EPGFIXER_CONFIG_H_ #include "regexp.h" @@ -36,4 +36,4 @@ protected: // Global instance extern cEpgfixerSetup EpgfixerSetup; -#endif //_EPGFIXER_CONFIG_H_ +#endif //__EPGFIXER_CONFIG_H_ @@ -16,7 +16,7 @@ #error "VDR-1.7.26 API version or greater is required!" #endif -static const char *VERSION = "0.0.6"; +static const char *VERSION = "0.0.7"; static const char *DESCRIPTION = trNOOP("Fix bugs in EPG"); class cPluginEpgfixer : public cPlugin { diff --git a/epgfixer/charset.conf b/epgfixer/charset.conf index bd84ae6..5dd81ef 100644 --- a/epgfixer/charset.conf +++ b/epgfixer/charset.conf @@ -1,3 +1,6 @@ # Convert character set of channels 1, 2 and 3 from iso8859-1 to character set # used by VDR -1,2,3=iso8859-1 +1,2,3:iso8859-1 +# Convert character set of all channels from iso8859-1 to character set +# used by VDR +iso8859-1 diff --git a/epghandler.c b/epghandler.c index c9db409..68e7a28 100644 --- a/epghandler.c +++ b/epghandler.c @@ -156,7 +156,7 @@ void cEpgfixerEpgHandler::FixOriginalEpgBugs(cEvent *event) // apparently have no idea of how to set correctly: const cComponents *components = event->Components(); if (EpgfixerSetup.components && components) { - for (int i = 0; i < components->NumComponents(); i++) { + for (int i = 0; i < components->NumComponents(); ++i) { tComponent *p = components->Component(i); switch (p->stream) { case 0x01: { // video @@ -225,7 +225,7 @@ void cEpgfixerEpgHandler::FixOriginalEpgBugs(cEvent *event) free(temp); } if (components) { - for (int i = 0; i < components->NumComponents(); i++) { + for (int i = 0; i < components->NumComponents(); ++i) { tComponent *p = components->Component(i); if (p->description) strreplace(p->description, '\n', ' '); @@ -235,56 +235,27 @@ void cEpgfixerEpgHandler::FixOriginalEpgBugs(cEvent *event) bool cEpgfixerEpgHandler::FixBugs(cEvent *Event) { - int res = false; - cRegexp *regex = (cRegexp *)EpgfixerRegexps.First(); - while (regex) { - if (regex->Enabled()) { - int ret = regex->Apply(Event); - if (ret && !res) - res = true; - } - regex = (cRegexp *)regex->Next(); - } - return res; + return EpgfixerRegexps.Apply(Event); } bool cEpgfixerEpgHandler::FixCharSets(cEvent *Event) { - int res = false; - cCharSet *charset = (cCharSet *)EpgfixerCharSets.First(); - while (charset) { - if (charset->Enabled()) { - int ret = charset->ConvertCharSet(Event); - if (ret && !res) - res = true; - } - charset = (cCharSet *)charset->Next(); - } - return res; + return EpgfixerCharSets.Apply(Event); } void cEpgfixerEpgHandler::StripHTML(cEvent *Event) { if (EpgfixerSetup.striphtml) { - char *tmpstring; - if (Event->Title()) - tmpstring = strdup(Event->Title()); - else - tmpstring = strdup(""); + char *tmpstring = NULL; + tmpstring = Event->Title() ? strdup(Event->Title()) : NULL; Event->SetTitle(striphtml(tmpstring)); - free(tmpstring); - if (Event->ShortText()) - tmpstring = strdup(Event->ShortText()); - else - tmpstring = strdup(""); + FREE(tmpstring); + tmpstring = Event->ShortText() ? strdup(Event->ShortText()) : NULL; Event->SetShortText(striphtml(tmpstring)); - free(tmpstring); - if (Event->Description()) - tmpstring = strdup(Event->Description()); - else - tmpstring = strdup(""); + FREE(tmpstring); + tmpstring = Event->Description() ? strdup(Event->Description()) : NULL; Event->SetDescription(striphtml(tmpstring)); - free(tmpstring); + FREE(tmpstring); } } @@ -28,7 +28,6 @@ cRegexp::~cRegexp(void) { Free(); free(regexp); - regexp = NULL; FreeCompiled(); } @@ -67,136 +66,101 @@ void cRegexp::FreeCompiled() void cRegexp::SetFromString(char *s, bool Enabled) { + FREE(regexp); Free(); + FreeCompiled(); enabled = Enabled; - if (s[0] == '!') - string = strdup(s+1); - else - string = strdup(s); bool compile = true; - if (s[0] == '!' || s[0] == '#') { + if (s[0] == '#') { enabled = false; - compile = false; + source = REGEXP_UNDEFINED; + string = strdup(s); + return; } - char *p = (s[0] == '#') ? s : strchr(s, '='); + if (s[0] == '!') { + enabled = compile = false; + string = strdup(s+1); + } + else + string = strdup(s); + char *p = strchr(s, '='); if (p) { - if (p[0] == '#') - source = REGEXP_UNDEFINED; - else { - *p = 0; - regexp = strdup(p + 1); - char *chanfield = (s[0] == '!') ? s+1 : s; - char *field = chanfield; - char *f = strchr(chanfield, ':'); - if (f) { - *f = 0; - field = f+1; - numchannels = loadChannelsFromString(chanfield, &channels_num, &channels_str); - } - if (strcmp(field, "title") == 0) - source = REGEXP_TITLE; - if (strcmp(field, "shorttext") == 0) - source = REGEXP_SHORTTEXT; - if (strcmp(field, "description") == 0) - source = REGEXP_DESCRIPTION; - if (compile) - Compile(); + *p = 0; + regexp = strdup(p + 1); + char *chanfield = (s[0] == '!') ? s+1 : s; + char *field = chanfield; + char *f = strchr(chanfield, ':'); + if (f) { + *f = 0; + field = f+1; + numchannels = LoadChannelsFromString(chanfield); } + if (strcmp(field, "title") == 0) + source = REGEXP_TITLE; + if (strcmp(field, "shorttext") == 0) + source = REGEXP_SHORTTEXT; + if (strcmp(field, "description") == 0) + source = REGEXP_DESCRIPTION; + if (compile) + Compile(); } } bool cRegexp::Apply(cEvent *Event) { - bool active = true; - if (numchannels > 0) { - bool found = false; - int i = 0; - while (i < numchannels && !found) { - if (Channels.GetByChannelID(Event->ChannelID())->Number() == GetChannelNum(i)) - found = true; - if (GetChannelID(i) && strcmp(*(Event->ChannelID().ToString()), GetChannelID(i)) == 0) - found = true; - i++; - } - if (!found) - active = false; - } - if (active && enabled && re) { - char *tmpstring; + if (enabled && re && IsActive(Event->ChannelID())) { + cString tmpstring; switch (source) { case REGEXP_TITLE: - tmpstring = strdup(Event->Title()); + tmpstring = Event->Title(); break; case REGEXP_SHORTTEXT: - if (Event->ShortText()) - tmpstring = strdup(Event->ShortText()); - else - tmpstring = strdup(""); + tmpstring = Event->ShortText(); break; case REGEXP_DESCRIPTION: - if (Event->Description()) - tmpstring = strdup(Event->Description()); - else - tmpstring = strdup(""); + tmpstring = Event->Description(); break; default: - tmpstring = strdup(""); + tmpstring = ""; break; } + if (!*tmpstring) + tmpstring = ""; const char *string; int ovector[20]; int rc; - rc = pcre_exec(re, sd, tmpstring, strlen(tmpstring), 0, 0, ovector, 20); + rc = pcre_exec(re, sd, *tmpstring, strlen(*tmpstring), 0, 0, ovector, 20); if (rc > 0) { int i = 0; while (i < 10) { if (pcre_get_named_substring(re, tmpstring, ovector, rc, strBackrefs[i], &string) != PCRE_ERROR_NOSUBSTRING) { - char *tempstring = 0; switch (i) { case TITLE: Event->SetTitle(string); break; case ATITLE: - tempstring = (char *)malloc(strlen(Event->Title())+strlen(string)+1); - strcpy(tempstring, Event->Title()); - strcat(tempstring, string); - Event->SetTitle(tempstring); + Event->SetTitle(*cString::sprintf("%s %s", Event->Title(), string)); break; case PTITLE: - tempstring = (char *)malloc(strlen(Event->Title())+strlen(string)+1); - strcpy(tempstring, string); - strcat(tempstring, Event->Title()); - Event->SetTitle(tempstring); + Event->SetTitle(*cString::sprintf("%s %s", string, Event->Title())); break; case SHORTTEXT: Event->SetShortText(string); break; case ASHORTTEXT: - tempstring = (char *)malloc(strlen(Event->ShortText())+strlen(string)+1); - strcpy(tempstring, Event->ShortText()); - strcat(tempstring, string); - Event->SetShortText(tempstring); + Event->SetShortText(*cString::sprintf("%s %s", Event->ShortText(), string)); break; case PSHORTTEXT: - tempstring = (char *)malloc(strlen(Event->ShortText())+strlen(string)+1); - strcpy(tempstring, string); - strcat(tempstring, Event->ShortText()); - Event->SetShortText(tempstring); + Event->SetShortText(*cString::sprintf("%s %s", string, Event->ShortText())); break; case DESCRIPTION: Event->SetDescription(string); break; case ADESCRIPTION: - tempstring = (char *)malloc(strlen(Event->Description())+strlen(string)+1); - strcpy(tempstring, Event->Description()); - strcat(tempstring, string); - Event->SetDescription(tempstring); + Event->SetDescription(*cString::sprintf("%s %s", Event->Description(), string)); break; case PDESCRIPTION: - tempstring = (char *)malloc(strlen(Event->Description())+strlen(string)+1); - strcpy(tempstring, string); - strcat(tempstring, Event->Description()); - Event->SetDescription(tempstring); + Event->SetDescription(*cString::sprintf("%s %s", string, Event->Description())); break; case RATING: Event->SetParentalRating(atoi(string)); @@ -205,14 +169,11 @@ bool cRegexp::Apply(cEvent *Event) break; } pcre_free_substring(string); - free(tempstring); } - i++; + ++i; } - free(tmpstring); return true; } - free(tmpstring); } return false; } @@ -230,5 +191,5 @@ void cRegexp::PrintConfigLineToFile(FILE *f) void cRegexp::ToggleEnabled(void) { if (source != REGEXP_UNDEFINED) - enabled = enabled ? 0 : 1; + enabled = !enabled; } @@ -5,8 +5,8 @@ * */ -#ifndef _REGEXP_H_ -#define _REGEXP_H_ +#ifndef __EPGFIXER_REGEXP_H_ +#define __EPGFIXER_REGEXP_H_ #include "tools.h" #include <vdr/epg.h> @@ -31,7 +31,7 @@ private: public: cRegexp(); virtual ~cRegexp(); - bool Apply(cEvent *Event); + virtual bool Apply(cEvent *Event); void SetFromString(char *string, bool Enabled); int GetSource() { return source; }; void ToggleEnabled(void); @@ -41,4 +41,4 @@ public: // Global instance extern cEpgfixerList<cRegexp> EpgfixerRegexps; -#endif //_REGEXP_H_ +#endif //__EPGFIXER_REGEXP_H_ diff --git a/setup_menu.c b/setup_menu.c index a90030d..c45fafb 100644 --- a/setup_menu.c +++ b/setup_menu.c @@ -35,7 +35,7 @@ private: lines[i] = (char *)malloc(sizeof(char)*MAXREGEXPLENGTH); snprintf(lines[i], MAXREGEXPLENGTH, "%s", item->GetString()); item = (T *)item->Next(); - i++; + ++i; } } void FreeArray() @@ -43,9 +43,9 @@ private: int i = 0; while (i < list->Count()) { free(lines[i]); - i++; + ++i; } - free(lines); + FREE(lines); } void Save() { @@ -71,7 +71,7 @@ protected: while (i < list->Count()) { item->SetFromString(lines[i], item->Enabled()); item = (T *)item->Next(); - i++; + ++i; } } void Set(void) @@ -82,7 +82,7 @@ protected: while (i < list->Count()) { Add(new cMenuEditStrItem(item->Enabled() ? "+" : "-", lines[i], MAXREGEXPLENGTH, RegexpChars)); item = (T *)item->Next(); - i++; + ++i; } SetHelp(tr("Toggle state"), tr("Add"), tr("Delete"), tr("Cancel")); Display(); @@ -152,41 +152,6 @@ char *striphtml(char *str) return NULL; } -int count(const char *string, const char separator) -{ - int num = 0; - while (string) { - num++; - string = strchr(string+1, separator); - } - return num; -} - -int loadChannelsFromString(const char *string, int **channels_num, char ***channels_str) -{ - int numchannels = count(string, ','); - if (numchannels > 0) { - char *c = strdup(string); - // Use channel numbers - if (atoi(string)) - *channels_num = (int *)malloc(sizeof(int)*numchannels); - else// use channel IDs - *channels_str = (char **)malloc(sizeof(char *)*numchannels); - int i = 0; - char *pc = strtok(c, ","); - while (i < numchannels) { - // Use channel numbers - if (atoi(string)) - (*channels_num)[i] = atoi(pc); - else// use channel IDs - (*channels_str)[i] = strdup(pc); - pc = strtok(NULL, ","); - i++; - } - } - return numchannels; -} - cListItem::cListItem() { enabled = false; @@ -203,19 +168,18 @@ cListItem::~cListItem(void) void cListItem::Free(void) { - free(channels_num); if (channels_str) { int i = 0; while (i < numchannels) { free(channels_str[i]); - i++; + ++i; } } - free(channels_str); - free(string); - channels_num = NULL; - channels_str = NULL; - string = NULL; + FREE(channels_num); + FREE(channels_str); + FREE(string); + numchannels = 0; + enabled = false; } const char *cListItem::GetChannelID(int index) @@ -231,10 +195,62 @@ int cListItem::GetChannelNum(int index) if (channels_num && index >= 0 && index < numchannels) return channels_num[index]; else - return -1; + return 0; +} + +bool cListItem::IsActive(tChannelID ChannelID) +{ + bool active = true; + if (numchannels > 0) { + bool found = false; + int i = 0; + while (i < numchannels) { + if ((Channels.GetByChannelID(ChannelID)->Number() == GetChannelNum(i)) || + (GetChannelID(i) && strcmp(*(ChannelID.ToString()), GetChannelID(i)) == 0)) { + found = true; + break; + } + ++i; + } + if (!found) + active = false; + } + return active; +} + +int cListItem::LoadChannelsFromString(const char *string) +{ + numchannels = 0; + char *tmpstring = strdup(string); + char *c = tmpstring; + while (c) { + ++numchannels; + c = strchr(c+1, ','); + } + if (numchannels > 0) { + char *c = tmpstring; + // Use channel numbers + if (atoi(string)) + channels_num = (int *)malloc(sizeof(int)*numchannels); + else// use channel IDs + channels_str = (char **)malloc(sizeof(char *)*numchannels); + int i = 0; + char *pc = strtok(c, ","); + while (i < numchannels) { + // Use channel numbers + if (atoi(string)) + (channels_num)[i] = atoi(pc); + else// use channel IDs + (channels_str)[i] = strdup(pc); + pc = strtok(NULL, ","); + ++i; + } + } + free(tmpstring); + return numchannels; } void cListItem::ToggleEnabled(void) { - enabled = enabled ? 0 : 1; + enabled = !enabled; } @@ -5,16 +5,17 @@ * */ -#ifndef _STRINGTOOLS_H_ -#define _STRINGTOOLS_H_ +#ifndef __EPGFIXER_STRINGTOOLS_H_ +#define __EPGFIXER_STRINGTOOLS_H_ +#include <vdr/epg.h> #include <vdr/tools.h> #include <unistd.h> #include <stdio.h> +#define FREE(x) { free(x); x = NULL; } + char *striphtml(char *str); -int count(const char *string, const char separator); -int loadChannelsFromString(const char *string, int **channels_num, char ***channels_str); class cListItem : public cListObject { @@ -27,14 +28,16 @@ protected: void Free(); const char *GetChannelID(int index); int GetChannelNum(int index); + int LoadChannelsFromString(const char *string); + bool IsActive(tChannelID ChannelID); public: cListItem(); virtual ~cListItem(); + virtual bool Apply(cEvent *Event) { return 0; } void SetFromString(char *string, bool Enabled); const char *GetString() { return string; } bool Enabled(void) { return enabled; } void ToggleEnabled(void); - int NumChannels() { return numchannels; } virtual void PrintConfigLineToFIle(FILE *f) {} }; @@ -75,8 +78,22 @@ public: Clear(); return LoadConfigFile(fileName, AllowComments); } + bool Apply(cEvent *Event) + { + int res = false; + T *item = (T *)(cList<T>::First()); + while (item) { + if (item->Enabled()) { + int ret = item->Apply(Event); + if (ret && !res) + res = true; + } + item = (T *)(item->Next()); + } + return res; + } void SetConfigFile(const char *FileName) { fileName = strdup(FileName); } const char *GetConfigFile() { return fileName; } }; -#endif //_STRINGTOOLS_H_ +#endif //__EPGFIXER_STRINGTOOLS_H_ |