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

#include "regexp.h"
#include <unistd.h>

/* Global instance */
cEpgfixerRegexps EpgfixerRegexps;

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

cRegexp::cRegexp()
{
  enabled = false;
  regexp = NULL;
  string = NULL;
  source = REGEXP_UNDEFINED;
  re = NULL;
  sd = NULL;
  numchannels = 0;
  channels_num = NULL;
  channels_str = NULL;
}

cRegexp::~cRegexp(void)
{
  Free();
}

void cRegexp::Free(void)
{
  free(channels_num);
  if (channels_str) {
     int i = 0;
     while (i < numchannels) {
           free(channels_str[i]);
           i++;
           }
     }
  free(channels_str);
  free(regexp);
  free(string);
  channels_num = NULL;
  channels_str = NULL;
  regexp = NULL;
  string = NULL;
  FreeCompiled();
}

int cRegexp::GetChannelNum(int index)
{
  if (channels_num && index >= 0 && index < numchannels)
     return channels_num[index];
  else
     return -1;
}

const char *cRegexp::GetChannelID(int index)
{
  if (channels_str && index >= 0 && index < numchannels)
     return channels_str[index];
  else
     return NULL;
}

void cRegexp::ToggleEnabled(void)
{
  enabled = enabled ? 0 : 1;
}

void cRegexp::SetFromString(char *s, bool Enabled, bool Precompile)
{
  Free();
  string = strdup(s);
  enabled = Enabled;
  bool precompile = Precompile;
  if (s[0] == '!' || s[0] == '#') {
     enabled = false;
     precompile = false;
     }
  char *p = (s[0] == '#') ? s : 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;
           char *c = chanfield;
           numchannels = 0;
           while (c) {
                 numchannels++;
                 c = strchr(c+1, ',');
                 }
           if (numchannels > 0) {
              c = chanfield;
              // Use channel numbers
              if (atoi(chanfield))
                 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(chanfield))
                       channels_num[i] = atoi(pc);
                    else// use channel IDs
                       channels_str[i] = strdup(pc);
                    pc = strtok(NULL, ",");
                    i++;
                    }
              }
           }
        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 (precompile)
           Compile();
        }
     }
}

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::Compile()
{
  FreeCompiled();
  const char *error;
  int erroffset;
  re = pcre_compile(regexp, 0, &error, &erroffset, NULL);
  if (error) {
     esyslog("PCRE compile error: %s at offset %i", error, erroffset);
     enabled = false;
     }
  else {
     sd = pcre_study(re, 0, (const char **)&error);
     if (error)
        esyslog("PCRE study error: %s", error);
     }
}

cEpgfixerRegexps::cEpgfixerRegexps()
{
  fileName = NULL;
  regexps = new cRegexpList();
}

cEpgfixerRegexps::~cEpgfixerRegexps()
{
  free(fileName);
  delete regexps;
}

void cEpgfixerRegexps::SetRegexpConfigFile(const char *FileName)
{
  fileName = strdup(FileName);
}

const char *cEpgfixerRegexps::RegexpConfigFile()
{
  return fileName;
}

bool cEpgfixerRegexps::ReloadRegexps(bool AllowComments, bool Precompile)
{
  regexps->Clear();
  return LoadRegexps(fileName, AllowComments, Precompile);
}

bool cEpgfixerRegexps::LoadRegexps(const char *FileName, bool AllowComments, bool Precompile)
{
  bool result = false;
  if (FileName && access(FileName, F_OK) == 0) {
     FILE *f = fopen(FileName, "r");
     if (f) {
        char *s;
        cReadLine ReadLine;
        while ((s = ReadLine.Read(f)) != NULL) {
              if (!isempty(s)) {
                 regexps->Add(new cRegexp());
                 regexps->Last()->SetFromString(s, true, Precompile);
                 }
              }
        fclose(f);
        }
     else {
        LOG_ERROR_STR(FileName);
        result = false;
        }
     }
  return result;
}