diff options
Diffstat (limited to 'regexp.c')
-rw-r--r-- | regexp.c | 133 |
1 files changed, 47 insertions, 86 deletions
@@ -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; } |