+/*! \file lttv/lttv/filter.c
+ * \brief Defines the core filter of application
+ *
+ * consist in AND, OR and NOT nested expressions, forming a tree with
+ * simple relations as leaves. The simple relations test if a field
+ * in an event is equal, not equal, smaller, smaller or equal, larger, or
+ * larger or equal to a specified value.
+ *
+ * Fields specified in a simple expression can take following
+ * values
+ *
+ * \verbatim
+ * LttvTracefileContext{}
+ * |->event\
+ * | |->name (String, converted to GQuark)
+ * | |->facility (String, converted to GQuark)
+ * | |->category (String, not yet implemented)
+ * | |->time (LttTime)
+ * | |->tsc (LttCycleCount --> uint64)
+ * | |->fields
+ * | |->"facility_name
+ * | |->"event name"
+ * | |->"field name"
+ * | |->"sub-field name"
+ * | |->...
+ * | |->"leaf-field name" (field type)
+ * |->tracefile
+ * | |->name (String, converted to GQuark)
+ * |->trace
+ * | |->name (String, converted to GQuark)
+ * |->state
+ * |->pid (guint)
+ * |->ppid (guint)
+ * |->creation_time (LttTime)
+ * |->insertion_time (LttTime)
+ * |->process_name (String, converted to GQuark)
+ * |->execution_mode (LttvExecutionMode)
+ * |->execution_submode (LttvExecutionSubmode)
+ * |->process_status (LttvProcessStatus)
+ * |->cpu (guint)
+ * \endverbatim
+ */
+
+/*
+ * \todo
+ * - refine switch of expression in multiple uses functions
+ * - remove the idle expressions in the tree
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+//#define TEST
+#ifdef TEST
+#include <time.h>
+#include <sys/time.h>
+#endif
+
+#include <lttv/lttv.h>
+#include <lttv/filter.h>
+#include <ltt/trace.h>
+#include <ltt/type.h>
+#include <ltt/facility.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * @fn LttvSimpleExpression* lttv_simple_expression_new()
+ *
+ * Constructor for LttvSimpleExpression
+ * @return pointer to new LttvSimpleExpression
+ */
+LttvSimpleExpression*
+lttv_simple_expression_new() {
+
+ LttvSimpleExpression* se = g_new(LttvSimpleExpression,1);
+
+ se->field = LTTV_FILTER_UNDEFINED;
+ se->op = NULL;
+ se->offset = 0;
+
+ return se;
+}
+
+/**
+ * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
+ *
+ * Parse through filtering field hierarchy as specified
+ * by user. This function compares each value to
+ * predetermined quarks
+ * @param fp The field path list
+ * @param se current simple expression
+ * @return success/failure of operation
+ */
+gboolean
+lttv_simple_expression_assign_field(GPtrArray* fp, LttvSimpleExpression* se) {
+
+ GString* f = NULL;
+
+ if(fp->len < 2) return FALSE;
+ g_assert((f=g_ptr_array_remove_index(fp,0)));
+
+ /*
+ * Parse through the specified
+ * hardcoded fields.
+ *
+ * Take note however that the
+ * 'event' subfields might change
+ * depending on values specified
+ * in core.xml file. Hence, if
+ * none of the subfields in the
+ * array match the hardcoded
+ * subfields, it will be considered
+ * as a dynamic field
+ */
+ if(!g_strcasecmp(f->str,"trace") ) {
+ /*
+ * Possible values:
+ * trace.name
+ */
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+ if(!g_strcasecmp(f->str,"name")) {
+ se->field = LTTV_FILTER_TRACE_NAME;
+ }
+ } else if(!g_strcasecmp(f->str,"traceset") ) {
+ /*
+ * FIXME: not yet implemented !
+ */
+ } else if(!g_strcasecmp(f->str,"tracefile") ) {
+ /*
+ * Possible values:
+ * tracefile.name
+ */
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+ if(!g_strcasecmp(f->str,"name")) {
+ se->field = LTTV_FILTER_TRACEFILE_NAME;
+ }
+ } else if(!g_strcasecmp(f->str,"state") ) {
+ /*
+ * Possible values:
+ * state.pid
+ * state.ppid
+ * state.creation_time
+ * state.insertion_time
+ * state.process_name
+ * state.execution_mode
+ * state.execution_submode
+ * state.process_status
+ * state.cpu
+ */
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+ if(!g_strcasecmp(f->str,"pid") ) {
+ se->field = LTTV_FILTER_STATE_PID;
+ }
+ else if(!g_strcasecmp(f->str,"ppid") ) {
+ se->field = LTTV_FILTER_STATE_PPID;
+ }
+ else if(!g_strcasecmp(f->str,"creation_time") ) {
+ se->field = LTTV_FILTER_STATE_CT;
+ }
+ else if(!g_strcasecmp(f->str,"insertion_time") ) {
+ se->field = LTTV_FILTER_STATE_IT;
+ }
+ else if(!g_strcasecmp(f->str,"process_name") ) {
+ se->field = LTTV_FILTER_STATE_P_NAME;
+ }
+ else if(!g_strcasecmp(f->str,"execution_mode") ) {
+ se->field = LTTV_FILTER_STATE_EX_MODE;
+ }
+ else if(!g_strcasecmp(f->str,"execution_submode") ) {
+ se->field = LTTV_FILTER_STATE_EX_SUBMODE;
+ }
+ else if(!g_strcasecmp(f->str,"process_status") ) {
+ se->field = LTTV_FILTER_STATE_P_STATUS;
+ }
+ else if(!g_strcasecmp(f->str,"cpu") ) {
+ se->field = LTTV_FILTER_STATE_CPU;
+ }
+ } else if(!g_strcasecmp(f->str,"event") ) {
+ /*
+ * Possible values:
+ * event.name
+ * event.category
+ * event.time
+ * event.tsc
+ * event.field
+ */
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+ if(!g_strcasecmp(f->str,"name") ) {
+ se->field = LTTV_FILTER_EVENT_NAME;
+ }
+ else if(!g_strcasecmp(f->str,"facility") ) {
+ se->field = LTTV_FILTER_EVENT_FACILITY;
+ }
+ else if(!g_strcasecmp(f->str,"category") ) {
+ /*
+ * FIXME: Category not yet functional in lttv
+ */
+ se->field = LTTV_FILTER_EVENT_CATEGORY;
+ }
+ else if(!g_strcasecmp(f->str,"time") ) {
+ se->field = LTTV_FILTER_EVENT_TIME;
+ }
+ else if(!g_strcasecmp(f->str,"tsc") ) {
+ se->field = LTTV_FILTER_EVENT_TSC;
+ }
+ else if(!g_strcasecmp(f->str,"field") ) {
+ se->field = LTTV_FILTER_EVENT_FIELD;
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+
+ } else {
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+ g_warning("Unknown event filter subtype %s", f->str);
+ }
+ } else {
+ g_string_free(f,TRUE);
+ f=g_ptr_array_remove_index(fp,0);
+
+ g_warning("Unrecognized field in filter string");
+ }
+
+ /* free memory for last string */
+ g_string_free(f,TRUE);
+
+ /* array should be empty */
+ g_assert(fp->len == 0);
+
+ if(se->field == LTTV_FILTER_UNDEFINED) {
+ g_warning("The specified field was not recognized !");
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/**
+ * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
+ *
+ * Sets the function pointer for the current
+ * Simple Expression
+ * @param se current simple expression
+ * @param op current operator
+ * @return success/failure of operation
+ */
+gboolean
+lttv_simple_expression_assign_operator(LttvSimpleExpression* se, LttvExpressionOp op) {
+
+ switch(se->field) {
+ /*
+ * string
+ */
+ case LTTV_FILTER_TRACE_NAME:
+ case LTTV_FILTER_TRACEFILE_NAME:
+ case LTTV_FILTER_STATE_P_NAME:
+ case LTTV_FILTER_EVENT_NAME:
+ case LTTV_FILTER_EVENT_FACILITY:
+ case LTTV_FILTER_STATE_EX_MODE:
+ case LTTV_FILTER_STATE_EX_SUBMODE:
+ case LTTV_FILTER_STATE_P_STATUS:
+ switch(op) {
+ case LTTV_FIELD_EQ:
+ se->op = lttv_apply_op_eq_quark;
+ break;
+ case LTTV_FIELD_NE:
+ se->op = lttv_apply_op_ne_quark;
+ break;
+ default:
+ g_warning("Error encountered in operator assignment = or != expected");
+ return FALSE;
+ }
+ break;
+ /*
+ * integer
+ */
+ case LTTV_FILTER_EVENT_TSC:
+ switch(op) {
+ case LTTV_FIELD_EQ:
+ se->op = lttv_apply_op_eq_uint64;
+ break;
+ case LTTV_FIELD_NE:
+ se->op = lttv_apply_op_ne_uint64;
+ break;
+ case LTTV_FIELD_LT:
+ se->op = lttv_apply_op_lt_uint64;
+ break;
+ case LTTV_FIELD_LE:
+ se->op = lttv_apply_op_le_uint64;
+ break;
+ case LTTV_FIELD_GT:
+ se->op = lttv_apply_op_gt_uint64;
+ break;
+ case LTTV_FIELD_GE:
+ se->op = lttv_apply_op_ge_uint64;
+ break;
+ default:
+ g_warning("Error encountered in operator assignment");
+ return FALSE;
+ }
+ break;
+ /*
+ * unsigned integers
+ */
+ case LTTV_FILTER_STATE_CPU:
+ case LTTV_FILTER_STATE_PID:
+ case LTTV_FILTER_STATE_PPID:
+ switch(op) {
+ case LTTV_FIELD_EQ:
+ se->op = lttv_apply_op_eq_uint;
+ break;
+ case LTTV_FIELD_NE:
+ se->op = lttv_apply_op_ne_uint;
+ break;
+ case LTTV_FIELD_LT:
+ se->op = lttv_apply_op_lt_uint;
+ break;
+ case LTTV_FIELD_LE:
+ se->op = lttv_apply_op_le_uint;
+ break;
+ case LTTV_FIELD_GT:
+ se->op = lttv_apply_op_gt_uint;
+ break;
+ case LTTV_FIELD_GE:
+ se->op = lttv_apply_op_ge_uint;
+ break;
+ default:
+ g_warning("Error encountered in operator assignment");
+ return FALSE;
+ }
+ break;
+
+ /*
+ * Enums
+ * Entered as string, converted to enum
+ *
+ * can only be compared with 'equal' or 'not equal' operators
+ *
+ * unsigned int of 16 bits are used here since enums
+ * should not go over 2^16-1 values
+ */
+// case /*NOTHING*/:
+// switch(op) {
+// case LTTV_FIELD_EQ:
+// se->op = lttv_apply_op_eq_uint16;
+// break;
+// case LTTV_FIELD_NE:
+// se->op = lttv_apply_op_ne_uint16;
+// break;
+// default:
+// g_warning("Error encountered in operator assignment = or != expected");
+// return FALSE;
+// }
+// break;
+ /*
+ * Ltttime
+ */
+ case LTTV_FILTER_STATE_CT:
+ case LTTV_FILTER_STATE_IT:
+ case LTTV_FILTER_EVENT_TIME:
+ switch(op) {
+ case LTTV_FIELD_EQ:
+ se->op = lttv_apply_op_eq_ltttime;
+ break;
+ case LTTV_FIELD_NE:
+ se->op = lttv_apply_op_ne_ltttime;
+ break;
+ case LTTV_FIELD_LT:
+ se->op = lttv_apply_op_lt_ltttime;
+ break;
+ case LTTV_FIELD_LE:
+ se->op = lttv_apply_op_le_ltttime;
+ break;
+ case LTTV_FIELD_GT:
+ se->op = lttv_apply_op_gt_ltttime;
+ break;
+ case LTTV_FIELD_GE:
+ se->op = lttv_apply_op_ge_ltttime;
+ break;
+ default:
+ g_warning("Error encountered in operator assignment");
+ return FALSE;
+ }
+ break;
+ default:
+ g_warning("Error encountered in operator assignation ! Field type:%i",se->field);
+ return FALSE;
+ }
+
+ return TRUE;
+
+}
+
+/**
+ * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
+ *
+ * Assign the value field to the current LttvSimpleExpression
+ * @param se pointer to the current LttvSimpleExpression
+ * @param value string value for simple expression
+ */
+gboolean
+lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value) {
+
+ unsigned i;
+ gboolean is_double = FALSE;
+ LttTime t = ltt_time_zero;
+ GString* v;
+
+ switch(se->field) {
+ /*
+ * Strings
+ * entered as strings, converted to Quarks
+ */
+ case LTTV_FILTER_TRACE_NAME:
+ case LTTV_FILTER_TRACEFILE_NAME:
+ case LTTV_FILTER_STATE_P_NAME:
+ case LTTV_FILTER_EVENT_NAME:
+ case LTTV_FILTER_EVENT_FACILITY:
+ case LTTV_FILTER_STATE_EX_MODE:
+ case LTTV_FILTER_STATE_EX_SUBMODE:
+ case LTTV_FILTER_STATE_P_STATUS:
+ // se->value.v_string = value;
+ se->value.v_quark = g_quark_from_string(value);
+ g_free(value);
+ break;
+ /*
+ * integer -- supposed to be uint64
+ */
+ case LTTV_FILTER_EVENT_TSC:
+ se->value.v_uint64 = atoi(value);
+ g_free(value);
+ break;
+ /*
+ * unsigned integers
+ */
+ case LTTV_FILTER_STATE_PID:
+ case LTTV_FILTER_STATE_PPID:
+ case LTTV_FILTER_STATE_CPU:
+ se->value.v_uint = atoi(value);
+ g_free(value);
+ break;
+ /*
+ * LttTime
+ */
+ case LTTV_FILTER_STATE_CT:
+ case LTTV_FILTER_STATE_IT:
+ case LTTV_FILTER_EVENT_TIME:
+ //se->value.v_double = atof(value);
+ /*
+ * parsing logic could be optimised,
+ * but as for now, simpler this way
+ */
+ v = g_string_new("");
+ for(i=0;i<strlen(value);i++) {
+ if(value[i] == '.') {
+ /* cannot specify number with more than one '.' */
+ if(is_double) return FALSE;
+ else is_double = TRUE;
+ t.tv_sec = atoi(v->str);
+ g_string_free(v,TRUE);
+ v = g_string_new("");
+ } else g_string_append_c(v,value[i]);
+ }
+ /* number can be integer or double */
+ if(is_double) t.tv_nsec = atoi(v->str);
+ else t.tv_sec = atoi(v->str);
+
+ g_string_free(v,TRUE);
+
+ se->value.v_ltttime = t;
+ g_free(value);
+ break;
+ default:
+ g_warning("Error encountered in value assignation ! Field type = %i",se->field);
+ g_free(value);
+ return FALSE;
+ }
+
+ return TRUE;
+
+}
+
+/**
+ * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
+ *
+ * Disallocate memory for the current
+ * simple expression
+ * @param se pointer to the current LttvSimpleExpression
+ */
+void
+lttv_simple_expression_destroy(LttvSimpleExpression* se) {
+
+ // g_free(se->value);
+// switch(se->field) {
+// case LTTV_FILTER_TRACE_NAME:
+// case LTTV_FILTER_TRACEFILE_NAME:
+// case LTTV_FILTER_STATE_P_NAME:
+// case LTTV_FILTER_EVENT_NAME:
+// g_free(se->value.v_string);
+// break;
+// }
+ g_free(se);
+
+}
+
+/**
+ * @fn gint lttv_struct_type(gint)
+ *
+ * Finds the structure type depending
+ * on the fields in parameters
+ * @params ft Field of the current structure
+ * @return LttvStructType enum or -1 for error
+ */
+gint
+lttv_struct_type(gint ft) {
+
+ switch(ft) {
+ case LTTV_FILTER_TRACE_NAME:
+ return LTTV_FILTER_TRACE;
+ break;
+ case LTTV_FILTER_TRACEFILE_NAME:
+ return LTTV_FILTER_TRACEFILE;
+ break;
+ case LTTV_FILTER_STATE_PID:
+ case LTTV_FILTER_STATE_PPID:
+ case LTTV_FILTER_STATE_CT:
+ case LTTV_FILTER_STATE_IT:
+ case LTTV_FILTER_STATE_P_NAME:
+ case LTTV_FILTER_STATE_EX_MODE:
+ case LTTV_FILTER_STATE_EX_SUBMODE:
+ case LTTV_FILTER_STATE_P_STATUS:
+ case LTTV_FILTER_STATE_CPU:
+ return LTTV_FILTER_STATE;
+ break;
+ case LTTV_FILTER_EVENT_NAME:
+ case LTTV_FILTER_EVENT_FACILITY:
+ case LTTV_FILTER_EVENT_CATEGORY:
+ case LTTV_FILTER_EVENT_TIME:
+ case LTTV_FILTER_EVENT_TSC:
+ case LTTV_FILTER_EVENT_FIELD:
+ return LTTV_FILTER_EVENT;
+ break;
+ default:
+ return -1;
+ }
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_uint(const gpointer v1, LttvFieldValue v2) {
+
+ guint* r = (guint*) v1;
+ return (*r == v2.v_uint);
+
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_uint64(const gpointer v1, LttvFieldValue v2) {
+
+ guint64* r = (guint64*) v1;
+ return (*r == v2.v_uint64);
+
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r == v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r == v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r == v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_string(const gpointer v1, LttvFieldValue v2) {
+ char* r = (char*) v1;
+ return (!g_strcasecmp(r,v2.v_string));
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_quark(const gpointer v1, LttvFieldValue v2) {
+ GQuark* r = (GQuark*) v1;
+ return (*r == v2.v_quark);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_eq_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+ return ltt_time_compare(*r, v2.v_ltttime)==0?1:0;
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_uint(const gpointer v1, LttvFieldValue v2) {
+ guint* r = (guint*) v1;
+ return (*r != v2.v_uint);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_uint64(const gpointer v1, LttvFieldValue v2) {
+ guint64* r = (guint64*) v1;
+ return (*r != v2.v_uint64);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r != v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r != v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r != v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_string(const gpointer v1, LttvFieldValue v2) {
+ char* r = (char*) v1;
+ return (g_strcasecmp(r,v2.v_string));
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_quark(const gpointer v1, LttvFieldValue v2) {
+ GQuark* r = (GQuark*) v1;
+ return (*r != v2.v_quark);
+}
+
+
+/**
+ * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'not equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ne_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+ return ltt_time_compare(*r, v2.v_ltttime)!=0?1:0;
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_uint(const gpointer v1, LttvFieldValue v2) {
+ guint* r = (guint*) v1;
+ return (*r < v2.v_uint);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_uint64(const gpointer v1, LttvFieldValue v2) {
+ guint64* r = (guint64*) v1;
+ return (*r < v2.v_uint64);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r < v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r < v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r < v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_lt_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+// return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec < v2.v_ltttime.tv_nsec)));
+ return ltt_time_compare(*r, v2.v_ltttime)==-1?1:0;
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_uint(const gpointer v1, LttvFieldValue v2) {
+ guint* r = (guint*) v1;
+ return (*r <= v2.v_uint);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_uint64(const gpointer v1, LttvFieldValue v2) {
+ guint64* r = (guint64*) v1;
+ return (*r <= v2.v_uint64);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r <= v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r <= v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r <= v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'lower or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_le_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+// return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec <= v2.v_ltttime.tv_nsec)));
+ return ltt_time_compare(*r, v2.v_ltttime)<1?1:0;
+}
+
+
+/**
+ * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_uint(const gpointer v1, LttvFieldValue v2) {
+ guint* r = (guint*) v1;
+ return (*r > v2.v_uint);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_uint64(const gpointer v1, LttvFieldValue v2) {
+ guint64* r = (guint64*) v1;
+ return (*r > v2.v_uint64);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r > v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r > v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r > v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater than' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_gt_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+// return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec > v2.v_ltttime.tv_nsec)));
+ return ltt_time_compare(*r, v2.v_ltttime)==1?1:0;
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_uint(const gpointer v1, LttvFieldValue v2) {
+ guint* r = (guint*) v1;
+ return (*r >= v2.v_uint);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_uint64(const gpointer v1, LttvFieldValue v2) {
+ guint64* r = (guint64*) v1;
+ return (*r >= v2.v_uint64);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_uint32(const gpointer v1, LttvFieldValue v2) {
+ guint32* r = (guint32*) v1;
+ return (*r >= v2.v_uint32);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_uint16(const gpointer v1, LttvFieldValue v2) {
+ guint16* r = (guint16*) v1;
+ return (*r >= v2.v_uint16);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_double(const gpointer v1, LttvFieldValue v2) {
+ double* r = (double*) v1;
+ return (*r >= v2.v_double);
+}
+
+/**
+ * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
+ *
+ * Applies the 'greater or equal' operator to the
+ * specified structure and value
+ * @param v1 left member of comparison
+ * @param v2 right member of comparison
+ * @return success/failure of operation
+ */
+gboolean lttv_apply_op_ge_ltttime(const gpointer v1, LttvFieldValue v2) {
+ LttTime* r = (LttTime*) v1;
+// return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec >= v2.v_ltttime.tv_nsec)));
+ return ltt_time_compare(*r, v2.v_ltttime)>-1?1:0;
+}
+
+
+
+/**
+ * Makes a copy of the current filter tree
+ * @param tree pointer to the current tree
+ * @return new copy of the filter tree
+ */
+LttvFilterTree*
+lttv_filter_tree_clone(const LttvFilterTree* tree) {
+
+ LttvFilterTree* newtree = lttv_filter_tree_new();
+
+ newtree->node = tree->node;
+
+ newtree->left = tree->left;
+ if(newtree->left == LTTV_TREE_NODE) {
+ newtree->l_child.t = lttv_filter_tree_clone(tree->l_child.t);
+ } else if(newtree->left == LTTV_TREE_LEAF) {
+ newtree->l_child.leaf = lttv_simple_expression_new();
+ newtree->l_child.leaf->field = tree->l_child.leaf->field;
+ newtree->l_child.leaf->offset = tree->l_child.leaf->offset;
+ newtree->l_child.leaf->op = tree->l_child.leaf->op;
+ /* FIXME: special case for string copy ! */
+ newtree->l_child.leaf->value = tree->l_child.leaf->value;
+ }
+
+ newtree->right = tree->right;
+ if(newtree->right == LTTV_TREE_NODE) {
+ newtree->r_child.t = lttv_filter_tree_clone(tree->r_child.t);
+ } else if(newtree->right == LTTV_TREE_LEAF) {
+ newtree->r_child.leaf = lttv_simple_expression_new();
+ newtree->r_child.leaf->field = tree->r_child.leaf->field;
+ newtree->r_child.leaf->offset = tree->r_child.leaf->offset;
+ newtree->r_child.leaf->op = tree->r_child.leaf->op;
+ newtree->r_child.leaf->value = tree->r_child.leaf->value;
+ }
+
+ return newtree;
+
+}
+
+/**
+ * Makes a copy of the current filter
+ * @param filter pointer to the current filter
+ * @return new copy of the filter
+ */
+LttvFilter*
+lttv_filter_clone(const LttvFilter* filter) {
+
+ if(!filter) return NULL;
+
+ LttvFilter* newfilter = g_new(LttvFilter,1);
+
+ strcpy(newfilter->expression,filter->expression);
+
+ newfilter->head = lttv_filter_tree_clone(filter->head);
+
+ return newfilter;
+
+}
+
+
+/**
+ * @fn LttvFilter* lttv_filter_new()
+ *
+ * Creates a new LttvFilter
+ * @return the current LttvFilter or NULL if error
+ */
+LttvFilter*
+lttv_filter_new() {
+
+ LttvFilter* filter = g_new(LttvFilter,1);
+ filter->expression = NULL;
+ filter->head = NULL;
+
+ return filter;
+
+}
+
+/**
+ * @fn gboolean lttv_filter_update(LttvFilter*)
+ *
+ * Updates the current LttvFilter by building
+ * its tree based upon the expression string
+ * @param filter pointer to the current LttvFilter
+ * @return Failure/Success of operation
+ */
+gboolean
+lttv_filter_update(LttvFilter* filter) {
+
+// g_print("filter::lttv_filter_new()\n"); /* debug */
+
+ if(filter->expression == NULL) return FALSE;
+
+ int
+ i,
+ p_nesting=0, /* parenthesis nesting value */
+ not=0;
+
+ /* trees */
+ LttvFilterTree
+ *tree = lttv_filter_tree_new(), /* main tree */
+ *subtree = NULL, /* buffer for subtrees */
+ *t1, /* buffer #1 */
+ *t2, /* buffer #2 */
+ *t3; /* buffer #3 */
+
+ /*
+ * the filter
+ * If the tree already exists,
+ * destroy it and build a new one
+ */
+ if(filter->head != NULL) lttv_filter_tree_destroy(filter->head);
+ filter->head = NULL; /* will be assigned at the end */
+
+ /*
+ * Tree Stack
+ * each element of the list
+ * is a sub tree created
+ * by the use of parenthesis in the
+ * global expression. The final tree
+ * will be the one left at the root of
+ * the list
+ */
+ GPtrArray *tree_stack = g_ptr_array_new();
+ g_ptr_array_add( tree_stack,(gpointer) tree );
+
+ /* temporary values */
+ GString *a_field_component = g_string_new("");
+ GString *a_string_spaces = g_string_new("");
+ GPtrArray *a_field_path = g_ptr_array_new();
+
+ /* simple expression buffer */
+ LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new();
+
+ /*
+ * Parse entire expression and construct
+ * the binary tree. There are two steps
+ * in browsing that string
+ * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
+ * 2. finding simple expressions
+ * - field path ( separated by dots )
+ * - op ( >, <, =, >=, <=, !=)
+ * - value ( integer, string ... )
+ * To spare computing time, the whole
+ * string is parsed in this loop for a
+ * O(n) complexity order.
+ *
+ * When encountering logical op &,|,^
+ * 1. parse the last value if any
+ * 2. create a new tree
+ * 3. add the expression (simple exp, or exp (subtree)) to the tree
+ * 4. concatenate this tree with the current tree on top of the stack
+ * When encountering math ops >,>=,<,<=,=,!=
+ * 1. add to op to the simple expression
+ * 2. concatenate last field component to field path
+ * When encountering concatening ops .
+ * 1. concatenate last field component to field path
+ * When encountering opening parenthesis (,{,[
+ * 1. create a new subtree on top of tree stack
+ * When encountering closing parenthesis ),},]
+ * 1. add the expression on right child of the current tree
+ * 2. the subtree is completed, allocate a new subtree
+ * 3. pop the tree value from the tree stack
+ */
+
+#ifdef TEST
+ struct timeval starttime;
+ struct timeval endtime;
+ gettimeofday(&starttime, NULL);
+#endif
+
+ for(i=0;i<strlen(filter->expression);i++) {
+ // debug
+// g_print("%c\n ",filter->expression[i]);
+
+ switch(filter->expression[i]) {
+ /*
+ * logical operators
+ */
+ case '&': /* and */
+
+ /* get current tree in tree stack */
+ t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
+
+ /* get current node at absolute right */
+ while(t1->right != LTTV_TREE_IDLE) {
+ g_assert(t1->right == LTTV_TREE_NODE);
+ t1 = t1->r_child.t;
+ }
+ t2 = lttv_filter_tree_new();
+ t2->node = LTTV_LOGICAL_AND;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t2;
+ if(not) { /* add not operator to tree */
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = t3;
+ t2 = t3;
+ not = 0;
+ }
+ if(subtree != NULL) { /* append subtree to current tree */
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = subtree;
+ subtree = NULL;
+ } else { /* append a simple expression */
+ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ t2->left = LTTV_TREE_LEAF;
+ t2->l_child.leaf = a_simple_expression;
+ a_simple_expression = lttv_simple_expression_new();
+ }
+ break;
+
+ case '|': /* or */
+
+ t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
+ while(t1->right != LTTV_TREE_IDLE) {
+ g_assert(t1->right == LTTV_TREE_NODE);
+ t1 = t1->r_child.t;
+ }
+ t2 = lttv_filter_tree_new();
+ t2->node = LTTV_LOGICAL_OR;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t2;
+ if(not) { // add not operator to tree
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = t3;
+ t2 = t3;
+ not = 0;
+ }
+ if(subtree != NULL) { /* append subtree to current tree */
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = subtree;
+ subtree = NULL;
+ } else { /* append a simple expression */
+ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ t2->left = LTTV_TREE_LEAF;
+ t2->l_child.leaf = a_simple_expression;
+ a_simple_expression = lttv_simple_expression_new();
+ }
+ break;
+
+ case '^': /* xor */
+
+ t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
+ while(t1->right != LTTV_TREE_IDLE) {
+ g_assert(t1->right == LTTV_TREE_NODE);
+ t1 = t1->r_child.t;
+ }
+ t2 = lttv_filter_tree_new();
+ t2->node = LTTV_LOGICAL_XOR;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t2;
+ if(not) { // add not operator to tree
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = t3;
+ t2 = t3;
+ not = 0;
+ }
+ if(subtree != NULL) { /* append subtree to current tree */
+ t2->left = LTTV_TREE_NODE;
+ t2->l_child.t = subtree;
+ subtree = NULL;
+ } else { /* append a simple expression */
+ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ t2->left = LTTV_TREE_LEAF;
+ t2->l_child.leaf = a_simple_expression;
+ a_simple_expression = lttv_simple_expression_new();
+ }
+ break;
+
+ case '!': /* not, or not equal (math op) */
+
+ if(filter->expression[i+1] == '=') { /* != */
+ g_ptr_array_add( a_field_path,(gpointer) a_field_component );
+ lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_NE);
+ i++;
+ } else { /* ! */
+ not=1;
+ }
+ break;
+
+ case '(': /* start of parenthesis */
+ case '[':
+ case '{':
+
+ p_nesting++; /* incrementing parenthesis nesting value */
+ t1 = lttv_filter_tree_new();
+ if(not) { /* add not operator to tree */
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t3;
+ not = 0;
+ }
+ g_ptr_array_add( tree_stack,(gpointer) t1 );
+ break;
+
+ case ')': /* end of parenthesis */
+ case ']':
+ case '}':
+
+ p_nesting--; /* decrementing parenthesis nesting value */
+ if(p_nesting<0 || tree_stack->len<2) {
+ g_warning("Wrong filtering options, the string\n\"%s\"\n\
+ is not valid due to parenthesis incorrect use",filter->expression);
+ return FALSE;
+ }
+
+ /* there must at least be the root tree left in the array */
+ g_assert(tree_stack->len>0);
+
+ t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
+ while(t1->right != LTTV_TREE_IDLE) {
+ t1 = t1->r_child.t;
+ }
+ if(not) { // add not operator to tree
+ g_print("ici");
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t3;
+ t1 = t3;
+ not = 0;
+ }
+ if(subtree != NULL) { /* append subtree to current tree */
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = subtree;
+ subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
+ g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
+ } else { /* assign subtree as current tree */
+ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ t1->right = LTTV_TREE_LEAF;
+ t1->r_child.leaf = a_simple_expression;
+ a_simple_expression = lttv_simple_expression_new();
+ subtree = g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
+ }
+ break;
+
+ /*
+ * mathematic operators
+ */
+ case '<': /* lower, lower or equal */
+
+ g_ptr_array_add( a_field_path,(gpointer) a_field_component );
+ lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ if(filter->expression[i+1] == '=') { /* <= */
+ i++;
+ lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LE);
+ } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LT);
+ break;
+
+ case '>': /* higher, higher or equal */
+
+ g_ptr_array_add( a_field_path,(gpointer) a_field_component );
+ lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ if(filter->expression[i+1] == '=') { /* >= */
+ i++;
+ lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GE);
+ } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GT);
+ break;
+
+ case '=': /* equal */
+
+ g_ptr_array_add( a_field_path,(gpointer) a_field_component );
+ lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_EQ);
+ break;
+
+ /*
+ * Field concatening caracter
+ */
+ case '.': /* dot */
+
+ /*
+ * divide field expression into elements
+ * in a_field_path array.
+ *
+ * A dot can also be present in double values
+ */
+ if(a_simple_expression->field == LTTV_FILTER_UNDEFINED) {
+ g_ptr_array_add( a_field_path,(gpointer) a_field_component );
+ a_field_component = g_string_new("");
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = g_string_new("");
+ }
+ break;
+ case ' ': /* keep spaces that are within a field component */
+ if(a_field_component->len == 0) break; /* ignore */
+ else
+ a_string_spaces = g_string_append_c(a_string_spaces,
+ filter->expression[i]);
+
+ case '\n': /* ignore */
+ break;
+ default: /* concatening current string */
+ if(a_string_spaces->len != 0) {
+ g_string_append(a_field_component, a_string_spaces->str);
+ a_string_spaces = g_string_set_size(a_string_spaces, 0);
+ }
+ a_field_component = g_string_append_c(a_field_component,
+ filter->expression[i]);
+ }
+ }
+
+ /*
+ * Preliminary check to see
+ * if tree was constructed correctly
+ */
+ if( p_nesting>0 ) {
+ g_warning("Wrong filtering options, the string\n\"%s\"\n\
+ is not valid due to parenthesis incorrect use",filter->expression);
+ return FALSE;
+ }
+
+ if(tree_stack->len != 1) /* only root tree should remain */
+ return FALSE;
+
+ /*
+ * processing last element of expression
+ */
+ t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
+ while(t1->right != LTTV_TREE_IDLE) {
+ g_assert(t1->right == LTTV_TREE_NODE);
+ t1 = t1->r_child.t;
+ }
+ if(not) { // add not operator to tree
+ t3 = lttv_filter_tree_new();
+ t3->node = LTTV_LOGICAL_NOT;
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = t3;
+ t1 = t3;
+ not = 0;
+ }
+ if(subtree != NULL) { /* add the subtree */
+ t1->right = LTTV_TREE_NODE;
+ t1->r_child.t = subtree;
+ subtree = NULL;
+ } else { /* add a leaf */
+ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
+ a_field_component = NULL;
+ g_string_free(a_string_spaces, TRUE);
+ a_string_spaces = NULL;
+ t1->right = LTTV_TREE_LEAF;
+ t1->r_child.leaf = a_simple_expression;
+ a_simple_expression = NULL;
+ }
+
+
+ /* free the pointer array */
+ g_assert(a_field_path->len == 0);
+ g_ptr_array_free(a_field_path,TRUE);
+
+ /* free the tree stack -- but keep the root tree */
+ filter->head = g_ptr_array_remove_index(tree_stack,0);
+ g_ptr_array_free(tree_stack,TRUE);
+
+ /* free the field buffer if allocated */
+ if(a_field_component != NULL) g_string_free(a_field_component,TRUE);
+ if(a_string_spaces != NULL) g_string_free(a_string_spaces, TRUE);