Statistics
| Branch: | Tag: | Revision:

root / lib / filter.c @ 4012ec30

History | View | Annotate | Download (3.55 KB)

1
#include <stdio.h>
2
#include <glib.h>
3

    
4
#include "filter.h"
5
#include "filter-private.h"
6

    
7

    
8
static int xfilter_debug_mode = 1;
9

    
10

    
11
int xfilter_init(void)
12
{
13
        return 0;
14
}
15

    
16
void xfilter_done(void)
17
{
18
}
19

    
20
XFilter *xfilter_new(XFilterType type, const char *name)
21
{
22
        XFilter *filter;
23

    
24
        if (type == XF_CONTENT) {
25
                filter = XFILTER(g_new0(XContentFilter, 1));
26
        } else {
27
                filter = XFILTER(g_new0(XTestFilter, 1));
28
        }
29

    
30
        filter->type = type;
31
        filter->name = g_strdup(name);
32

    
33
        return filter;
34
}
35

    
36
void xfilter_free(XFilter *filter)
37
{
38
        if (!filter)
39
                return;
40
        g_free(filter->name);
41
        g_free(filter);
42
}
43

    
44
XFilterType xfilter_get_type(XFilter *filter)
45
{
46
        g_return_val_if_fail(filter != NULL, -1);
47

    
48
        return filter->type;
49
}
50

    
51
const char *xfilter_get_name(XFilter *filter)
52
{
53
        g_return_val_if_fail(filter != NULL, NULL);
54

    
55
        return filter->name;
56
}
57

    
58
void xfilter_set_input_mime_types(XFilter *filter, const char **types)
59
{
60
}
61

    
62
void xfilter_set_output_mime_type(XFilter *filter, const char *type)
63
{
64
}
65

    
66
XMessageData *xfilter_message_data_new(const char *src, const char *mime_type)
67
{
68
        XMessageData *data;
69

    
70
        data = g_new0(XMessageData, 1);
71
        data->content = g_strdup(src);
72
        data->mime_type = g_strdup(mime_type);
73
        return data;
74
}
75

    
76
void xfilter_message_data_free(XMessageData *msgdata)
77
{
78
        if (!msgdata)
79
                return;
80
        g_free(msgdata->mime_type);
81
        g_free(msgdata->content);
82
        g_free(msgdata);
83
}
84

    
85
void xfilter_message_data_set_content(XMessageData *msgdata, char *content)
86
{
87
        msgdata->content = content;
88
}
89

    
90
const char *xfilter_message_data_get_mime_type(const XMessageData *msgdata)
91
{
92
        g_return_val_if_fail(msgdata != NULL, NULL);
93

    
94
        return msgdata->mime_type;
95
}
96

    
97
const char *xfilter_message_data_get_content(const XMessageData *msgdata)
98
{
99
        g_return_val_if_fail(msgdata != NULL, NULL);
100

    
101
        return msgdata->content;
102
}
103

    
104
void xfilter_set_content_filter_func(XContentFilter *filter, XContentFilterFunc func)
105
{
106
        g_return_if_fail(XFILTER(filter)->type == XF_CONTENT);
107

    
108
        filter->content_filter_func = func;
109
}
110

    
111
void xfilter_set_test_filter_func(XTestFilter *filter, XTestFilterFunc func)
112
{
113
        g_return_if_fail(XFILTER(filter)->type == XF_TEST);
114

    
115
        filter->test_filter_func = func;
116
}
117

    
118
XFilterStatus xfilter_exec(XFilter *filter, const XMessageData *msgdata, XMessageData **retdata)
119
{
120
        g_return_val_if_fail(filter != NULL, XF_ERROR);
121
        g_return_val_if_fail(msgdata != NULL, XF_ERROR);
122

    
123
        if (filter->type == XF_CONTENT) {
124
                if (!X_CONTENT_FILTER(filter)->content_filter_func)
125
                        return XF_ERROR;
126
                return X_CONTENT_FILTER(filter)->content_filter_func(filter, msgdata, retdata);
127
        } else {
128
                if (!X_TEST_FILTER(filter)->test_filter_func)
129
                        return XF_ERROR;
130
                return X_TEST_FILTER(filter)->test_filter_func(filter, msgdata);
131
        }
132
}
133

    
134
void xfilter_result_print(XFilterResult *result)
135
{
136
        printf("XFilterResult: status = %d, probability = %f\n", result->status, result->probability);
137
}
138

    
139
XFilterResult *xfilter_result_new(void)
140
{
141
        XFilterResult *res;
142

    
143
        res = g_new0(XFilterResult, 1);
144
        res->status = XF_NONE;
145
        res->probability = 0.5;
146
        return res;
147
}
148

    
149
XFilterStatus xfilter_result_get_status(XFilterResult *result)
150
{
151
        g_return_val_if_fail(result != NULL, XF_ERROR);
152

    
153
        return result->status;
154
}
155

    
156
XMessageData *xfiltrer_result_get_message_data(XFilterResult *result)
157
{
158
        g_return_val_if_fail(result != NULL, NULL);
159

    
160
        return result->msgdata;
161
}
162

    
163
void xfilter_result_free(XFilterResult *result)
164
{
165
        if (!result)
166
                return;
167
        xfilter_message_data_free(result->msgdata);
168
        g_free(result);
169
}
170

    
171
void xfilter_debug_print(const char *format, ...)
172
{
173
        va_list args;
174
        char buf[1024];
175

    
176
        if (!xfilter_debug_mode)
177
                return;
178

    
179
        va_start(args, format);
180
        g_vsnprintf(buf, sizeof(buf), format, args);
181
        va_end(args);
182

    
183
        fputs(buf, stderr);
184
}