+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);
+
+ /* free the simple expression buffer if allocated */
+ if(a_simple_expression != NULL) lttv_simple_expression_destroy(a_simple_expression);
+
+ g_assert(filter->head != NULL); /* tree should exist */
+ g_assert(subtree == NULL); /* remaining subtree should be included in main tree */
+
+#ifdef TEST
+ gettimeofday(&endtime, NULL);
+
+ /* Calcul du temps de l'algorithme */
+ double time1 = starttime.tv_sec + (starttime.tv_usec/1000000.0);
+ double time2 = endtime.tv_sec + (endtime.tv_usec/1000000.0);
+// g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
+ g_print("%.10f %i\n",(time2-time1)*1000,strlen(filter->expression));
+#endif
+
+ /* debug */
+ g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
+ lttv_print_tree(filter->head,0) ;
+ g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
+
+ /* success */
+ return TRUE;
+
+}
+
+/**
+ * @fn void lttv_filter_destroy(LttvFilter*)
+ *
+ * Destroy the current LttvFilter
+ * @param filter pointer to the current LttvFilter
+ */
+void
+lttv_filter_destroy(LttvFilter* filter) {
+
+ if(!filter) return;
+
+ if(filter->expression)
+ g_free(filter->expression);
+ if(filter->head)
+ lttv_filter_tree_destroy(filter->head);
+ g_free(filter);
+
+}
+
+/**
+ * @fn LttvFilterTree* lttv_filter_tree_new()
+ *
+ * Assign a new tree for the current expression
+ * or sub expression
+ * @return pointer of LttvFilterTree
+ */
+LttvFilterTree*
+lttv_filter_tree_new() {
+ LttvFilterTree* tree;
+
+ tree = g_new(LttvFilterTree,1);
+ tree->node = 0; //g_new(lttv_expression,1);
+ tree->left = LTTV_TREE_IDLE;
+ tree->right = LTTV_TREE_IDLE;
+ tree->r_child.t = NULL;
+ tree->l_child.t = NULL;
+
+ return tree;
+}
+
+/**
+ * @fn void lttv_filter_append_expression(LttvFilter*,char*)
+ *
+ * Append a new expression to the expression
+ * defined in the current filter
+ * @param filter pointer to the current LttvFilter
+ * @param expression string that must be appended
+ * @return Success/Failure of operation
+ */
+gboolean
+lttv_filter_append_expression(LttvFilter* filter, const char *expression) {
+
+ if(expression == NULL) return FALSE;
+ if(filter == NULL) return FALSE;
+ if(expression[0] == '\0') return FALSE; /* Empty expression */
+
+ GString* s = g_string_new("");
+ if(filter->expression != NULL) {
+ g_string_append(s,filter->expression);
+ g_string_append_c(s,'&');
+ }
+ g_string_append(s,expression);
+
+ g_free(filter->expression);
+ filter->expression = g_string_free(s,FALSE);
+
+ /* TRUE if construction of tree proceeded without errors */
+ return lttv_filter_update(filter);
+
+}
+
+/**
+ * @fn void lttv_filter_clear_expression(LttvFilter*)
+ *
+ * Clear the filter expression from the
+ * current filter and sets its pointer to NULL
+ * @param filter pointer to the current LttvFilter
+ */
+void
+lttv_filter_clear_expression(LttvFilter* filter) {
+
+ if(filter->expression != NULL) {
+ g_free(filter->expression);
+ filter->expression = NULL;
+ }
+
+}
+
+/**
+ * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
+ *
+ * Destroys the tree and his sub-trees
+ * @param tree Tree which must be destroyed
+ */
+void
+lttv_filter_tree_destroy(LttvFilterTree* tree) {
+
+ if(tree == NULL) return;