Beispiel #1
0
bool matcher::matches_r(expression * e, matchable * item) {
	if (e) {
		switch (e->op) {
			/* the operator "and" and "or" simply connect two different subexpressions */
			case LOGOP_AND:
				return matches_r(e->l, item) && matches_r(e->r, item); // short-circuit evaulation in C -> short circuit evaluation in the filter language

			case LOGOP_OR:
				return matches_r(e->l, item) || matches_r(e->r, item); // same here

			/* while the other operator connect an attribute with a value */
			case MATCHOP_EQ:
				return matchop_eq(e, item);

			case MATCHOP_NE:
				return !matchop_eq(e, item);

			case MATCHOP_LT:
				return matchop_lt(e, item);

			case MATCHOP_BETWEEN:
				return matchop_between(e, item);

			case MATCHOP_GT:
				return matchop_gt(e, item);

			case MATCHOP_LE:
				return !matchop_gt(e, item);

			case MATCHOP_GE:
				return !matchop_lt(e, item);

			case MATCHOP_RXEQ:
				return matchop_rxeq(e, item);

			case MATCHOP_RXNE:
				return !matchop_rxeq(e, item);

			case MATCHOP_CONTAINS:
				return matchop_cont(e, item);

			case MATCHOP_CONTAINSNOT:
				return !matchop_cont(e, item);

			default:
				LOG(LOG_ERROR, "matcher::matches_r: invalid operator %d", e->op);
				assert(false); // that's an error condition
		}
		return false;
	} else {
		return true; // shouldn't happen
	}
}
Beispiel #2
0
bool matcher::matches(matchable* item) {
	/*
	 * with this method, every class that is derived from matchable can be
	 * matched against a filter expression that was previously passed to the
	 * class with the parse() method.
	 *
	 * This makes it easy to use the matcher virtually everywhere, since C++
	 * allows multiple inheritance (i.e. deriving from matchable can even be
	 * used in class hierarchies), and deriving from matchable means that you
	 * only have to implement two methods has_attribute() and get_attribute().
	 *
	 * The whole matching code is speed-critical, as the matching happens on a
	 * lot of different occassions, and slow matching can be easily measured
	 * (and felt by the user) on slow computers with a lot of items to match.
	 */
	bool retval = false;
	if (item) {
		scope_measure m1("matcher::matches");
		retval = matches_r(p.get_root(), item);
	}
	return retval;
}