diff options
author | Nicolas Dufresne <nicolas.dufresne@collabora.co.uk> | 2011-03-18 17:14:23 -0400 |
---|---|---|
committer | Nicolas Dufresne <nicolas.dufresne@collabora.co.uk> | 2011-03-25 14:03:03 -0400 |
commit | d7844626f21999884f29307660af35245a52ea7d (patch) | |
tree | f0ffbb6c768d82ab8bf802497c157fa8c04da850 | |
parent | 9af6d6177c9659db9d0df6a6ddd9dfa83b48c2f2 (diff) |
Use regex instead of simple strstr to search XML
-rw-r--r-- | telepathy-logger/log-store-xml.c | 58 | ||||
-rw-r--r-- | tests/dbus/test-tpl-log-store-xml.c | 28 |
2 files changed, 57 insertions, 29 deletions
diff --git a/telepathy-logger/log-store-xml.c b/telepathy-logger/log-store-xml.c index 1646aca..5159088 100644 --- a/telepathy-logger/log-store-xml.c +++ b/telepathy-logger/log-store-xml.c @@ -1073,18 +1073,47 @@ log_store_xml_get_all_files (TplLogStoreXml *self, static GList * _log_store_xml_search_in_files (TplLogStoreXml *self, const gchar *text, - GList *files) + GList *files, + guint type_mask) { GList *l; GList *hits = NULL; - gchar *text_casefold; + gchar *markup_text; gchar *escaped_text; + GString *pattern = NULL; + GRegex *regex = NULL; + GError *error = NULL; g_return_val_if_fail (TPL_IS_LOG_STORE_XML (self), NULL); g_return_val_if_fail (!TPL_STR_EMPTY (text), NULL); - escaped_text = g_markup_escape_text (text, -1); - text_casefold = g_utf8_casefold (escaped_text, -1); + markup_text = g_markup_escape_text (text, -1); + escaped_text = g_regex_escape_string (markup_text, -1); + g_free (markup_text); + + pattern = g_string_new (""); + + if (type_mask & TPL_EVENT_MASK_TEXT) + g_string_append_printf (pattern, + "<message [^>]*>[^<]*%s[^<]*</message>" + "|<message( [^>]* | )id='[^>]*%s[^>]*'" + "|<message( [^>]* | )name='[^>]*%s[^>]*'", + escaped_text, escaped_text, escaped_text); + + if (TPL_STR_EMPTY (pattern->str)) + goto out; + + regex = g_regex_new (pattern->str, + G_REGEX_CASELESS | G_REGEX_OPTIMIZE, + 0, + &error); + + if (!regex) + { + DEBUG ("Failed to compile regex: %s", error->message); + g_error_free (error); + goto out; + } for (l = files; l; l = g_list_next (l)) { @@ -1092,7 +1121,6 @@ _log_store_xml_search_in_files (TplLogStoreXml *self, GMappedFile *file; gsize length; gchar *contents = NULL; - gchar *contents_casefold = NULL; filename = l->data; @@ -1106,9 +1134,7 @@ _log_store_xml_search_in_files (TplLogStoreXml *self, if (length == 0 || contents == NULL) goto fail; - contents_casefold = g_utf8_casefold (contents, length); - - if (strstr (contents_casefold, text_casefold)) + if (g_regex_match_full (regex, contents, length, 0, 0, NULL, NULL)) { TplLogSearchHit *hit; @@ -1126,14 +1152,19 @@ fail: if (file != NULL) g_mapped_file_unref (file); - g_free (contents_casefold); g_free (filename); } - g_list_free (files); - g_free (text_casefold); +out: g_free (escaped_text); + if (pattern != NULL) + g_string_free (pattern, TRUE); + + if (regex != NULL) + g_regex_unref (regex); + + g_list_free (files); return hits; } @@ -1149,13 +1180,10 @@ log_store_xml_search_new (TplLogStore *store, g_return_val_if_fail (TPL_IS_LOG_STORE_XML (self), NULL); g_return_val_if_fail (!TPL_STR_EMPTY (text), NULL); - if (!(type_mask & TPL_EVENT_MASK_TEXT)) - return NULL; - files = log_store_xml_get_all_files (self, NULL); DEBUG ("Found %d log files in total", g_list_length (files)); - return _log_store_xml_search_in_files (self, text, files); + return _log_store_xml_search_in_files (self, text, files, type_mask); } /* Returns: (GList *) of (TplLogSearchHit *) */ diff --git a/tests/dbus/test-tpl-log-store-xml.c b/tests/dbus/test-tpl-log-store-xml.c index 477cb90..91a4e4c 100644 --- a/tests/dbus/test-tpl-log-store-xml.c +++ b/tests/dbus/test-tpl-log-store-xml.c @@ -98,18 +98,18 @@ test_clear (XmlTestCaseFixture *fixture, { GList *hits; hits = _tpl_log_store_search_new (fixture->store, - "1263405203", + "user@collabora.co.uk", TPL_EVENT_MASK_TEXT); g_assert (hits != NULL); - g_assert_cmpint (g_list_length (hits), ==, 1); + g_assert_cmpint (g_list_length (hits), ==, 4); tpl_log_manager_search_free (hits); _tpl_log_store_clear (fixture->store); hits = _tpl_log_store_search_new (fixture->store, - "1263405203", + "user@collabora.co.uk", TPL_EVENT_MASK_TEXT); g_assert_cmpint (g_list_length (hits), ==, 0); @@ -123,13 +123,13 @@ test_clear_account (XmlTestCaseFixture *fixture, GList *hits; TpAccount *account; GError *error = NULL; - const gchar *kept = "1263405203"; - const gchar *cleared = "f95e605a3ae97c463b626a3538567bc90fc58730"; + const gchar *kept = "user2@collabora.co.uk"; + const gchar *cleared = "test2@collabora.co.uk"; hits = _tpl_log_store_search_new (fixture->store, kept, TPL_EVENT_MASK_TEXT); - g_assert_cmpint (g_list_length (hits), ==, 1); + g_assert_cmpint (g_list_length (hits), ==, 4); tpl_log_manager_search_free (hits); @@ -152,7 +152,7 @@ test_clear_account (XmlTestCaseFixture *fixture, hits = _tpl_log_store_search_new (fixture->store, kept, TPL_EVENT_MASK_TEXT); - g_assert_cmpint (g_list_length (hits), ==, 1); + g_assert_cmpint (g_list_length (hits), ==, 4); tpl_log_manager_search_free (hits); @@ -174,23 +174,23 @@ test_clear_entity (XmlTestCaseFixture *fixture, GError *error = NULL; const gchar *always_kept, *kept, *cleared; - always_kept = "1263405203"; + always_kept = "user2@collabora.co.uk"; if (is_room) { - kept = "f95e605a3ae97c463b626a3538567bc90fc58730"; - cleared = "8957fb4064049e7a1f9d8f84234d3bf09fb6778c"; + kept = "Hey, Just generating logs"; + cleared = "meego@conference.collabora.co.uk/test2@collabora.co.uk"; } else { - kept = "8957fb4064049e7a1f9d8f84234d3bf09fb6778c"; - cleared = "f95e605a3ae97c463b626a3538567bc90fc58730"; + kept = "meego@conference.collabora.co.uk/test2@collabora.co.uk"; + cleared = "Hey, Just generating logs"; } hits = _tpl_log_store_search_new (fixture->store, always_kept, TPL_EVENT_MASK_TEXT); - g_assert_cmpint (g_list_length (hits), ==, 1); + g_assert_cmpint (g_list_length (hits), ==, 4); tpl_log_manager_search_free (hits); @@ -227,7 +227,7 @@ test_clear_entity (XmlTestCaseFixture *fixture, hits = _tpl_log_store_search_new (fixture->store, always_kept, TPL_EVENT_MASK_TEXT); - g_assert_cmpint (g_list_length (hits), ==, 1); + g_assert_cmpint (g_list_length (hits), ==, 4); tpl_log_manager_search_free (hits); |