summaryrefslogtreecommitdiff
path: root/event.c
blob: 4ec0b474ba191305740627e31d088ca3c3c28e05 (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
/**
 * based on event.c,v 1.1.1.1 2006/02/26 14:11:02 lordjaxom
 *
 * version by Midas
 *
 */

#include "event.h"

#include <ctype.h>

static char *duptolower(const char *s) {
	char *c = strdup(s);
	char *p = c;
	for (; *p; ++p)
		*p = tolower(*p);
	return c;
}

const char* cEventBlock::LastTitle="block_dummy_title3";
const bool* cEventBlock::ReplayingRecording=false;

cEventsBlock EventsBlock;

cEventBlock::cEventBlock(void):
		mRegularExp(false),
		mIgnoreCase(false),
		mCompiled(false)
{
  strncpy(mPattern, tr("New Entry"), sizeof(mPattern));
}

cEventBlock::cEventBlock(const char *Pattern):
		mRegularExp(false),
		mIgnoreCase(false),
		mCompiled(false)
{
  strncpy(mPattern, Pattern, sizeof(mPattern));
}

cEventBlock::cEventBlock(const cEventBlock &Src)
{
	operator=(Src);
}

cEventBlock &cEventBlock::operator=(const cEventBlock &Src)
{
	printf("copy construct\n");
	strcpy(mPattern, Src.mPattern);
	mRegularExp = Src.mRegularExp;
	mIgnoreCase = Src.mIgnoreCase;
	mCompiled   = false;
	Compile();
	return *this;
}

cEventBlock::~cEventBlock()
{
	if (mRegularExp)
		regfree(&mExpression);
}

bool cEventBlock::Acceptable(const char *Event) const 
{
	if (mRegularExp)
		return regexec(&mExpression, Event, 0, NULL, 0) != 0;
	else if (mIgnoreCase) {
		char *ev = duptolower(Event);
		char *pa = duptolower(mPattern);
		printf("check for %s in %s\n", pa, ev);
		bool res = strstr(ev, pa) == NULL;
		free(ev); free(pa);
		return res;
	} else
		return strstr(Event, mPattern) == NULL;
}

bool cEventBlock::Parse(char *s) {
	char *patternbuf = NULL;
	int fields = sscanf(s, "%d:%d:%a[^\n]", &mRegularExp, &mIgnoreCase, &patternbuf);

	if (fields == 3) {
		strncpy(mPattern, skipspace(stripspace(patternbuf)), sizeof(mPattern));
		free(patternbuf);
	} else { // backward compatibility
		strncpy(mPattern, skipspace(stripspace(s)), sizeof(mPattern));
		mRegularExp = false;
		mIgnoreCase = false;
	}

  return Compile();
}

bool cEventBlock::Compile(void) {
	mCompiled = false;
	if (mRegularExp) {
		if (regcomp(&mExpression, mPattern, REG_EXTENDED | (mIgnoreCase ? REG_ICASE : 0)) != 0) {
			esyslog("ERROR: malformed regular expression: %s", mPattern);
			return false;
		} else
			mCompiled = true;
	}
	return true;
}

bool cEventBlock::Save(FILE *f) {
  return fprintf(f, "%d:%d:%s\n", mRegularExp, mIgnoreCase, mPattern) > 0;
}

bool cEventsBlock::Acceptable(const char *Event) {
  const cEventBlock *event = First();
  while (event != NULL) {
    if (!event->Acceptable(Event))
      return false;
    event = Next(event);
  }
  return true;
}

cEventsBlock &cEventsBlock::operator=(const cEventsBlock &Source) {
  cList<cEventBlock>::Clear();
  
  const cEventBlock *event = Source.First();
  while (event != NULL) {
    printf("transfering %p\n", event);
    Add(new cEventBlock(*event));
    event = Source.Next(event);
  }
  return *this;
}