source: filterelement.c @ fa00c5c

owl
Last change on this file since fa00c5c was fa00c5c, checked in by James M. Kretchmar <kretch@mit.edu>, 16 years ago
Correct license.
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
2 *
3 * This file is part of Owl.
4 *
5 * Owl is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Owl is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * ---------------------------------------------------------------
19 *
20 * As of Owl version 2.1.12 there are patches contributed by
21 * developers of the branched BarnOwl project, Copyright (c)
22 * 2006-2009 The BarnOwl Developers. All rights reserved.
23 */
24
25#include "owl.h"
26
27static const char fileIdent[] = "$Id$";
28
29#define OWL_FILTERELEMENT_NULL        0
30#define OWL_FILTERELEMENT_TRUE        1
31#define OWL_FILTERELEMENT_FALSE       2
32#define OWL_FILTERELEMENT_OPENBRACE   3
33#define OWL_FILTERELEMENT_CLOSEBRACE  4
34#define OWL_FILTERELEMENT_AND         5
35#define OWL_FILTERELEMENT_OR          6
36#define OWL_FILTERELEMENT_NOT         7
37#define OWL_FILTERELEMENT_RE          8
38#define OWL_FILTERELEMENT_FILTER      9
39#define OWL_FILTERELEMENT_PERL       10
40
41void owl_filterelement_create_null(owl_filterelement *fe)
42{
43  fe->type=OWL_FILTERELEMENT_NULL;
44  fe->field=NULL;
45  fe->filtername=NULL;
46}
47
48void owl_filterelement_create_openbrace(owl_filterelement *fe)
49{
50  owl_filterelement_create_null(fe);
51  fe->type=OWL_FILTERELEMENT_OPENBRACE;
52}
53
54void owl_filterelement_create_closebrace(owl_filterelement *fe)
55{
56  owl_filterelement_create_null(fe);
57  fe->type=OWL_FILTERELEMENT_CLOSEBRACE;
58}
59
60void owl_filterelement_create_and(owl_filterelement *fe)
61{
62  owl_filterelement_create_null(fe);
63  fe->type=OWL_FILTERELEMENT_AND;
64}
65
66void owl_filterelement_create_or(owl_filterelement *fe)
67{
68  owl_filterelement_create_null(fe);
69  fe->type=OWL_FILTERELEMENT_OR;
70}
71
72void owl_filterelement_create_not(owl_filterelement *fe)
73{
74  owl_filterelement_create_null(fe);
75  fe->type=OWL_FILTERELEMENT_NOT;
76}
77
78void owl_filterelement_create_true(owl_filterelement *fe)
79{
80  owl_filterelement_create_null(fe);
81  fe->type=OWL_FILTERELEMENT_TRUE;
82}
83
84void owl_filterelement_create_false(owl_filterelement *fe)
85{
86  owl_filterelement_create_null(fe);
87  fe->type=OWL_FILTERELEMENT_FALSE;
88}
89
90void owl_filterelement_create_re(owl_filterelement *fe, char *field, char *re)
91{
92  owl_filterelement_create_null(fe);
93  fe->type=OWL_FILTERELEMENT_RE;
94  fe->field=owl_strdup(field);
95  owl_regex_create(&(fe->re), re);
96}
97
98void owl_filterelement_create_filter(owl_filterelement *fe, char *name)
99{
100  owl_filterelement_create_null(fe);
101  fe->type=OWL_FILTERELEMENT_FILTER;
102  fe->filtername=owl_strdup(name);
103}
104
105void owl_filterelement_create_perl(owl_filterelement *fe, char *name)
106{
107  owl_filterelement_create_null(fe);
108  fe->type=OWL_FILTERELEMENT_PERL;
109  fe->filtername=owl_strdup(name);
110}
111
112void owl_filterelement_free(owl_filterelement *fe)
113{
114  if (fe->field) owl_free(fe->field);
115  if (fe->filtername) owl_free(fe->filtername);
116}
117
118int owl_filterelement_is_null(owl_filterelement *fe)
119{
120  if (fe->type==OWL_FILTERELEMENT_NULL) return(1);
121  return(0);
122}
123
124int owl_filterelement_is_openbrace(owl_filterelement *fe)
125{
126  if (fe->type==OWL_FILTERELEMENT_OPENBRACE) return(1);
127  return(0);
128}
129
130int owl_filterelement_is_closebrace(owl_filterelement *fe)
131{
132  if (fe->type==OWL_FILTERELEMENT_CLOSEBRACE) return(1);
133  return(0);
134}
135
136int owl_filterelement_is_and(owl_filterelement *fe)
137{
138  if (fe->type==OWL_FILTERELEMENT_AND) return(1);
139  return(0);
140}
141
142int owl_filterelement_is_or(owl_filterelement *fe)
143{
144  if (fe->type==OWL_FILTERELEMENT_OR) return(1);
145  return(0);
146}
147
148int owl_filterelement_is_not(owl_filterelement *fe)
149{
150  if (fe->type==OWL_FILTERELEMENT_NOT) return(1);
151  return(0);
152}
153
154int owl_filterelement_is_true(owl_filterelement *fe)
155{
156  if (fe->type==OWL_FILTERELEMENT_TRUE) return(1);
157  return(0);
158}
159
160int owl_filterelement_is_false(owl_filterelement *fe)
161{
162  if (fe->type==OWL_FILTERELEMENT_FALSE) return(1);
163  return(0);
164}
165
166int owl_filterelement_is_re(owl_filterelement *fe)
167{
168  if (fe->type==OWL_FILTERELEMENT_RE) return(1);
169  return(0);
170}
171
172int owl_filterelement_is_perl(owl_filterelement *fe)
173{
174  if (fe->type==OWL_FILTERELEMENT_PERL) return(1);
175  return(0);
176}
177
178owl_regex *owl_filterelement_get_re(owl_filterelement *fe)
179{
180  return(&(fe->re));
181}
182
183int owl_filterelement_is_filter(owl_filterelement *fe)
184{
185  if (fe->type==OWL_FILTERELEMENT_FILTER) return(1);
186  return(0);
187}
188
189char *owl_filterelement_get_field(owl_filterelement *fe)
190{
191  if (fe->field) return(fe->field);
192  return("unknown-field");
193}
194
195char *owl_filterelement_get_filtername(owl_filterelement *fe)
196{
197  if (fe->filtername) return(fe->filtername);
198  return("unknown-filter");
199}
200
201int owl_filterelement_is_value(owl_filterelement *fe)
202{
203  if ( (fe->type==OWL_FILTERELEMENT_TRUE) ||
204       (fe->type==OWL_FILTERELEMENT_FALSE) ||
205       (fe->type==OWL_FILTERELEMENT_RE) ||
206       (fe->type==OWL_FILTERELEMENT_PERL) ||
207       (fe->type==OWL_FILTERELEMENT_FILTER)) {
208    return(1);
209  }
210  return(0);
211}
212
213/* caller must free the return */
214char *owl_filterelement_to_string(owl_filterelement *fe)
215{
216  if (owl_filterelement_is_openbrace(fe)) {
217    return(owl_strdup("( "));
218  } else if (owl_filterelement_is_closebrace(fe)) {
219    return(owl_strdup(") "));
220  } else if (owl_filterelement_is_and(fe)) {
221    return(owl_strdup("and "));
222  } else if (owl_filterelement_is_or(fe)) {
223    return(owl_strdup("or "));
224  } else if (owl_filterelement_is_not(fe)) {
225    return(owl_strdup("not "));
226  } else if (owl_filterelement_is_true(fe)) {
227    return(owl_strdup("true "));
228  } else if (owl_filterelement_is_false(fe)) {
229    return(owl_strdup("false "));
230  } else if (owl_filterelement_is_re(fe)) {
231    return(owl_sprintf("%s %s ", fe->field, owl_regex_get_string(&(fe->re))));
232  } else if (owl_filterelement_is_filter(fe)) {
233    return(owl_sprintf("filter %s ", fe->filtername));
234  } else if (owl_filterelement_is_perl(fe)) {
235    return(owl_sprintf("perl %s ", fe->filtername));
236  }
237
238  return(owl_strdup("?"));
239}
Note: See TracBrowser for help on using the repository browser.