filter text module:
[lttv.git] / ltt / branches / poly / lttv / modules / text / textFilter.c
index bcca87515df5a354fecc11034026756941918a0e..8f7a7d81ed567a4da53a9ad8c514bcdf94ececf7 100644 (file)
  * MA 02111-1307, USA.
  */
 
-/* The text filter facility will prompt for user filter option 
- * and transmit them to the lttv core */
+/* 
+ * The text filter facility will prompt for user filter option 
+ * and transmit them to the lttv core 
+ */
 
 #include <lttv/lttv.h>
 #include <lttv/option.h>
 /* Insert the hooks before and after each trace and tracefile, and for each
    event. Print a global header. */
 
-static char
+/*
+ *  YET TO BE ANSWERED !
+ *  - why does this module need dependency with batchAnalysis ?
+ */
+
+/*
+ *  TODO
+ *  - specify wich hook function will be used to call the core filter
+ */
+
+static char 
   *a_file_name = NULL,
-  *a_filter_string = NULL;
+  *a_string = NULL;
 
 static LttvHooks
   *before_traceset,
   *event_hook;
 
-static FILE *a_file;
-
 /**
- * analyses user filter options and issues the filter string
- * to the core
+ * filters the file input from user
  * @param hook_data the hook data
- * @param call_data the call data
+ * @return success/failure of operation
  */
-static void parse_filter_options(void *hook_data, void *call_data) {
-
-       char* parsed_string=NULL;       /* the string compiled from user's input */ 
-       
-       /* debug */
-       g_print("textFilter::parse_filter_options\n");  
-       
-       /* recovering hooks data */
-       LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
-
-       LttvTracefileState *tfs = (LttvTracefileState *)call_data;
-
-//     LttvTrace* lt = tfc->t_context->vt;
-       
-       /* hook header */
-       g_info("TextFilter options parser");
-       
-       /*
+void filter_analyze_file(void *hook_data) {
+
+  g_print("textFilter::filter_analyze_file\n");
+  LttvAttributeValue value;
+
+  LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
+
+  /*
         *      User may specify filtering options through static file
         *      and/or command line string.  From these sources, an 
         *      option string is rebuilded and sent to the filter core
         */
-       if(a_file_name != NULL) {       /* -f switch in use */
-               a_file = fopen(a_file_name, "r"); 
-               if(a_file == NULL) 
-                       g_error("textFilter::parse_filter_content() cannot open file %s", a_file_name);
-
-               fscanf(a_file,"%s",parsed_string);
-               
-               fclose(a_file);
-       }
-       if(a_filter_string != NULL)     /* -s switch in use */
-               parsed_string = a_filter_string;        
-
-       if(parsed_string==NULL) 
-               g_warning("textFilter::parser_filter_options() no filtering options specified !"); 
-
-       /*      send the filtering string to the core   */
-       lttv_filter_new(parsed_string,tfs);     
-       
+  
+/* OLD CODE
+ * GString* a_file_content = g_string_new("");
+  a_file = fopen(a_file_name, "r"); 
+       if(a_file == NULL) { 
+               g_warning("file %s does not exist", a_file_name);
+    return;
+  }
+  
+  char* line = NULL;
+  size_t len = 0;
+
+  while(!feof(a_file)) {
+    len = getline(&line,&len,a_file);
+    g_string_append(a_file_content,line);
+  }
+  free(line);
+
+  fclose(a_file);
+*/  
+  
+  if(!g_file_test(a_file_name,G_FILE_TEST_EXISTS)) {
+    g_warning("file %s does not exist", a_file_name);
+    return;
+  }
+
+  char* a_file_content = NULL;
+
+  g_file_get_contents(a_file_name,&a_file_content,NULL,NULL);
+  
+  g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
+      LTTV_POINTER, &value));
+
+  if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&');
+  g_string_append_c((GString*)*(value.v_pointer),'(');
+  g_string_append((GString*)*(value.v_pointer),a_file_content);
+  g_string_append_c((GString*)*(value.v_pointer),')');
+  
 }
 
 /**
- * filter to current event depending on the 
- * filter options tree
+ * filters the string input from user
  * @param hook_data the hook data
- * @param call_data the call data
- * @return success/error of operation
+ * @return success/failure of operation
  */
