Generalize matcher list building from lines
authorRicardo Mones <ricardo@mones.org>
Wed, 27 Mar 2013 09:12:55 +0000 (10:12 +0100)
committerRicardo Mones <ricardo@mones.org>
Sun, 31 Mar 2013 01:37:46 +0000 (03:37 +0200)
New function "matcherlist_new_from_lines" to build the MatcherList
required to match one or more regexps from a newline-separated string.

Seen previously a specific internal function of AttachWarner plugin.

src/matcher.c
src/matcher.h

index 19fa12a3aef646e5dfd22b075ecd4a179ea9f9fe..c95655cf49889b1e5a05b94d0ff8c7bbb3d5f4bc 100644 (file)
@@ -1134,6 +1134,103 @@ MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
        return cond;
 }
 
+#ifdef G_OS_UNIX
+/*!
+ *\brief       Builds a single regular expresion from an array of srings.
+ *
+ *\param       strings The lines containing the different sub-regexp.
+ *
+ *\return      The newly allocated regexp string.
+ */
+static gchar *build_complete_regexp(gchar **strings)
+{
+       int i = 0;
+       gchar *expr = NULL;
+       while (strings && strings[i] && *strings[i]) {
+               int old_len = expr ? strlen(expr):0;
+               int new_len = 0;
+               gchar *tmpstr = NULL;
+
+               if (g_utf8_validate(strings[i], -1, NULL))
+                       tmpstr = g_strdup(strings[i]);
+               else
+                       tmpstr = conv_codeset_strdup(strings[i], 
+                                       conv_get_locale_charset_str_no_utf8(),
+                                       CS_INTERNAL);
+
+               if (strstr(tmpstr, "\n"))
+                       *(strstr(tmpstr, "\n")) = '\0';
+
+               new_len = strlen(tmpstr);
+
+               expr = g_realloc(expr, 
+                       expr ? (old_len + strlen("|()") + new_len + 1)
+                            : (strlen("()") + new_len + 1));
+               
+               if (old_len) {
+                       strcpy(expr + old_len, "|(");
+                       strcpy(expr + old_len + 2, tmpstr);
+                       strcpy(expr + old_len + 2 + new_len, ")");
+               } else {
+                       strcpy(expr+old_len, "(");
+                       strcpy(expr+old_len + 1, tmpstr);
+                       strcpy(expr+old_len + 1 + new_len, ")");
+               }
+               g_free(tmpstr);
+               i++;
+       }
+       return expr;
+}
+#endif
+
+/*!
+ *\brief       Create a new list of matchers from a multi-line string
+ *
+ *\param       lines String with "\n"-separated expressions
+ *\param       bool_and Operator
+ *
+ *\return      MatcherList * New matcher list
+ */
+MatcherList *matcherlist_new_from_lines(gchar *lines, gboolean bool_and)
+{
+       MatcherProp *m = NULL;
+       GSList *matchers = NULL;
+       gchar **strings = g_strsplit(lines, "\n", -1);
+
+#ifdef G_OS_UNIX
+       gchar *expr = NULL;
+       expr = build_complete_regexp(strings);
+       debug_print("building matcherprop for expr '%s'\n", expr?expr:"NULL");
+       
+       m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, MATCHTYPE_REGEXP, 
+                           expr, 0);
+       if (m == NULL) {
+               /* print error message */
+               debug_print("failed to allocate memory for matcherprop\n");
+       } else {
+               matchers = g_slist_append(matchers, m);
+       }
+
+       g_free(expr);
+#else
+       int i = 0;
+       while (strings && strings[i] && *strings[i]) {
+               m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, MATCHTYPE_MATCHCASE, 
+                           strings[i], 0);
+               if (m == NULL) {
+                       /* print error message */
+                       debug_print("failed to allocate memory for matcherprop\n");
+               } else {
+                       matchers = g_slist_append(matchers, m);
+               }
+               i++;
+       }
+#endif
+       g_strfreev(strings);
+
+       return matcherlist_new(matchers, bool_and);
+}
+
 /*!
  *\brief       Frees a list of matchers
  *
index 56b0d9619d63d5b43c39b55b588b813d6330990e..13542a68007714f786a3daae7d51525654551c67 100644 (file)
@@ -162,6 +162,8 @@ MatcherProp *matcherprop_copy               (const MatcherProp *src);
 
 MatcherList * matcherlist_new          (GSList         *matchers, 
                                         gboolean       bool_and);
+MatcherList * matcherlist_new_from_lines(gchar         *lines,
+                                        gboolean       bool_and);
 void matcherlist_free                  (MatcherList    *cond);
 
 MatcherList *matcherlist_parse         (gchar          **str);