diff options
author | Tambet Ingo <tambet@gmail.com> | 2008-10-24 16:20:22 +0300 |
---|---|---|
committer | Tambet Ingo <tambet@gmail.com> | 2008-10-24 16:20:22 +0300 |
commit | f82b187c2bb3efb117f96e5b6062128f20ab2f39 (patch) | |
tree | b9dad67b61990b30a9c7add57b7b839a5dfee429 /src/mm-util.c | |
parent | 0bd4f4a6049838dd7b09320c18c1991196ae80ee (diff) |
Move the string parser with regexp from huawei plugin to generic utility function.
It's useful for other modems too that need to strip unsolicited messages from
responses.
Diffstat (limited to 'src/mm-util.c')
-rw-r--r-- | src/mm-util.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/mm-util.c b/src/mm-util.c new file mode 100644 index 00000000..33796ac6 --- /dev/null +++ b/src/mm-util.c @@ -0,0 +1,54 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include "mm-util.h" + +static gboolean +remove_eval_cb (const GMatchInfo *match_info, + GString *result, + gpointer user_data) +{ + int *result_len = (int *) user_data; + int start; + int end; + + if (g_match_info_fetch_pos (match_info, 0, &start, &end)) + *result_len -= (end - start); + + return FALSE; +} + +void +mm_util_strip_string (GString *string, + GRegex *regex, + MMUtilStripFn callback, + gpointer user_data) +{ + GMatchInfo *match_info; + gboolean matches; + char *str; + + matches = g_regex_match_full (regex, string->str, string->len, 0, 0, &match_info, NULL); + if (callback) { + while (g_match_info_matches (match_info)) { + str = g_match_info_fetch (match_info, 1); + callback (str, user_data); + g_free (str); + + g_match_info_next (match_info, NULL); + } + } + + g_match_info_free (match_info); + + if (matches) { + /* Remove matches */ + int result_len = string->len; + + str = g_regex_replace_eval (regex, string->str, string->len, 0, 0, + remove_eval_cb, &result_len, NULL); + + g_string_truncate (string, 0); + g_string_append_len (string, str, result_len); + g_free (str); + } +} |