-static gboolean filter_event_content(void *hook_data, void *call_data) {
+void filter_analyze_string(void *hook_data) {
+
+  g_print("textFilter::filter_analyze_string\n");
+
+  LttvAttributeValue value;
 
-       g_print("textFilter::filter_event_content\n");  /* debug */
+  LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
+  
+  /*
+        *      User may specify filtering options through static file
+        *      and/or command line string.  From these sources, an 
+        *      option string is rebuilded and sent to the filter core
+        */
+/*  if(a_filter_string==NULL) {
+    a_filter_string = g_string_new("");
+    g_string_append(a_filter_string,a_string);
+  }
+  else {
+    g_string_append(a_filter_string,"&"); 
+    g_string_append(a_filter_string,a_string);
+  }
+*/
+  
+  g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
+      LTTV_POINTER, &value));
+
+  if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&');
+  g_string_append_c((GString*)*(value.v_pointer),'(');
+  g_string_append((GString*)*(value.v_pointer),a_string);
+  g_string_append_c((GString*)*(value.v_pointer),')');
+
+//  LttvFilter* filter = lttv_filter_new();
+//  lttv_filter_append_expression(filter,a_string);
 }
 
 /**
@@ -112,43 +160,32 @@ static gboolean filter_event_content(void *hook_data, void *call_data) {
  */
 static void init() {
   
-  g_print("textFilter::init()");       /* debug */
-       
+  g_print("textFilter::init()\n");     /* debug */
+
   LttvAttributeValue value;
 
   LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
 
-  g_info("Init textFilter.c");
+  g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
+      LTTV_POINTER, &value));
   
-  a_filter_string = NULL;
-  lttv_option_add("string", 's', 
+  *(value.v_pointer) = g_string_new("");
+
+  g_info("Init textFilter.c");
+  a_string = NULL;
+  lttv_option_add("expression", 'e', 
       "filters a string issued by the user on the command line", 
       "string", 
-      LTTV_OPT_STRING, &a_filter_string, NULL, NULL);
+      LTTV_OPT_STRING, &a_string, filter_analyze_string, NULL);
   // add function to call for option
   
   a_file_name = NULL;
   lttv_option_add("filename", 'f', 
       "browse the filter options contained in specified file", 
       "file name", 
-      LTTV_OPT_STRING, &a_file_name, NULL, NULL);
-  
-  /*
-   *   Note to myself !
-   *   LttvAttributeValue::v_pointer is a gpointer* --> void**
-   *   see union LttvAttributeValue for more info 
-   */
-  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event",
-      LTTV_POINTER, &value));
-  g_assert((event_hook = *(value.v_pointer)) != NULL);
-  lttv_hooks_add(event_hook, filter_event_content, NULL, LTTV_PRIO_DEFAULT);
+      LTTV_OPT_STRING, &a_file_name, filter_analyze_file, NULL);
 
-  g_assert(lttv_iattribute_find_by_path(attributes,"hooks/trace/before",
-       LTTV_POINTER, &value));
-  g_assert((before_traceset = *(value.v_pointer)) != NULL);
-  lttv_hooks_add(before_traceset, parse_filter_options, NULL, LTTV_PRIO_DEFAULT);
-
-  
 }
 
 /**
@@ -157,13 +194,18 @@ static void init() {
 static void destroy() {
   g_info("Destroy textFilter");
 
-  lttv_option_remove("string");
+  lttv_option_remove("expression");
 
   lttv_option_remove("filename");
 
-  lttv_hooks_remove_data(event_hook, filter_event_content, NULL);
+  LttvAttributeValue value;
+
+  LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
 
-  lttv_hooks_remove_data(before_traceset, parse_filter_options, NULL);
+  g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
+      LTTV_POINTER, &value));
+  g_string_free((GString*)*(value.v_pointer),TRUE);
   
 }
 
This page took 0.025109 seconds and 4 git commands to generate.