summaryrefslogtreecommitdiff
path: root/regexp.c
blob: db820efc56bc87af384d567c251611effae58fca (plain)
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
/*
 * regexp.c: Regular expression list item
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "regexp.h"

// for PCRE without JIT support
#ifndef PCRE_STUDY_JIT_COMPILE
#define PCRE_STUDY_JIT_COMPILE 0
#endif

/* Global instance */
cEpgfixerList<cRegexp, cEvent> EpgfixerRegexps;

const char *strSources[] = { "title","shorttext","description","undefined" };

typedef enum { ATITLE,PTITLE,TITLE,ASHORTTEXT,PSHORTTEXT,SHORTTEXT,ADESCRIPTION,PDESCRIPTION,DESCRIPTION,RATING } backrefs;
const char *strBackrefs[] = { "atitle","ptitle","title","ashorttext","pshorttext","shorttext","adescription","pdescription","description","rating" };

cRegexp::cRegexp()
{
  regexp = NULL;
  source = REGEXP_UNDEFINED;
  re = NULL;
  sd = NULL;
}

cRegexp::~cRegexp(void)
{
  Free();
  free(regexp);
  FreeCompiled();
}

void cRegexp::Compile()
{
  FreeCompiled();
  const char *error;
  int erroffset;
  re = pcre_compile(regexp, 0, &error, &erroffset, NULL);
  if (error) {
     error("PCRE compile error: %s at offset %i", error, erroffset);
     enabled = false;
     }
  else {
     sd = pcre_study(re, PCRE_STUDY_JIT_COMPILE, (const char **)&error);
     if (error)
        error("PCRE study error: %s", error);
     }
}

void cRegexp::FreeCompiled()
{
  if (re) {
     pcre_free(re);
     re = NULL;
     }
  if (sd) {
#ifdef PCRE_CONFIG_JIT
     pcre_free_study(sd);
#else
     pcre_free(sd);
#endif
     sd = NULL;
     }
}

void cRegexp::SetFromString(char *s, bool Enabled)
{
  FREE(regexp);
  Free();
  FreeCompiled();
  enabled = Enabled;
  bool compile = true;
  if (s[0] == '#') {
     enabled = false;
     source = REGEXP_UNDEFINED;
     string = strdup(s);
     return;
     }
  if (s[0] == '!') {
     enabled = compile = false;
     string = strdup(s+1);
     }
  else
     string = strdup(s);
  char *p = strchr(s, '=');
  if (p) {
     *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)
{
  if (enabled && re && IsActive(Event->ChannelID())) {
     cString tmpstring;
     switch (source) {
            case REGEXP_TITLE:
              tmpstring = Event->Title();
              break;
            case REGEXP_SHORTTEXT:
              tmpstring = Event->ShortText();
              break;
            case REGEXP_DESCRIPTION:
              tmpstring = Event->Description();
              break;
            default:
              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);
     if (rc > 0) {
        int i = 0;
        while (i < 10) {
          if (pcre_get_named_substring(re, tmpstring, ovector, rc, strBackrefs[i], &string) != PCRE_ERROR_NOSUBSTRING) {
             switch (i) {
               case TITLE:
                 Event->SetTitle(string);
                 break;
               case ATITLE:
                 Event->SetTitle(*cString::sprintf("%s %s", Event->Title(), string));
                 break;
               case PTITLE:
                 Event->SetTitle(*cString::sprintf("%s %s", string, Event->Title()));
                 break;
               case SHORTTEXT:
                 Event->SetShortText(string);
                 break;
               case ASHORTTEXT:
                 Event->SetShortText(*cString::sprintf("%s %s", Event->ShortText(), string));
                 break;
               case PSHORTTEXT:
                 Event->SetShortText(*cString::sprintf("%s %s", string, Event->ShortText()));
                 break;
               case DESCRIPTION:
                 Event->SetDescription(string);
                 break;
               case ADESCRIPTION:
                 Event->SetDescription(*cString::sprintf("%s %s", Event->Description(), string));
                 break;
               case PDESCRIPTION:
                 Event->SetDescription(*cString::sprintf("%s %s", string, Event->Description()));
                 break;
               case RATING:
                 Event->SetParentalRating(atoi(string));
                 break;
               default:
                 break;
               }
             pcre_free_substring(string);
             }
           ++i;
           }
        return true;
        }
     }
  return false;
}

void cRegexp::ToggleEnabled(void)
{
  if (source != REGEXP_UNDEFINED)
     enabled = !enabled;
}