commit fe709cce38ba0f99759752102c1012f2fc0f265f
parent a839f1b6111f9a5b9af50fee05095607853530b2
Author: Ricardo Mones <ricardo@mones.org>
Date: Wed, 27 Mar 2013 10:12:55 +0100
Generalize matcher list building from lines
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.
Diffstat:
2 files changed, 99 insertions(+), 0 deletions(-)
diff --git a/src/matcher.c b/src/matcher.c
@@ -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
*
diff --git a/src/matcher.h b/src/matcher.h
@@ -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);